Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: add RANDOMKEY command #396

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6487,24 +6487,45 @@ async def test_lolwut(self, redis_client: TGlideClient):
result = await redis_client.lolwut(2, [10, 20], RandomNode())
assert "Redis ver. " in node_result

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("cluster_mode", [True])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_random_key(self, redis_client: TGlideClient):
async def test_cluster_client_random_key(self, redis_client: TGlideClient):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async def test_cluster_client_random_key(self, redis_client: TGlideClient):
async def test_cluster_client_random_key(self, redis_client: GlideClusterClient):

And you can remove assert for client type then

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, will address all of these when I push to upstream

key = get_random_string(10)

# set a key in the database in case it was empty
# setup: delete all keys
assert isinstance(redis_client, GlideClusterClient)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert isinstance(redis_client, GlideClusterClient)

assert await redis_client.flushall(FlushMode.SYNC)

# no keys exists, so random_key returns None
assert await redis_client.random_key() is None

assert await redis_client.set(key, "foo") == OK
# `key` should be the only existing key, so random_key should return `key`
assert await redis_client.random_key() == key
assert await redis_client.random_key(AllPrimaries()) == key

result = await redis_client.random_key()
assert result is not None
assert await redis_client.exists([result]) == 1
@pytest.mark.parametrize("cluster_mode", [False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_standalone_client_random_key(self, redis_client: TGlideClient):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same in this test

key = get_random_string(10)

if isinstance(redis_client, GlideClusterClient):
result = await redis_client.random_key(AllPrimaries())
assert result is not None
assert await redis_client.exists([result]) == 1
assert isinstance(redis_client, GlideClient)
# setup: delete all keys in DB 0 and DB 1
assert await redis_client.select(0) == OK
assert await redis_client.flushdb(FlushMode.SYNC) == OK
assert await redis_client.select(1) == OK
assert await redis_client.flushdb(FlushMode.SYNC) == OK

assert await redis_client.flushall(FlushMode.SYNC)
# no keys exist so random_key returns None
assert await redis_client.random_key() is None
# set `key` in DB 1
assert await redis_client.set(key, "foo") == OK
# `key` should be the only key in the database
assert await redis_client.random_key() == key

# switch back to DB 0
assert await redis_client.select(0) == OK
# DB 0 should still have no keys, so random_key should still return None
assert await redis_client.random_key() is None


Expand Down
Loading