Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tests/test_search_querystring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Unit tests for the search query string builders.

These tests exercise the pure-Python query building logic and do not require a
running Valkey server.
"""

from valkey.commands.search.querystring import GeoValue, geo, intersect


class TestGeoValue:
"""``geo()`` must render longitude before latitude.

RediSearch expects geo filters as ``[lon lat radius unit]``, which is also
the order used by ``GeoValue`` and ``query.GeoFilter``.
"""

def test_geo_renders_longitude_before_latitude(self):
assert geo(lat=51.45, lon=-0.44, radius=10).to_string() == "[-0.44 51.45 10 km]"

def test_geo_matches_geovalue_built_directly(self):
assert (
geo(lat=51.45, lon=-0.44, radius=10).to_string()
== GeoValue(-0.44, 51.45, 10).to_string()
)

def test_geo_keeps_the_requested_unit(self):
assert (
geo(lat=51.45, lon=-0.44, radius=10, unit="mi").to_string()
== "[-0.44 51.45 10 mi]"
)

def test_geo_in_a_query(self):
query = intersect(location=geo(lat=51.45, lon=-0.44, radius=10))
assert query.to_string() == "@location:[-0.44 51.45 10 km]"
6 changes: 5 additions & 1 deletion valkey/commands/search/querystring.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ def ge(n):
def geo(lat, lon, radius, unit="km"):
"""
Indicate that value is a geo region

``GeoValue`` stores and renders longitude first, so the arguments are
swapped here to keep the ``lat, lon`` order of this helper's public
signature.
"""
return GeoValue(lat, lon, radius, unit)
return GeoValue(lon, lat, radius, unit)


class Value:
Expand Down