Skip to content

Commit 9738105

Browse files
committed
Add extra tests for GEO search (#3244)
Add more tests for GEO search, to cover the query operators `within`, `contains`, `intersects` and `disjoint`, for POINT and POLYGON, i.e. the currently supported shapes and operators.
1 parent aea05d7 commit 9738105

File tree

1 file changed

+69
-13
lines changed

1 file changed

+69
-13
lines changed

tests/test_search.py

+69-13
Original file line numberDiff line numberDiff line change
@@ -2107,16 +2107,60 @@ def test_geo_params(client):
21072107
params_dict = {"lat": "34.95126", "lon": "29.69465", "radius": 1000, "units": "km"}
21082108
q = Query("@g:[$lon $lat $radius $units]").dialect(2)
21092109
res = client.ft().search(q, query_params=params_dict)
2110-
if is_resp2_connection(client):
2111-
assert 3 == res.total
2112-
assert "doc1" == res.docs[0].id
2113-
assert "doc2" == res.docs[1].id
2114-
assert "doc3" == res.docs[2].id
2115-
else:
2116-
assert 3 == res["total_results"]
2117-
assert "doc1" == res["results"][0]["id"]
2118-
assert "doc2" == res["results"][1]["id"]
2119-
assert "doc3" == res["results"][2]["id"]
2110+
_assert_geosearch_result(client, res, ["doc1", "doc2", "doc3"])
2111+
2112+
2113+
@pytest.mark.redismod
2114+
def test_geoshapes_query_intersects_and_disjoint(client):
2115+
client.ft().create_index((GeoShapeField("g", coord_system=GeoShapeField.FLAT)))
2116+
client.hset("doc_point1", mapping={"g": "POINT (10 10)"})
2117+
client.hset("doc_point2", mapping={"g": "POINT (50 50)"})
2118+
client.hset("doc_polygon1", mapping={"g": "POLYGON ((20 20, 25 35, 35 25, 20 20))"})
2119+
client.hset(
2120+
"doc_polygon2", mapping={"g": "POLYGON ((60 60, 65 75, 70 70, 65 55, 60 60))"}
2121+
)
2122+
2123+
intersection = client.ft().search(
2124+
Query("@g:[intersects $shape]").dialect(3),
2125+
query_params={"shape": "POLYGON((15 15, 75 15, 50 70, 20 40, 15 15))"},
2126+
)
2127+
_assert_geosearch_result(client, intersection, ["doc_point2", "doc_polygon1"])
2128+
2129+
disjunction = client.ft().search(
2130+
Query("@g:[disjoint $shape]").dialect(3),
2131+
query_params={"shape": "POLYGON((15 15, 75 15, 50 70, 20 40, 15 15))"},
2132+
)
2133+
_assert_geosearch_result(client, disjunction, ["doc_point1", "doc_polygon2"])
2134+
2135+
2136+
@pytest.mark.redismod
2137+
@skip_ifmodversion_lt("2.10.0", "search")
2138+
def test_geoshapes_query_contains_and_within(client):
2139+
client.ft().create_index((GeoShapeField("g", coord_system=GeoShapeField.FLAT)))
2140+
client.hset("doc_point1", mapping={"g": "POINT (10 10)"})
2141+
client.hset("doc_point2", mapping={"g": "POINT (50 50)"})
2142+
client.hset("doc_polygon1", mapping={"g": "POLYGON ((20 20, 25 35, 35 25, 20 20))"})
2143+
client.hset(
2144+
"doc_polygon2", mapping={"g": "POLYGON ((60 60, 65 75, 70 70, 65 55, 60 60))"}
2145+
)
2146+
2147+
contains_a = client.ft().search(
2148+
Query("@g:[contains $shape]").dialect(3),
2149+
query_params={"shape": "POINT(25 25)"},
2150+
)
2151+
_assert_geosearch_result(client, contains_a, ["doc_polygon1"])
2152+
2153+
contains_b = client.ft().search(
2154+
Query("@g:[contains $shape]").dialect(3),
2155+
query_params={"shape": "POLYGON((24 24, 24 26, 25 25, 24 24))"},
2156+
)
2157+
_assert_geosearch_result(client, contains_b, ["doc_polygon1"])
2158+
2159+
within = client.ft().search(
2160+
Query("@g:[within $shape]").dialect(3),
2161+
query_params={"shape": "POLYGON((15 15, 75 15, 50 70, 20 40, 15 15))"},
2162+
)
2163+
_assert_geosearch_result(client, within, ["doc_point2", "doc_polygon1"])
21202164

21212165

21222166
@pytest.mark.redismod
@@ -2280,7 +2324,19 @@ def test_geoshape(client: redis.Redis):
22802324
q2 = Query("@geom:[CONTAINS $poly]").dialect(3)
22812325
qp2 = {"poly": "POLYGON((2 2, 2 50, 50 50, 50 2, 2 2))"}
22822326
result = client.ft().search(q1, query_params=qp1)
2283-
assert len(result.docs) == 1
2284-
assert result.docs[0]["id"] == "small"
2327+
_assert_geosearch_result(client, result, ["small"])
22852328
result = client.ft().search(q2, query_params=qp2)
2286-
assert len(result.docs) == 2
2329+
_assert_geosearch_result(client, result, ["small", "large"])
2330+
2331+
2332+
def _assert_geosearch_result(client, result, expected_doc_ids):
2333+
"""
2334+
Make sure the result of a geo search is as expected, taking into account the RESP
2335+
version being used.
2336+
"""
2337+
if is_resp2_connection(client):
2338+
assert set([doc.id for doc in result.docs]) == set(expected_doc_ids)
2339+
assert result.total == len(expected_doc_ids)
2340+
else:
2341+
assert set([doc["id"] for doc in result["results"]]) == set(expected_doc_ids)
2342+
assert result["total_results"] == len(expected_doc_ids)

0 commit comments

Comments
 (0)