diff options
author | Chayim <chayim@users.noreply.github.com> | 2022-02-16 10:26:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-16 10:26:31 +0200 |
commit | 0ed06603695ba9533d1086dcd7d60cd5eb5e17d0 (patch) | |
tree | 530e96469a396c221e28ac6a8b369984d01bd66c /tests | |
parent | 6c00e091e93d07834fcdd811b2a8473848310db0 (diff) | |
download | redis-py-4.1.tar.gz |
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_graph.py | 127 | ||||
-rw-r--r-- | tests/test_search.py | 55 |
2 files changed, 166 insertions, 16 deletions
diff --git a/tests/test_graph.py b/tests/test_graph.py index c6dc9a4..c885aa4 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -1,6 +1,7 @@ import pytest from redis.commands.graph import Edge, Node, Path +from redis.commands.graph.execution_plan import Operation from redis.exceptions import ResponseError @@ -260,21 +261,6 @@ def test_cached_execution(client): @pytest.mark.redismod -def test_explain(client): - create_query = """CREATE (:Rider {name:'Valentino Rossi'})-[:rides]->(:Team {name:'Yamaha'}), - (:Rider {name:'Dani Pedrosa'})-[:rides]->(:Team {name:'Honda'}), - (:Rider {name:'Andrea Dovizioso'})-[:rides]->(:Team {name:'Ducati'})""" - client.graph().query(create_query) - - result = client.graph().explain( - "MATCH (r:Rider)-[:rides]->(t:Team) WHERE t.name = $name RETURN r.name, t.name, $params", # noqa - {"name": "Yehuda"}, - ) - expected = "Results\n Project\n Conditional Traverse | (t:Team)->(r:Rider)\n Filter\n Node By Label Scan | (t:Team)" # noqa - assert result == expected - - -@pytest.mark.redismod def test_slowlog(client): create_query = """CREATE (:Rider {name:'Valentino Rossi'})-[:rides]->(:Team {name:'Yamaha'}), (:Rider {name:'Dani Pedrosa'})-[:rides]->(:Team {name:'Honda'}), @@ -475,3 +461,114 @@ def test_cache_sync(client): assert A._properties[1] == "x" assert A._relationshipTypes[0] == "S" assert A._relationshipTypes[1] == "R" + + +@pytest.mark.redismod +def test_execution_plan(client): + redis_graph = client.graph("execution_plan") + create_query = """CREATE (:Rider {name:'Valentino Rossi'})-[:rides]->(:Team {name:'Yamaha'}), + (:Rider {name:'Dani Pedrosa'})-[:rides]->(:Team {name:'Honda'}), + (:Rider {name:'Andrea Dovizioso'})-[:rides]->(:Team {name:'Ducati'})""" + redis_graph.query(create_query) + + result = redis_graph.execution_plan( + "MATCH (r:Rider)-[:rides]->(t:Team) WHERE t.name = $name RETURN r.name, t.name, $params", # noqa + {"name": "Yehuda"}, + ) + expected = "Results\n Project\n Conditional Traverse | (t:Team)->(r:Rider)\n Filter\n Node By Label Scan | (t:Team)" # noqa + assert result == expected + + redis_graph.delete() + + +@pytest.mark.redismod +def test_explain(client): + redis_graph = client.graph("execution_plan") + # graph creation / population + create_query = """CREATE +(:Rider {name:'Valentino Rossi'})-[:rides]->(:Team {name:'Yamaha'}), +(:Rider {name:'Dani Pedrosa'})-[:rides]->(:Team {name:'Honda'}), +(:Rider {name:'Andrea Dovizioso'})-[:rides]->(:Team {name:'Ducati'})""" + redis_graph.query(create_query) + + result = redis_graph.explain( + """MATCH (r:Rider)-[:rides]->(t:Team) +WHERE t.name = $name +RETURN r.name, t.name +UNION +MATCH (r:Rider)-[:rides]->(t:Team) +WHERE t.name = $name +RETURN r.name, t.name""", + {"name": "Yamaha"}, + ) + expected = """\ +Results +Distinct + Join + Project + Conditional Traverse | (t:Team)->(r:Rider) + Filter + Node By Label Scan | (t:Team) + Project + Conditional Traverse | (t:Team)->(r:Rider) + Filter + Node By Label Scan | (t:Team)""" + assert str(result).replace(" ", "").replace("\n", "") == expected.replace( + " ", "" + ).replace("\n", "") + + expected = Operation("Results").append_child( + Operation("Distinct").append_child( + Operation("Join") + .append_child( + Operation("Project").append_child( + Operation( + "Conditional Traverse", "(t:Team)->(r:Rider)" + ).append_child( + Operation("Filter").append_child( + Operation("Node By Label Scan", "(t:Team)") + ) + ) + ) + ) + .append_child( + Operation("Project").append_child( + Operation( + "Conditional Traverse", "(t:Team)->(r:Rider)" + ).append_child( + Operation("Filter").append_child( + Operation("Node By Label Scan", "(t:Team)") + ) + ) + ) + ) + ) + ) + + assert result.structured_plan == expected + + result = redis_graph.explain( + """MATCH (r:Rider), (t:Team) + RETURN r.name, t.name""" + ) + expected = """\ +Results +Project + Cartesian Product + Node By Label Scan | (r:Rider) + Node By Label Scan | (t:Team)""" + assert str(result).replace(" ", "").replace("\n", "") == expected.replace( + " ", "" + ).replace("\n", "") + + expected = Operation("Results").append_child( + Operation("Project").append_child( + Operation("Cartesian Product") + .append_child(Operation("Node By Label Scan")) + .append_child(Operation("Node By Label Scan")) + ) + ) + + assert result.structured_plan == expected + + redis_graph.delete() diff --git a/tests/test_search.py b/tests/test_search.py index 6c79041..0d15dcc 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -964,7 +964,7 @@ def test_aggregations_groupby(client): res = client.ft().aggregate(req).rows[0] assert res[1] == "redis" - assert res[3] == "10" + assert res[3] == "8" # median of 3,8,10 req = aggregations.AggregateRequest("redis").group_by( "@parent", @@ -1521,3 +1521,56 @@ def test_profile_limited(client): ) assert det["Iterators profile"]["Type"] == "INTERSECT" assert len(res.docs) == 3 # check also the search result + + +@pytest.mark.redismod +def test_text_params(modclient): + modclient.flushdb() + modclient.ft().create_index((TextField("name"),)) + + modclient.ft().add_document("doc1", name="Alice") + modclient.ft().add_document("doc2", name="Bob") + modclient.ft().add_document("doc3", name="Carol") + + params_dict = {"name1": "Alice", "name2": "Bob"} + q = Query("@name:($name1 | $name2 )") + res = modclient.ft().search(q, query_params=params_dict) + assert 2 == res.total + assert "doc1" == res.docs[0].id + assert "doc2" == res.docs[1].id + + +@pytest.mark.redismod +def test_numeric_params(modclient): + modclient.flushdb() + modclient.ft().create_index((NumericField("numval"),)) + + modclient.ft().add_document("doc1", numval=101) + modclient.ft().add_document("doc2", numval=102) + modclient.ft().add_document("doc3", numval=103) + + params_dict = {"min": 101, "max": 102} + q = Query("@numval:[$min $max]") + res = modclient.ft().search(q, query_params=params_dict) + + assert 2 == res.total + assert "doc1" == res.docs[0].id + assert "doc2" == res.docs[1].id + + +@pytest.mark.redismod +def test_geo_params(modclient): + + modclient.flushdb() + modclient.ft().create_index((GeoField("g"))) + modclient.ft().add_document("doc1", g="29.69465, 34.95126") + modclient.ft().add_document("doc2", g="29.69350, 34.94737") + modclient.ft().add_document("doc3", g="29.68746, 34.94882") + + params_dict = {"lat": "34.95126", "lon": "29.69465", "radius": 1000, "units": "km"} + q = Query("@g:[$lon $lat $radius $units]") + res = modclient.ft().search(q, query_params=params_dict) + assert 3 == res.total + assert "doc1" == res.docs[0].id + assert "doc2" == res.docs[1].id + assert "doc3" == res.docs[2].id |