1"""Tests for `chives.dates`."""
7from chives.dates import date_matches_any_format, find_all_dates, reformat_date
10def test_find_all_dates() -> None:
11 """find_all_dates finds all the nested dates in a JSON object."""
12 json_value = json.loads("""{
13 "doc1": {"id": "1", "date_created": "2025-10-14T05:34:07+0000"},
15 {"color": "blue", "date_saved": "2015-03-01 23:34:39 +00:00"},
16 {"color": "yellow", "date_saved": "2013-9-21 13:43:00Z", "is_square": true},
17 {"color": "green", "date_saved": null}
19 "date_verified": "2024-08-30"
22 assert list(find_all_dates(json_value)) == [
24 {"id": "1", "date_created": "2025-10-14T05:34:07+0000"},
26 "2025-10-14T05:34:07+0000",
29 {"color": "blue", "date_saved": "2015-03-01 23:34:39 +00:00"},
31 "2015-03-01 23:34:39 +00:00",
34 {"color": "yellow", "date_saved": "2013-9-21 13:43:00Z", "is_square": True},
36 "2013-9-21 13:43:00Z",
38 (json_value, "date_verified", "2024-08-30"),
42def test_date_matches_any_format() -> None:
44 Tests for `date_matches_any_format`.
46 assert date_matches_any_format(
47 "2001-01-01", formats=["%Y-%m-%d", "%Y-%m-%dT%H:%M:%S%z"]
49 assert not date_matches_any_format("2001-01-01", formats=["%Y-%m-%dT%H:%M:%S%z"])
52@pytest.mark.parametrize(
53 "s, orig_fmt, formatted_date",
55 ("2025-11-12T15:34:39.570Z", "%Y-%m-%dT%H:%M:%S.%fZ", "2025-11-12T15:34:39Z"),
56 ("2025-03-12 09:57:03", "%Y-%m-%d %H:%M:%S", "2025-03-12T09:57:03Z"),
57 ("2016-02-25 05:28:35 GMT", "%Y-%m-%d %H:%M:%S %Z", "2016-02-25T05:28:35Z"),
58 ("2011-12-06T10:45:15-08:00", "%Y-%m-%dT%H:%M:%S%z", "2011-12-06T18:45:15Z"),
61def test_reformat_date(s: str, orig_fmt: str, formatted_date: str) -> None:
62 """Tests for `reformat_date`."""
63 assert reformat_date(s, orig_fmt) == formatted_date