From 02514f7e538a2ef2434034d6c87fd7d35a466e46 Mon Sep 17 00:00:00 2001 From: Binbin Date: Fri, 14 Jun 2024 14:08:02 +0800 Subject: [PATCH] Add missing asserts in json tests (#3261) Fix some some statements with missing asserts. --- tests/test_asyncio/test_json.py | 23 +++++++++--------- tests/test_json.py | 43 +++++++++++++++++---------------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/tests/test_asyncio/test_json.py b/tests/test_asyncio/test_json.py index c713f1aca2..cc98a55323 100644 --- a/tests/test_asyncio/test_json.py +++ b/tests/test_asyncio/test_json.py @@ -103,7 +103,7 @@ async def test_jsonsetexistentialmodifiersshouldsucceed(decoded_r: redis.Redis): assert await decoded_r.json().set("obj", Path("foo"), "baz", xx=True) assert await decoded_r.json().set("obj", Path("qaz"), "baz", nx=True) - # Test that flags are mutually exlusive + # Test that flags are mutually exclusive with pytest.raises(Exception): await decoded_r.json().set("obj", Path("foo"), "baz", nx=True, xx=True) @@ -483,7 +483,7 @@ async def test_json_mget_dollar(decoded_r: redis.Redis): ) # Test mget with single path - await decoded_r.json().mget("doc1", "$..a") == [1, 3, None] + assert await decoded_r.json().mget(["doc1"], "$..a") == [[1, 3, None]] # Test mget with multi path res = await decoded_r.json().mget(["doc1", "doc2"], "$..a") assert res == [[1, 3, None], [4, 6, [None]]] @@ -539,7 +539,7 @@ async def test_numby_commands_dollar(decoded_r: redis.Redis): await decoded_r.json().set( "doc1", "$", {"a": "b", "b": [{"a": 2}, {"a": 5.0}, {"a": "c"}]} ) - await decoded_r.json().numincrby("doc1", ".b[0].a", 3) == 5 + assert await decoded_r.json().numincrby("doc1", ".b[0].a", 3) == 5 # Test legacy NUMMULTBY await decoded_r.json().set( @@ -547,7 +547,7 @@ async def test_numby_commands_dollar(decoded_r: redis.Redis): ) with pytest.deprecated_call(): - await decoded_r.json().nummultby("doc1", ".b[0].a", 3) == 6 + assert await decoded_r.json().nummultby("doc1", ".b[0].a", 3) == 6 @pytest.mark.redismod @@ -556,13 +556,13 @@ async def test_strappend_dollar(decoded_r: redis.Redis): "doc1", "$", {"a": "foo", "nested1": {"a": "hello"}, "nested2": {"a": 31}} ) # Test multi - await decoded_r.json().strappend("doc1", "bar", "$..a") == [6, 8, None] + assert await decoded_r.json().strappend("doc1", "bar", "$..a") == [6, 8, None] res = [{"a": "foobar", "nested1": {"a": "hellobar"}, "nested2": {"a": 31}}] assert_resp_response(decoded_r, await decoded_r.json().get("doc1", "$"), res, [res]) # Test single - await decoded_r.json().strappend("doc1", "baz", "$.nested1.a") == [11] + assert await decoded_r.json().strappend("doc1", "baz", "$.nested1.a") == [11] res = [{"a": "foobar", "nested1": {"a": "hellobarbaz"}, "nested2": {"a": 31}}] assert_resp_response(decoded_r, await decoded_r.json().get("doc1", "$"), res, [res]) @@ -572,7 +572,7 @@ async def test_strappend_dollar(decoded_r: redis.Redis): await decoded_r.json().strappend("non_existing_doc", "$..a", "err") # Test multi - await decoded_r.json().strappend("doc1", "bar", ".*.a") == 8 + assert await decoded_r.json().strappend("doc1", "bar", ".*.a") == 14 res = [{"a": "foobar", "nested1": {"a": "hellobarbazbar"}, "nested2": {"a": 31}}] assert_resp_response(decoded_r, await decoded_r.json().get("doc1", "$"), res, [res]) @@ -594,8 +594,8 @@ async def test_strlen_dollar(decoded_r: redis.Redis): assert res1 == res2 # Test single - await decoded_r.json().strlen("doc1", "$.nested1.a") == [8] - await decoded_r.json().strlen("doc1", "$.nested2.a") == [None] + assert await decoded_r.json().strlen("doc1", "$.nested1.a") == [8] + assert await decoded_r.json().strlen("doc1", "$.nested2.a") == [None] # Test missing key with pytest.raises(exceptions.ResponseError): @@ -614,7 +614,8 @@ async def test_arrappend_dollar(decoded_r: redis.Redis): }, ) # Test multi - await decoded_r.json().arrappend("doc1", "$..a", "bar", "racuda") == [3, 5, None] + res = [3, 5, None] + assert await decoded_r.json().arrappend("doc1", "$..a", "bar", "racuda") == res res = [ { "a": ["foo", "bar", "racuda"], @@ -794,7 +795,7 @@ async def test_arrpop_dollar(decoded_r: redis.Redis): }, ) # Test multi (all paths are updated, but return result of last path) - await decoded_r.json().arrpop("doc1", "..a", "1") is None + assert await decoded_r.json().arrpop("doc1", "..a", "1") == "null" res = [{"a": [], "nested1": {"a": ["hello", "world"]}, "nested2": {"a": 31}}] assert_resp_response(decoded_r, await decoded_r.json().get("doc1", "$"), res, [res]) diff --git a/tests/test_json.py b/tests/test_json.py index 5cf9b11e17..9996c898d4 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -104,7 +104,7 @@ def test_jsonsetexistentialmodifiersshouldsucceed(client): assert client.json().set("obj", Path("foo"), "baz", xx=True) assert client.json().set("obj", Path("qaz"), "baz", nx=True) - # Test that flags are mutually exlusive + # Test that flags are mutually exclusive with pytest.raises(Exception): client.json().set("obj", Path("foo"), "baz", nx=True, xx=True) @@ -453,6 +453,7 @@ def test_json_forget_with_dollar(client): client.json().forget("not_a_document", "..a") +@pytest.mark.onlynoncluster @pytest.mark.redismod def test_json_mget_dollar(client): # Test mget with multi paths @@ -473,14 +474,14 @@ def test_json_mget_dollar(client): assert_resp_response(client, client.json().get("doc2", "$..a"), res, [res]) # Test mget with single path - client.json().mget("doc1", "$..a") == [1, 3, None] + assert 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]]] + res = [[1, 3, None], [4, 6, [None]]] + assert client.json().mget(["doc1", "doc2"], "$..a") == res # Test missing key - client.json().mget(["doc1", "missing_doc"], "$..a") == [[1, 3, None], None] - res = client.json().mget(["missing_doc1", "missing_doc2"], "$..a") - assert res == [None, None] + assert client.json().mget(["doc1", "missing_doc"], "$..a") == [[1, 3, None], None] + assert client.json().mget(["missing_doc1", "missing_doc2"], "$..a") == [None, None] @pytest.mark.redismod @@ -518,13 +519,13 @@ def test_numby_commands_dollar(client): # Test legacy NUMINCRBY client.json().set("doc1", "$", {"a": "b", "b": [{"a": 2}, {"a": 5.0}, {"a": "c"}]}) - client.json().numincrby("doc1", ".b[0].a", 3) == 5 + assert client.json().numincrby("doc1", ".b[0].a", 3) == 5 # Test legacy NUMMULTBY client.json().set("doc1", "$", {"a": "b", "b": [{"a": 2}, {"a": 5.0}, {"a": "c"}]}) with pytest.deprecated_call(): - client.json().nummultby("doc1", ".b[0].a", 3) == 6 + assert client.json().nummultby("doc1", ".b[0].a", 3) == 6 @pytest.mark.redismod @@ -533,25 +534,25 @@ def test_strappend_dollar(client): "doc1", "$", {"a": "foo", "nested1": {"a": "hello"}, "nested2": {"a": 31}} ) # Test multi - client.json().strappend("doc1", "bar", "$..a") == [6, 8, None] + assert client.json().strappend("doc1", "bar", "$..a") == [6, 8, None] - # res = [{"a": "foobar", "nested1": {"a": "hellobar"}, "nested2": {"a": 31}}] - # assert_resp_response(client, client.json().get("doc1", "$"), res, [res]) + res = [{"a": "foobar", "nested1": {"a": "hellobar"}, "nested2": {"a": 31}}] + assert_resp_response(client, client.json().get("doc1", "$"), res, [res]) # Test single - client.json().strappend("doc1", "baz", "$.nested1.a") == [11] + assert client.json().strappend("doc1", "baz", "$.nested1.a") == [11] - # res = [{"a": "foobar", "nested1": {"a": "hellobarbaz"}, "nested2": {"a": 31}}] - # assert_resp_response(client, client.json().get("doc1", "$"), res, [res]) + res = [{"a": "foobar", "nested1": {"a": "hellobarbaz"}, "nested2": {"a": 31}}] + assert_resp_response(client, client.json().get("doc1", "$"), res, [res]) # Test missing key with pytest.raises(exceptions.ResponseError): client.json().strappend("non_existing_doc", "$..a", "err") # Test multi - client.json().strappend("doc1", "bar", ".*.a") == 8 - # res = [{"a": "foo", "nested1": {"a": "hellobar"}, "nested2": {"a": 31}}] - # assert_resp_response(client, client.json().get("doc1", "$"), res, [res]) + assert client.json().strappend("doc1", "bar", ".*.a") == 14 + res = [{"a": "foobar", "nested1": {"a": "hellobarbazbar"}, "nested2": {"a": 31}}] + assert_resp_response(client, client.json().get("doc1", "$"), res, [res]) # Test missing path with pytest.raises(exceptions.ResponseError): @@ -571,8 +572,8 @@ def test_strlen_dollar(client): assert res1 == res2 # Test single - client.json().strlen("doc1", "$.nested1.a") == [8] - client.json().strlen("doc1", "$.nested2.a") == [None] + assert client.json().strlen("doc1", "$.nested1.a") == [8] + assert client.json().strlen("doc1", "$.nested2.a") == [None] # Test missing key with pytest.raises(exceptions.ResponseError): @@ -591,7 +592,7 @@ def test_arrappend_dollar(client): }, ) # Test multi - client.json().arrappend("doc1", "$..a", "bar", "racuda") == [3, 5, None] + assert client.json().arrappend("doc1", "$..a", "bar", "racuda") == [3, 5, None] res = [ { "a": ["foo", "bar", "racuda"], @@ -775,7 +776,7 @@ def test_arrpop_dollar(client): }, ) # Test multi (all paths are updated, but return result of last path) - client.json().arrpop("doc1", "..a", "1") is None + assert client.json().arrpop("doc1", "..a", "1") == "null" res = [{"a": [], "nested1": {"a": ["hello", "world"]}, "nested2": {"a": 31}}] assert_resp_response(client, client.json().get("doc1", "$"), res, [res])