diff options
author | Chayim I. Kirshen <c@kirshen.com> | 2021-11-02 14:29:59 +0200 |
---|---|---|
committer | Chayim I. Kirshen <c@kirshen.com> | 2021-11-02 14:29:59 +0200 |
commit | 2bf3fe98843a961849c33c4a5ed8af22f959dc61 (patch) | |
tree | 7bdbddc3005db41dcf6c46ca0840b39a0a79214f | |
parent | dca95e2c1df6650d14d70f4d3c3ee1e537982c58 (diff) | |
download | redis-py-2bf3fe98843a961849c33c4a5ed8af22f959dc61.tar.gz |
test fixes
-rw-r--r-- | redis/commands/json/__init__.py | 21 | ||||
-rw-r--r-- | redis/commands/json/decoders.py | 2 | ||||
-rw-r--r-- | tests/test_json.py | 36 |
3 files changed, 29 insertions, 30 deletions
diff --git a/redis/commands/json/__init__.py b/redis/commands/json/__init__.py index 1727383..6bc592f 100644 --- a/redis/commands/json/__init__.py +++ b/redis/commands/json/__init__.py @@ -1,14 +1,11 @@ from json import JSONDecoder, JSONEncoder from .decoders import ( - int_or_list, - int_or_none -) -from .helpers import bulk_of_jsons -from .decoders import ( decode_list_or_int, - decode_toggle + decode_toggle, + int_or_none, ) +from .helpers import bulk_of_jsons from ..helpers import nativestr, delist from .commands import JSONCommands @@ -51,15 +48,15 @@ class JSON(JSONCommands): "JSON.NUMINCRBY": self._decode, "JSON.NUMMULTBY": self._decode, "JSON.TOGGLE": decode_toggle, - "JSON.STRAPPEND": int, + "JSON.STRAPPEND": decode_list_or_int, "JSON.STRLEN": decode_list_or_int, - "JSON.ARRAPPEND": int, - "JSON.ARRINDEX": int, - "JSON.ARRINSERT": 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.ARRTRIM": int, - "JSON.OBJLEN": int, + "JSON.ARRTRIM": decode_list_or_int, + "JSON.OBJLEN": decode_list_or_int, "JSON.OBJKEYS": delist, # "JSON.RESP": delist, "JSON.DEBUG": decode_list_or_int, diff --git a/redis/commands/json/decoders.py b/redis/commands/json/decoders.py index fa85253..4f46f0e 100644 --- a/redis/commands/json/decoders.py +++ b/redis/commands/json/decoders.py @@ -9,6 +9,8 @@ def decode_toggle(b): def decode_list_or_int(b): if isinstance(b, list): return b + if b is None: + return None return int(b) diff --git a/tests/test_json.py b/tests/test_json.py index 31516dc..1a39f06 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -430,11 +430,11 @@ def test_json_mget_dollar(client): # Test mget with single path client.json().mget('doc1', '$..a') == [1,3,None] # Test mget with multi path - client.json().mget('doc1', 'doc2', '$..a') == [[1,3,None], [4,6,[None]]] + client.json().mget(['doc1', 'doc2'], '$..a') == [[1,3,None], [4,6,[None]]] # Test missing key - client.json().mget('doc1', 'missing_doc', '$..a') == [[1,3,None], None] - res = client.json().mget('missing_doc1', 'missing_doc2', '$..a') + client.json().mget(['doc1', 'missing_doc'], '$..a') == [[1,3,None], None] + res = client.json().mget(['missing_doc1', 'missing_doc2'], '$..a') assert res == [None, None] @pytest.mark.redismod @@ -480,25 +480,25 @@ def test_strappend_dollar(client): client.json().set('doc1', '$', {"a":"foo", "nested1": {"a": "hello"}, "nested2": {"a": 31}}) # Test multi - client.json().strappend('doc1', '$..a', '"bar"') == [6, 8, None] + client.json().strappend('doc1', '"bar"', '$..a') == [6, 8, None] client.json().get('doc1', '$') == [{"a":"foobar","nested1":{"a":"hellobar"},"nested2":{"a":31}}] # Test single - client.json().strappend('doc1', '$.nested1.a', '"baz"') == [11] + client.json().strappend('doc1', '"baz"', '$.nested1.a') == [11] client.json().get('doc1', '$') == [{"a":"foobar","nested1":{"a":"hellobarbaz"},"nested2":{"a":31}}] # Test missing key - with pytest.raises(exceptions.DataError): + with pytest.raises(exceptions.ResponseError): client.json().strappend('non_existing_doc', '$..a', '"err"') # Test multi - client.json().strappend('doc1', '.*.a', '"bar"') == 8 - client.json.get('doc1', '$') == [{"a":"foo","nested1":{"a":"hellobar"},"nested2":{"a":31}}] + client.json().strappend('doc1', '"bar"', '.*.a') == 8 + client.json().get('doc1', '$') == [{"a":"foo","nested1":{"a":"hellobar"},"nested2":{"a":31}}] # Test missing path - with pytest.raises(exceptions.DataError): + with pytest.raises(exceptions.ResponseError): client.json().strappend('doc1', '"piu"') @pytest.mark.redismod @@ -508,7 +508,7 @@ def test_strlen_dollar(client): client.json().set('doc1', '$', {"a":"foo", "nested1": {"a": "hello"}, "nested2": {"a": 31}}) assert client.json().strlen('doc1', '$..a') == [3, 5, None] - res2 = client.json().strappend('doc1', '$..a', '"bar"') == [6, 8, None] + res2 = client.json().strappend('doc1', 'bar', '$..a') res1 = client.json().strlen('doc1', '$..a') assert res1 == res2 @@ -672,15 +672,15 @@ def test_arrtrim_dollar(client): [{"a": [], "nested1": {"a": []}, "nested2": {"a": 31}}] # Test missing key - with pytest.raises(exceptions.DataError): - client.json().arrtrim('non_existing_doc', '..a', '0') + with pytest.raises(exceptions.ResponseError): + client.json().arrtrim('non_existing_doc', '..a', '0', 1) # 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().arrtrim('doc1', '..a', '1', '-1') == 2 - res = r.execute_command('JSON.GET', 'doc1', '$') == \ + res = client.json().get('doc1', '$') == \ [{"a": [], "nested1": {"a": [None, "world"]}, "nested2": {"a": 31}}] # Test single assert client.json().arrtrim('doc1', '.nested1.a', '1', '1') == 1 @@ -688,7 +688,7 @@ def test_arrtrim_dollar(client): [{"a": [], "nested1": {"a": ["world"]}, "nested2": {"a": 31}}] # Test missing key - with pytest.raises(exceptions.DataError): + with pytest.raises(exceptions.ResponseError): client.json().arrtrim('non_existing_doc', '..a') @pytest.mark.redismod @@ -725,7 +725,7 @@ def test_objlen_dollar(client): assert client.json().objlen('non_existing_doc', '$..a') is None # Test missing path - with pytest.raises(exceptions.DataError): + with pytest.raises(exceptions.ResponseError): client.json().objlen('doc1', '$.nowhere') @@ -736,10 +736,10 @@ def test_objlen_dollar(client): assert client.json().objlen('doc1', '.nested2.a') == 1 # Test missing key - assert client.json.objlen('non_existing_doc', '..a') is None + assert client.json().objlen('non_existing_doc', '..a') is None # Test missing path - with pytest.raises(exceptions.DataError): + with pytest.raises(exceptions.ResponseError): client.json().objlen('doc1', '.nowhere') @pytest.mark.redismod @@ -801,7 +801,7 @@ def test_clear_dollar(client): assert client.json().get('doc1', '$') == [{}] # Test missing key - with pytest.raises(exceptions.DataError): + with pytest.raises(exceptions.ResponseError): client.json().clear('non_existing_doc', '$..a') @pytest.mark.redismod |