|
| 1 | +import datetime |
| 2 | + |
| 3 | +import pytest |
| 4 | +from grz_db.common import serialize_datetime_to_iso_z |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.parametrize( |
| 8 | + "dt,description", |
| 9 | + [ |
| 10 | + # SQLite: reads back as naive (no tzinfo) |
| 11 | + (datetime.datetime(2024, 1, 1, 12, 0, 0), "SQLite naive datetime"), |
| 12 | + # PostgreSQL: reads back as timezone-aware UTC |
| 13 | + (datetime.datetime(2024, 1, 1, 12, 0, 0, tzinfo=datetime.UTC), "PostgreSQL UTC datetime"), |
| 14 | + # Non-UTC timezone (should be converted to UTC) |
| 15 | + ( |
| 16 | + datetime.datetime(2024, 1, 1, 13, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(hours=1))), |
| 17 | + "Non-UTC timezone", |
| 18 | + ), |
| 19 | + ], |
| 20 | +) |
| 21 | +def test_serialize_datetime_to_iso_z_database_agnostic(dt, description): |
| 22 | + """ |
| 23 | + serialize_datetime_to_iso_z must produce identical 'Z'-suffixed output |
| 24 | + regardless of whether the datetime came from SQLite (naive) or PostgreSQL (timezone-aware). |
| 25 | + This ensures signature verification works consistently across database backends. |
| 26 | + """ |
| 27 | + result = serialize_datetime_to_iso_z(dt) |
| 28 | + assert result.endswith("Z"), f"{description}: Expected 'Z' suffix, got: {result}" |
| 29 | + assert "+00:00" not in result, f"{description}: Should not contain '+00:00'" |
| 30 | + # All should normalize to the same UTC representation |
| 31 | + assert result == "2024-01-01T12:00:00Z", f"{description}: Expected normalized UTC, got: {result}" |
0 commit comments