diff options
author | Chayim I. Kirshen <c@kirshen.com> | 2021-11-02 14:59:08 +0200 |
---|---|---|
committer | Chayim I. Kirshen <c@kirshen.com> | 2021-11-02 14:59:08 +0200 |
commit | e4d61be9fdc95f2c6a2a60f0c3c0ecd538027200 (patch) | |
tree | ce412d1367e5f46f1a54a96348d3f2be08ed57d8 | |
parent | 235f0a45df3b61e5f77539413d463f36db894727 (diff) | |
download | redis-py-e4d61be9fdc95f2c6a2a60f0c3c0ecd538027200.tar.gz |
reduced decoders to one
-rw-r--r-- | redis/commands/json/__init__.py | 8 | ||||
-rw-r--r-- | redis/commands/json/commands.py | 2 | ||||
-rw-r--r-- | redis/commands/json/decoders.py | 19 | ||||
-rw-r--r-- | tests/test_json.py | 71 |
4 files changed, 34 insertions, 66 deletions
diff --git a/redis/commands/json/__init__.py b/redis/commands/json/__init__.py index 6bc592f..7303e5d 100644 --- a/redis/commands/json/__init__.py +++ b/redis/commands/json/__init__.py @@ -2,8 +2,6 @@ from json import JSONDecoder, JSONEncoder from .decoders import ( decode_list_or_int, - decode_toggle, - int_or_none, ) from .helpers import bulk_of_jsons from ..helpers import nativestr, delist @@ -47,14 +45,14 @@ class JSON(JSONCommands): "JSON.SET": lambda r: r and nativestr(r) == "OK", "JSON.NUMINCRBY": self._decode, "JSON.NUMMULTBY": self._decode, - "JSON.TOGGLE": decode_toggle, + "JSON.TOGGLE": decode_list_or_int, "JSON.STRAPPEND": decode_list_or_int, "JSON.STRLEN": decode_list_or_int, "JSON.ARRAPPEND": decode_list_or_int, "JSON.ARRINDEX": decode_list_or_int, "JSON.ARRINSERT": decode_list_or_int, - "JSON.ARRLEN": int_or_none, - "JSON.ARRPOP": self._decode, + "JSON.ARRLEN": decode_list_or_int, + "JSON.ARRPOP": decode_list_or_int, "JSON.ARRTRIM": decode_list_or_int, "JSON.OBJLEN": decode_list_or_int, "JSON.OBJKEYS": delist, diff --git a/redis/commands/json/commands.py b/redis/commands/json/commands.py index fb00e22..a601f5f 100644 --- a/redis/commands/json/commands.py +++ b/redis/commands/json/commands.py @@ -192,7 +192,7 @@ class JSONCommands: the key name, the path is determined to be the first. If a single option is passed, then the rootpath (i.e Path.rootPath()) is used. """ - pieces = [name, str(path), value] + pieces = [name, str(path), self._encode(value)] return self.execute_command( "JSON.STRAPPEND", *pieces ) diff --git a/redis/commands/json/decoders.py b/redis/commands/json/decoders.py index 4f46f0e..5dcf50c 100644 --- a/redis/commands/json/decoders.py +++ b/redis/commands/json/decoders.py @@ -1,21 +1,10 @@ -from ..helpers import delist - - -def decode_toggle(b): - if isinstance(b, list): - return b - return b == b"true" - def decode_list_or_int(b): if isinstance(b, list): return b if b is None: return None + elif b == b"true": + return True + elif b == b"false": + return False return int(b) - - -def int_or_none(b): - if b is None: - return None - if isinstance(b, int): - return b diff --git a/tests/test_json.py b/tests/test_json.py index 5cea4ab..ae6764d 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -129,10 +129,7 @@ def test_toggle(client): @pytest.mark.redismod def test_strappend(client): client.json().set("jsonkey", Path.rootPath(), 'foo') - import json - assert 6 == client.json().strappend("jsonkey", json.dumps('bar')) - with pytest.raises(redis.exceptions.ResponseError): - assert 6 == client.json().strappend("jsonkey", 'bar') + assert 6 == client.json().strappend("jsonkey", 'bar') assert "foobar" == client.json().get("jsonkey", Path.rootPath()) @@ -150,8 +147,7 @@ def test_debug(client): def test_strlen(client): client.json().set("str", Path.rootPath(), "foo") assert 3 == client.json().strlen("str", Path.rootPath()) - import json - client.json().strappend("str", json.dumps("bar"), Path.rootPath()) + client.json().strappend("str", "bar", Path.rootPath()) assert 6 == client.json().strlen("str", Path.rootPath()) assert 6 == client.json().strlen("str") @@ -524,66 +520,52 @@ def test_strlen_dollar(client): def test_arrappend_dollar(client): client.json().set('doc1', '$', {"a":["foo"], "nested1": {"a": ["hello", None, "world"]}, "nested2": {"a": 31}}) # Test multi - client.json().arrappend('doc1', '$..a', '"bar"', '"racuda"') == [3, 5, None] + client.json().arrappend('doc1', '$..a', 'bar', 'racuda') == [3, 5, None] assert client.json().get('doc1', '$') == \ [{"a": ["foo", "bar", "racuda"], "nested1": {"a": ["hello", None, "world", "bar", "racuda"]}, "nested2": {"a": 31}}] # Test single - assert client.json().arrappend('doc1', '$.nested1.a', '"baz"') == [6] + assert client.json().arrappend('doc1', '$.nested1.a', 'baz') == [6] assert client.json().get('doc1', '$') == \ [{"a": ["foo", "bar", "racuda"], "nested1": {"a": ["hello", None, "world", "bar", "racuda", "baz"]}, "nested2": {"a": 31}}] # Test missing key - with pytest.raises(exceptions.DataError): - client.json.arrappend('non_existing_doc', '$..a') + with pytest.raises(exceptions.ResponseError): + client.json().arrappend('non_existing_doc', '$..a') # Test legacy client.json().set('doc1', '$', {"a":["foo"], "nested1": {"a": ["hello", None, "world"]}, "nested2": {"a": 31}}) # Test multi (all paths are updated, but return result of last path) - assert client.json().arrappend('doc1', '..a', '"bar"', '"racuda"') == 5 + assert client.json().arrappend('doc1', '..a', 'bar', 'racuda') == 5 - assert client.json.get('doc1', '$') == \ + assert client.json().get('doc1', '$') == \ [{"a": ["foo", "bar", "racuda"], "nested1": {"a": ["hello", None, "world", "bar", "racuda"]}, "nested2": {"a": 31}}] # Test single - assert client.json().arrappend('doc1', '.nested1.a', '"baz"') == 6 + assert client.json().arrappend('doc1', '.nested1.a', 'baz') == 6 assert client.json().get('doc1', '$') == \ [{"a": ["foo", "bar", "racuda"], "nested1": {"a": ["hello", None, "world", "bar", "racuda", "baz"]}, "nested2": {"a": 31}}] # Test missing key - with pytest.raises(exceptions.DataError): - client.json.arrappend('non_existing_doc', '$..a') + with pytest.raises(exceptions.ResponseError): + client.json().arrappend('non_existing_doc', '$..a') @pytest.mark.redismod def test_arrinsert_dollar(client): client.json().set('doc1', '$', {"a":["foo"], "nested1": {"a": ["hello", None, "world"]}, "nested2": {"a": 31}}) # Test multi - assert client.json().arrinsert('doc1', '$..a', '1', '"bar"', '"racuda"') == [3, 5, None] + assert client.json().arrinsert('doc1', '$..a', '1', 'bar', 'racuda') == [3, 5, None] assert client.json().get('doc1', '$') == \ [{"a": ["foo", "bar", "racuda"], "nested1": {"a": ["hello", "bar", "racuda", None, "world"]}, "nested2": {"a": 31}}] # Test single - assert client.json().arrinsert('doc1', '$.nested1.a', -2, '"baz"') == [6] + assert client.json().arrinsert('doc1', '$.nested1.a', -2, 'baz') == [6] assert client.json().get('doc1', '$') == \ [{"a": ["foo", "bar", "racuda"], "nested1": {"a": ["hello", "bar", "racuda", "baz", None, "world"]}, "nested2": {"a": 31}}] # Test missing key - with pytest.raises(exceptions.DataError): - client.json.arrappend('non_existing_doc', '$..a') - - # Test legacy - client.json().set('doc1', '$', {"a":["foo"], "nested1": {"a": ["hello", None, "world"]}, "nested2": {"a": 31}}) - assert client.json().arrappend('doc1', '..a', '1', '"bar"', '"racuda"') == 5 - - assert client.json().get('doc1', '$') == \ - [{"a": ["foo", "bar", "racuda"], "nested1": {"a": ["hello", "bar", "racuda", None, "world"]}, "nested2": {"a": 31}}] - # Test single - assert client.json().arrinsert('doc1', '.nested1.a', -2, '"baz"') == 6 - assert client.json().get('doc1', '$') == \ - [{"a": ["foo", "bar", "racuda"], "nested1": {"a": ["hello", "bar", "racuda", "baz", None, "world"]}, "nested2": {"a": 31}}] + with pytest.raises(exceptions.ResponseError): + client.json().arrappend('non_existing_doc', '$..a') - # Test missing key - with pytest.raises(exceptions.DataError): - client.json.arrinsert('non_existing_doc', '$..a') @pytest.mark.redismod def test_arrlen_dollar(client): @@ -596,15 +578,14 @@ def test_arrlen_dollar(client): [4, 6, None] client.json().clear('doc1', '$.a') - assert client.json.arrlen('doc1', '$..a') == [0, 6, None] + assert client.json().arrlen('doc1', '$..a') == [0, 6, None] # Test single assert client.json().arrlen('doc1', '$.nested1.a') == [6] # Test missing key - with pytest.raises(exceptions.DataError): - client.json.arrappend('non_existing_doc', '$..a') + with pytest.raises(exceptions.ResponseError): + client.json().arrappend('non_existing_doc', '$..a') - # Test legacy client.json().set('doc1', '$', {"a":["foo"], "nested1": {"a": ["hello", None, "world"]}, "nested2": {"a": 31}}) # Test multi (return result of last path) assert client.json().arrlen('doc1', '$..a') == [1, 3, None] @@ -620,17 +601,17 @@ def test_arrlen_dollar(client): def test_arrpop_dollar(client): client.json().set('doc1', '$', {"a":["foo"], "nested1": {"a": ["hello", None, "world"]}, "nested2": {"a": 31}}) # Test multi - assert client.json().arrpop('doc1', '$..a', '1') == ['"foo"', 'None', None] + assert client.json().arrpop('doc1', '$..a', '1') == ['foo', None, None] assert client.json().get('doc1', '$') == \ [{"a": [], "nested1": {"a": ["hello", "world"]}, "nested2": {"a": 31}}] - assert client.json().arrpop('doc1', '$..a', '-1') == [None, '"world"', None] + assert client.json().arrpop('doc1', '$..a', '-1') == [None, 'world', None] assert client.json().get('doc1', '$') == \ [{"a": [], "nested1": {"a": ["hello"]}, "nested2": {"a": 31}}] # Test single - assert client.json().arrpop('doc1', '$.nested1.a', -2) == ['"hello"'] + assert client.json().arrpop('doc1', '$.nested1.a', -2) == ['hello'] assert client.json().get('doc1', '$') == \ [{"a": [], "nested1": {"a": []}, "nested2": {"a": 31}}] @@ -829,20 +810,20 @@ def test_debug_dollar(client): client.json().set('doc1', '$', jdata) # Test multi - assert client.json().debug('doc1', '$..a') == \ + assert client.json().debug("MEMORY", 'doc1', '$..a') == \ [72, 24, 24, 16, 16, 1, 0] # Test single - assert client.json().debug('doc1', '$.nested2.a') == [24] + assert client.json().debug("MEMORY", 'doc1', '$.nested2.a') == [24] # Test legacy - assert client.json().debug('doc1', '..a') == 72 + assert client.json().debug("MEMORY", 'doc1', '..a') == 72 # Test missing path (defaults to root) - assert client.json().debug('doc1') == 72 + assert client.json().debug("MEMORY", 'doc1') == 72 # Test missing key - assert client.json().debug('non_existing_doc', '$..a') == [] + assert client.json().debug("MEMORY",'non_existing_doc', '$..a') == [] def test_resp_dollar(client): |