Skip to content
Merged
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
19 changes: 13 additions & 6 deletions mgqpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ def coerce(a: Any, b: Any) -> tuple[Any, Any]:

match (a, b):
# one is date/datetime, other is str
case (datetime.date() | datetime.datetime(), str()):
return a, _try_date(b, type(a))
case (str(), datetime.date() | datetime.datetime()):
return _try_date(a, type(b)), b
case (datetime.datetime() as dt, str()):
return dt, _try_date(b, datetime.datetime, tzinfo=dt.tzinfo)
case (datetime.date() as d, str()):
return d, _try_date(b, datetime.date)
case (str(), datetime.datetime() as dt):
return _try_date(a, datetime.datetime, tzinfo=dt.tzinfo), dt
case (str(), datetime.date() as d):
return _try_date(a, datetime.date), d

# one is decimal
case (decimal.Decimal(), _):
Expand All @@ -40,12 +44,15 @@ def coerce(a: Any, b: Any) -> tuple[Any, Any]:


# Datetime ↔︎ ISO 8601 string
def _try_date(s: str, target):
def _try_date(s: str, target, *, tzinfo: datetime.tzinfo | None = None):
"""Attempt to convert *val* to target date or datetime"""
try:
if target is datetime.date:
return datetime.date.fromisoformat(s)
return datetime.datetime.fromisoformat(s)
dt = datetime.datetime.fromisoformat(s)
if tzinfo is not None and dt.tzinfo is None:
return dt.replace(tzinfo=tzinfo)
return dt
except ValueError:
# Not a valid ISO format – leave as string so normal
# comparison semantics apply (and tests expecting a failure
Expand Down
21 changes: 21 additions & 0 deletions tests/test_python_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
{"ts": {"$eq": "2025-07-31T12:00:00"}},
True,
),
(
{"ts": _dt.datetime(2025, 7, 31, 0, 0, 0)},
{"ts": {"$eq": "2025-07-31"}},
True,
),
(
{"ts": _dt.datetime(2025, 7, 31, 12, 0, 0)},
{"ts": {"$gt": "2025-07-31"}},
True,
),
(
{"ts": _dt.datetime(2025, 7, 31, 12, 0, 0)},
{"ts": {"$gt": "2025-07-30T23:59:59"}},
Expand All @@ -53,6 +63,7 @@ def test_datetime_with_timezone():

assert Query({"ts": {"$eq": "2025-07-31T10:00:00+00:00"}}).test({"ts": aware})
assert Query({"ts": {"$eq": aware}}).test({"ts": "2025-07-31T10:00:00+00:00"})
assert Query({"ts": {"$gt": "2025-07-31"}}).test({"ts": aware})


@pytest.fixture()
Expand All @@ -74,6 +85,10 @@ def fruit_basket_dict():
{"fruits.0.harvested": {"$eq": "2024-08-01"}},
True,
),
(
{"fruits.0.harvested": {"$eq": _dt.date(2024, 8, 1)}},
True,
),
(
{"fruits.1.type": {"$eq": "aggregate"}},
True,
Expand All @@ -96,6 +111,12 @@ def test_date_coercion(fruit_basket_dict, query, expected):
assert Query(query).test(fruit_basket_dict) is expected


def test_date_filter_string_doc():
assert Query({"harvested": {"$eq": _dt.date(2024, 8, 1)}}).test(
{"harvested": "2024-08-01"}
)


@pytest.mark.parametrize(
"doc, query, expected",
[
Expand Down