From cfb13d4629219275e97d70a7d4da9e897f17d98b Mon Sep 17 00:00:00 2001 From: malinaa96 Date: Tue, 8 Jun 2021 22:44:07 +0200 Subject: [PATCH 1/4] Add support for COPY command --- redis/client.py | 8 +++++++- tests/test_commands.py | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/redis/client.py b/redis/client.py index 59575cd835..b65ee964b5 100755 --- a/redis/client.py +++ b/redis/client.py @@ -561,7 +561,7 @@ class Redis: """ RESPONSE_CALLBACKS = { **string_keys_to_dict( - 'AUTH EXPIRE EXPIREAT HEXISTS HMSET MOVE MSETNX PERSIST ' + 'AUTH COPY EXPIRE EXPIREAT HEXISTS HMSET MOVE MSETNX PERSIST ' 'PSETEX RENAMENX SISMEMBER SMOVE SETEX SETNX', bool ), @@ -1612,6 +1612,12 @@ def bitpos(self, key, bit, start=None, end=None): "when end is specified") return self.execute_command('BITPOS', *params) + def copy(self, source, destination): + """ + Copy the value stored in the ``source`` key to the ``destination`` key. + """ + return self.execute_command('COPY', source, destination) + def decr(self, name, amount=1): """ Decrements the value of ``key`` by ``amount``. If no key exists, diff --git a/tests/test_commands.py b/tests/test_commands.py index d1f85b7306..940d9b0e3f 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -579,6 +579,14 @@ def test_bitpos_wrong_arguments(self, r): with pytest.raises(exceptions.RedisError): r.bitpos(key, 7) == 12 + @skip_if_server_version_lt('6.2.0') + def test_copy(self, r): + assert r.copy("a", "b") == 0 + r.set("a", "foo") + assert r.copy("a", "b") == 1 + assert r.get("a") == b"foo" + assert r.get("b") == b"foo" + def test_decr(self, r): assert r.decr('a') == -1 assert r['a'] == b'-1' From d9397b37d425aa2b22585ad1d50d72355a044fe4 Mon Sep 17 00:00:00 2001 From: malinaa96 Date: Sat, 12 Jun 2021 12:51:11 +0200 Subject: [PATCH 2/4] Add db and replace options to copy command --- redis/client.py | 16 ++++++++++++++-- tests/test_commands.py | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/redis/client.py b/redis/client.py index b65ee964b5..c1f194ba3f 100755 --- a/redis/client.py +++ b/redis/client.py @@ -1612,11 +1612,23 @@ def bitpos(self, key, bit, start=None, end=None): "when end is specified") return self.execute_command('BITPOS', *params) - def copy(self, source, destination): + def copy(self, source, destination, destination_db=None, replace=False): """ Copy the value stored in the ``source`` key to the ``destination`` key. + + ``destination_db`` an alternative destination database. By default, + the ``destination`` key is created in the source Redis database. + + ``replace`` whether the ``destination`` key should be removed before + copying the value to it. By default, the value is not copied if + the ``destination`` key already exists. """ - return self.execute_command('COPY', source, destination) + params = [source, destination] + if destination_db: + params.extend(["DB", destination_db]) + if replace: + params.append("REPLACE") + return self.execute_command('COPY', *params) def decr(self, name, amount=1): """ diff --git a/tests/test_commands.py b/tests/test_commands.py index 940d9b0e3f..1cbbfb05d9 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -587,6 +587,21 @@ def test_copy(self, r): assert r.get("a") == b"foo" assert r.get("b") == b"foo" + @skip_if_server_version_lt('6.2.0') + def test_copy_and_replace(self, r): + r.set("a", "foo1") + r.set("b", "foo2") + assert r.copy("a", "b") == 0 + assert r.copy("a", "b", replace=True) == 1 + + @skip_if_server_version_lt('6.2.0') + def test_copy_to_another_database(self, request): + r0 = _get_client(redis.Redis, request, db=0) + r1 = _get_client(redis.Redis, request, db=1) + r0.set("a", "foo") + assert r0.copy("a", "b", destination_db=1) == 1 + assert r1.get("b") == b"foo" + def test_decr(self, r): assert r.decr('a') == -1 assert r['a'] == b'-1' From c66372b9b2ecf08f51afc20aba38b5e4f8c02b5a Mon Sep 17 00:00:00 2001 From: malinaa96 Date: Thu, 15 Jul 2021 22:28:10 +0200 Subject: [PATCH 3/4] Adjust if condition and remove trailing spaces --- redis/client.py | 4 ++-- tests/test_commands.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/redis/client.py b/redis/client.py index c1f194ba3f..5eaf168885 100755 --- a/redis/client.py +++ b/redis/client.py @@ -1616,7 +1616,7 @@ def copy(self, source, destination, destination_db=None, replace=False): """ Copy the value stored in the ``source`` key to the ``destination`` key. - ``destination_db`` an alternative destination database. By default, + ``destination_db`` an alternative destination database. By default, the ``destination`` key is created in the source Redis database. ``replace`` whether the ``destination`` key should be removed before @@ -1624,7 +1624,7 @@ def copy(self, source, destination, destination_db=None, replace=False): the ``destination`` key already exists. """ params = [source, destination] - if destination_db: + if destination_db is None: params.extend(["DB", destination_db]) if replace: params.append("REPLACE") diff --git a/tests/test_commands.py b/tests/test_commands.py index 98f6842c6a..a5a7e45e2f 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -590,7 +590,7 @@ def test_copy(self, r): def test_copy_and_replace(self, r): r.set("a", "foo1") r.set("b", "foo2") - assert r.copy("a", "b") == 0 + assert r.copy("a", "b") == 0 assert r.copy("a", "b", replace=True) == 1 @skip_if_server_version_lt('6.2.0') From 435759aa008c12c379130636561ec854ad961390 Mon Sep 17 00:00:00 2001 From: malinaa96 Date: Thu, 15 Jul 2021 22:56:09 +0200 Subject: [PATCH 4/4] Fix if condition --- redis/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redis/client.py b/redis/client.py index 5eaf168885..72178f3068 100755 --- a/redis/client.py +++ b/redis/client.py @@ -1624,7 +1624,7 @@ def copy(self, source, destination, destination_db=None, replace=False): the ``destination`` key already exists. """ params = [source, destination] - if destination_db is None: + if destination_db is not None: params.extend(["DB", destination_db]) if replace: params.append("REPLACE")