Skip to content

Commit

Permalink
feat:support for redis 5.1.0b1
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Jan 1, 2024
1 parent e9ec46a commit d2cc98e
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
matrix:
redis-image: [ "redis:6.2.13", "redis:7.0.12", "redis:7.2.2" ]
python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12" ]
redis-py: [ "4.3.6", "4.6.0", "5.0.1" ]
redis-py: [ "4.3.6", "4.6.0", "5.0.1", "5.1.0b1" ]
include:
- python-version: "3.11"
redis-image: "redis:6.2.13"
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/implement-command.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ to the real server output.
decorator `@testtools.run_test_if_redispy_ver` on your tests. example:

```python
@testtools.run_test_if_redispy_ver('above', '4.2.0') # This will run for redis-py 4.2.0 or above.
@testtools.run_test_if_redispy_ver('gte', '4.2.0') # This will run for redis-py 4.2.0 or above.
def test_expire_should_not_expire__when_no_expire_is_set(r):
r.set('foo', 'bar')
assert r.get('foo') == b'bar'
Expand Down
5 changes: 3 additions & 2 deletions docs/guides/test-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ To write a new test case for a command:
- Determine when support for the command was introduced
- To limit the redis-server versions, it will run on use:
`@pytest.mark.max_server(version)` and `@pytest.mark.min_server(version)`
- To limit the redis-py version use `@run_test_if_redispy_ver(above/below, version)`
- To limit the redis-py version use `@run_test_if_redispy_ver('gte', version)`
(you can use `ge`/`gte`/`lte`/`lt`/`eq`/`ne`).
- pytest will inject a redis connection to the argument `r` of the test.

Sample of running a test for redis-py v4.2.0 and above, redis-server 7.0 and above.

```python
@pytest.mark.min_server('7')
@testtools.run_test_if_redispy_ver('above', '4.2.0')
@testtools.run_test_if_redispy_ver('gte', '4.2.0')
def test_expire_should_not_expire__when_no_expire_is_set(r):
r.set('foo', 'bar')
assert r.get('foo') == b'bar'
Expand Down
4 changes: 2 additions & 2 deletions test/test_json/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ def test_nummultby(r: redis.Redis):
assert r.json().nummultby("doc1", ".b[0].a", 3) == 6


@testtools.run_test_if_redispy_ver('above', '4.6')
@testtools.run_test_if_redispy_ver('gte', '4.6')
@pytest.mark.min_server('7.1')
def test_json_merge(r: redis.Redis):
# Test with root path $
Expand All @@ -579,7 +579,7 @@ def test_json_merge(r: redis.Redis):
}


@testtools.run_test_if_redispy_ver('above', '4.6')
@testtools.run_test_if_redispy_ver('gte', '4.6')
@pytest.mark.min_server('7.1')
def test_mset(r: redis.Redis):
r.json().mset([("1", Path.root_path(), 1), ("2", Path.root_path(), 2)])
Expand Down
2 changes: 1 addition & 1 deletion test/test_mixins/test_hash_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def test_hmset_empty_raises_error(r: redis.Redis):
r.hmset('foo', {})


@testtools.run_test_if_redispy_ver('below', '4.6')
@testtools.run_test_if_redispy_ver('lte', '4.6')
def test_hmset_redispy4(r: redis.Redis):
r.hset('foo', 'k1', 'v1')
assert r.hmset('foo', {'k2': 'v2', 'k3': 'v3'}) is True
Expand Down
10 changes: 5 additions & 5 deletions test/test_mixins/test_pubsub_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def test_pubsub_run_in_thread(r: redis.Redis):

@pytest.mark.slow
@pytest.mark.parametrize("timeout_value", [
1, pytest.param(None, marks=testtools.run_test_if_redispy_ver('above', '3.2'))
1, pytest.param(None, marks=testtools.run_test_if_redispy_ver('gte', '3.2'))
])
def test_pubsub_timeout(r, timeout_value):
def publish():
Expand Down Expand Up @@ -448,7 +448,7 @@ def test_pubsub_numsub(r: redis.Redis):


@pytest.mark.min_server('7')
@testtools.run_test_if_redispy_ver('above', '5.0.0rc2')
@testtools.run_test_if_redispy_ver('gte', '5.0.0rc2')
def test_published_message_to_shard_channel(r: redis.Redis):
p = r.pubsub()
p.ssubscribe("foo")
Expand All @@ -461,7 +461,7 @@ def test_published_message_to_shard_channel(r: redis.Redis):


@pytest.mark.min_server('7')
@testtools.run_test_if_redispy_ver('above', '5.0.0rc2')
@testtools.run_test_if_redispy_ver('gte', '5.0.0rc2')
def test_subscribe_property_with_shard_channels_cluster(r: redis.Redis):
p = r.pubsub()
keys = ["foo", "bar", "uni" + chr(4456) + "code"]
Expand Down Expand Up @@ -507,7 +507,7 @@ def test_subscribe_property_with_shard_channels_cluster(r: redis.Redis):


@pytest.mark.min_server('7')
@testtools.run_test_if_redispy_ver('above', '5.0.0')
@testtools.run_test_if_redispy_ver('gte', '5.0.0')
def test_pubsub_shardnumsub(r: redis.Redis):
channels = {b"foo", b"bar", b"baz"}
p1 = r.pubsub()
Expand All @@ -527,7 +527,7 @@ def test_pubsub_shardnumsub(r: redis.Redis):


@pytest.mark.min_server('7')
@testtools.run_test_if_redispy_ver('above', '5.0.0rc2')
@testtools.run_test_if_redispy_ver('gte', '5.0.0rc2')
def test_pubsub_shardchannels(r: redis.Redis):
p = r.pubsub()
p.ssubscribe("foo", "bar", "baz", "quux")
Expand Down
4 changes: 2 additions & 2 deletions test/test_mixins/test_streams_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ def test_xpending_range_negative(r: redis.Redis):


@pytest.mark.max_server('6.3')
@testtools.run_test_if_redispy_ver('above', '4.4')
@testtools.run_test_if_redispy_ver('gte', '4.4')
def test_xautoclaim_redis6(r: redis.Redis):
stream, group, consumer1, consumer2 = "stream", "group", "consumer1", "consumer2"

Expand Down Expand Up @@ -666,7 +666,7 @@ def test_xautoclaim_redis6(r: redis.Redis):


@pytest.mark.min_server('7')
@testtools.run_test_if_redispy_ver('above', '4.4')
@testtools.run_test_if_redispy_ver('gte', '4.4')
def test_xautoclaim_redis7(r: redis.Redis):
stream, group, consumer1, consumer2 = "stream", "group", "consumer1", "consumer2"

Expand Down
13 changes: 11 additions & 2 deletions test/test_redis_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,22 @@ async def test_failed_script_error7(self, req_aioredis2):


@fake_only
async def test_repr(req_aioredis2: redis.asyncio.Redis):
@testtools.run_test_if_redispy_ver('lte', '5.0.9')
async def test_repr_redis_until_51(req_aioredis2: redis.asyncio.Redis):
assert re.fullmatch(
r'ConnectionPool<FakeConnection<server=<fakeredis._server.FakeServer object at .*>,db=0>>',
repr(req_aioredis2.connection_pool)
)


@testtools.run_test_if_redispy_ver('gte', '5.1.0')
async def test_repr_redis_51(req_aioredis2: redis.asyncio.Redis):
assert re.fullmatch(
r'<redis.asyncio.connection.ConnectionPool(<fakeredis.aioredis.FakeConnection(server=<fakeredis._server.FakeServer object at .*>,db=0)>)>',
repr(req_aioredis2.connection_pool)
)


@fake_only
@pytest.mark.disconnected
async def test_not_connected(req_aioredis2: redis.asyncio.Redis):
Expand Down Expand Up @@ -332,7 +341,7 @@ async def test_async():
assert x == b"plz"


@testtools.run_test_if_redispy_ver('above', '4.4.0')
@testtools.run_test_if_redispy_ver('gte', '4.4.0')
@pytest.mark.parametrize('nowait', [False, True])
@pytest.mark.fake
async def test_connection_disconnect(nowait):
Expand Down
10 changes: 8 additions & 2 deletions test/testtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ def raw_command(r: redis.Redis, *args):
r.response_callbacks = response_callbacks


ALLOWED_CONDITIONS = {'above', 'below'}
ALLOWED_CONDITIONS = {'eq', 'gte', 'lte', 'lt', 'gt', 'ne'}


def run_test_if_redispy_ver(condition: str, ver: str):
if condition not in ALLOWED_CONDITIONS:
raise ValueError(f'condition {condition} is not in allowed conditions ({ALLOWED_CONDITIONS})')
cond = REDIS_VERSION >= Version(ver) if condition == 'above' else REDIS_VERSION <= Version(ver)
cond = False
cond = cond or condition == 'eq' and REDIS_VERSION == Version(ver)
cond = cond or condition == 'gte' and REDIS_VERSION >= Version(ver)
cond = cond or condition == 'lte' and REDIS_VERSION <= Version(ver)
cond = cond or condition == 'lt' and REDIS_VERSION < Version(ver)
cond = cond or condition == 'gt' and REDIS_VERSION > Version(ver)
cond = cond or condition == 'ne' and REDIS_VERSION != Version(ver)
return pytest.mark.skipif(
not cond,
reason=f"Test is not applicable to redis-py {REDIS_VERSION} ({condition}, {ver})"
Expand Down

0 comments on commit d2cc98e

Please sign in to comment.