Skip to content

Commit 29b861b

Browse files
authored
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 9d85723 commit 29b861b

File tree

1 file changed

+69
-13
lines changed

1 file changed

+69
-13
lines changed

tests/test_search.py

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

21192163

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

0 commit comments

Comments
 (0)