From c46a09b7f174437351909e67fa3cfce1d4dd3f3b Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Tue, 20 Jul 2021 21:16:00 +0300 Subject: [PATCH 1/6] hrandfield --- redis/client.py | 20 ++++++++++++++++++++ tests/test_commands.py | 15 +++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/redis/client.py b/redis/client.py index 72178f3068..d4438e9709 100755 --- a/redis/client.py +++ b/redis/client.py @@ -1820,6 +1820,26 @@ def pttl(self, name): "Returns the number of milliseconds until the key ``name`` will expire" return self.execute_command('PTTL', name) + def hrandfield(self, key, count=None, withvalues=False): + """ + Return a random field from the hash value stored at key. + + count: if the argument is positive, return an array of distinct fields. + If called with a negative count, the behavior changes and the command is + allowed to return the same field multiple times. In this case, the number + of returned fields is the absolute value of the specified count. + withvalues: The optional WITHVALUES modifier changes the reply so it + includes the respective values of the randomly selected hash fields. + """ + params = [] + if count is not None: + params.append(count) + if withvalues: + params.append("WITHVALUES") + + return self.execute_command("HRANDFIELD", key, *params) + + def randomkey(self): "Returns the name of a random key" return self.execute_command('RANDOMKEY') diff --git a/tests/test_commands.py b/tests/test_commands.py index a5a7e45e2f..48673553b0 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -874,6 +874,21 @@ def test_pttl_no_key(self, r): "PTTL on servers 2.8 and after return -2 when the key doesn't exist" assert r.pttl('a') == -2 + def test_hrandfield(self, r): + assert r.hrandfield('key') is None + for val in ('hello', 'I', 'am', 'a', 'test'): + r.hset('key', val.upper(), val) + assert r.hrandfield('key') is not None + assert len(r.hrandfield('key', 2)) == 2 + + # with values + rand_fields_values = r.hrandfield('key', 2, True) + assert len(rand_fields_values) == 4 + assert rand_fields_values[0] == rand_fields_values[1].upper() + + assert len(r.hrandfield('key', 10)) == 5 # without duplications + assert len(r.hrandfield('key', -10)) == 10 # with duplications + def test_randomkey(self, r): assert r.randomkey() is None for key in ('a', 'b', 'c'): From dcd056d0a19fb1835f00c16533dd4b8226288e20 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Tue, 20 Jul 2021 22:45:40 +0300 Subject: [PATCH 2/6] use mapping in hset --- tests/test_commands.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 48673553b0..7383d182b3 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -876,18 +876,13 @@ def test_pttl_no_key(self, r): def test_hrandfield(self, r): assert r.hrandfield('key') is None - for val in ('hello', 'I', 'am', 'a', 'test'): - r.hset('key', val.upper(), val) + r.hset('key', mapping={'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}) assert r.hrandfield('key') is not None - assert len(r.hrandfield('key', 2)) == 2 - - # with values - rand_fields_values = r.hrandfield('key', 2, True) - assert len(rand_fields_values) == 4 - assert rand_fields_values[0] == rand_fields_values[1].upper() - assert len(r.hrandfield('key', 10)) == 5 # without duplications - assert len(r.hrandfield('key', -10)) == 10 # with duplications + assert len(r.hrandfield('key', 2)) == 2 + assert len(r.hrandfield('key', 2, True)) == 4 # with values + assert len(r.hrandfield('key', 10)) == 5 # without duplications + assert len(r.hrandfield('key', -10)) == 10 # with duplications def test_randomkey(self, r): assert r.randomkey() is None From 69cc1d4f15f62f7b9af82a89bcf742b49f9c7b67 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Wed, 21 Jul 2021 10:26:13 +0300 Subject: [PATCH 3/6] skip if version not fit --- tests/test_commands.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_commands.py b/tests/test_commands.py index 7383d182b3..5f7222c3f7 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -874,6 +874,7 @@ def test_pttl_no_key(self, r): "PTTL on servers 2.8 and after return -2 when the key doesn't exist" assert r.pttl('a') == -2 + @skip_if_server_version_lt('6.2.0') def test_hrandfield(self, r): assert r.hrandfield('key') is None r.hset('key', mapping={'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}) From 43cc8c6b7ac63888dede2aee2e12efccd50f0c14 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Wed, 21 Jul 2021 10:39:42 +0300 Subject: [PATCH 4/6] remove empty line --- tests/test_commands.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 5f7222c3f7..66086e02a2 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -879,7 +879,6 @@ def test_hrandfield(self, r): assert r.hrandfield('key') is None r.hset('key', mapping={'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}) assert r.hrandfield('key') is not None - assert len(r.hrandfield('key', 2)) == 2 assert len(r.hrandfield('key', 2, True)) == 4 # with values assert len(r.hrandfield('key', 10)) == 5 # without duplications From e894f744c3e70268cb8c141f50fdabcb353da55a Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Wed, 21 Jul 2021 13:23:25 +0300 Subject: [PATCH 5/6] flake8 comments --- redis/client.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/redis/client.py b/redis/client.py index d4438e9709..c30a36d389 100755 --- a/redis/client.py +++ b/redis/client.py @@ -1825,9 +1825,10 @@ def hrandfield(self, key, count=None, withvalues=False): Return a random field from the hash value stored at key. count: if the argument is positive, return an array of distinct fields. - If called with a negative count, the behavior changes and the command is - allowed to return the same field multiple times. In this case, the number - of returned fields is the absolute value of the specified count. + If called with a negative count, the behavior changes and the command + is allowed to return the same field multiple times. In this case, + the number of returned fields is the absolute value of the + specified count. withvalues: The optional WITHVALUES modifier changes the reply so it includes the respective values of the randomly selected hash fields. """ @@ -1839,7 +1840,6 @@ def hrandfield(self, key, count=None, withvalues=False): return self.execute_command("HRANDFIELD", key, *params) - def randomkey(self): "Returns the name of a random key" return self.execute_command('RANDOMKEY') From 104358cb8778ce95fae2fe1714cc0014c35ffeba Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Thu, 22 Jul 2021 10:10:09 +0300 Subject: [PATCH 6/6] new line for each comment --- tests/test_commands.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 66086e02a2..9a81e07b44 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -880,9 +880,12 @@ def test_hrandfield(self, r): r.hset('key', mapping={'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}) assert r.hrandfield('key') is not None assert len(r.hrandfield('key', 2)) == 2 - assert len(r.hrandfield('key', 2, True)) == 4 # with values - assert len(r.hrandfield('key', 10)) == 5 # without duplications - assert len(r.hrandfield('key', -10)) == 10 # with duplications + # with values + assert len(r.hrandfield('key', 2, True)) == 4 + # without duplications + assert len(r.hrandfield('key', 10)) == 5 + # with duplications + assert len(r.hrandfield('key', -10)) == 10 def test_randomkey(self, r): assert r.randomkey() is None