/
/
/
1"""Tests for the datetime-helper usage check."""
2
3import ast
4
5from scripts.check_datetime_helpers import _naive_now_call, main
6
7
8def _first_call(source: str) -> ast.Call:
9 """Return the first ``ast.Call`` node in a source snippet."""
10 return next(node for node in ast.walk(ast.parse(source)) if isinstance(node, ast.Call))
11
12
13def test_matches_direct_datetime_now_variants() -> None:
14 """All common direct ``datetime`` now/utcnow call shapes are matched."""
15 assert _naive_now_call(_first_call("datetime.now(UTC)")) == "datetime.now"
16 assert _naive_now_call(_first_call("datetime.utcnow()")) == "datetime.utcnow"
17 assert _naive_now_call(_first_call("datetime.datetime.now(tz)")) == "datetime.datetime.now"
18 assert _naive_now_call(_first_call("datetime.datetime.utcnow()")) == "datetime.datetime.utcnow"
19
20
21def test_ignores_unrelated_now_calls() -> None:
22 """A ``now()`` on a non-datetime object and other time calls are not matched."""
23 assert _naive_now_call(_first_call("time.time()")) is None
24 assert _naive_now_call(_first_call("clock.now()")) is None
25 assert _naive_now_call(_first_call("helpers_datetime.utc()")) is None
26
27
28def test_real_tree_matches_baseline() -> None:
29 """The shipped tree has no datetime call sites beyond the grandfathered baseline."""
30 assert main([]) == 0
31