|
4 | 4 | Tests for utility functions that don't require Flask/DB context. |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import datetime |
| 8 | + |
7 | 9 | import pytest |
8 | 10 | from unittest.mock import MagicMock, patch |
9 | 11 | from colorama import Fore, Style |
10 | 12 |
|
11 | 13 | from ref.core.util import ( |
12 | 14 | AnsiColorUtil, |
| 15 | + datetime_to_string, |
13 | 16 | is_db_serialization_error, |
14 | 17 | is_deadlock_error, |
15 | 18 | ssh_key_basename, |
| 19 | + utc_datetime_to_local_tz, |
16 | 20 | ) |
17 | 21 |
|
18 | 22 |
|
@@ -268,3 +272,57 @@ def test_leading_whitespace_is_stripped(self): |
268 | 272 |
|
269 | 273 | def test_unknown_algo_falls_back_to_rsa(self): |
270 | 274 | assert ssh_key_basename("bogus-algo AAAA") == "id_rsa" |
| 275 | + |
| 276 | + |
| 277 | +@pytest.mark.offline |
| 278 | +class TestUtcDatetimeToLocalTz: |
| 279 | + """Conversion of naive-UTC datetimes (as stored in the DB) to the |
| 280 | + configured system timezone, for display purposes.""" |
| 281 | + |
| 282 | + def test_converts_naive_utc_to_berlin_cest(self): |
| 283 | + naive_utc = datetime.datetime(2026, 4, 26, 15, 59) |
| 284 | + with patch("ref.core.util.SystemSettingsManager") as ssm: |
| 285 | + ssm.TIMEZONE.value = "Europe/Berlin" |
| 286 | + local = utc_datetime_to_local_tz(naive_utc) |
| 287 | + assert local.strftime("%Y-%m-%dT%H:%M") == "2026-04-26T17:59" |
| 288 | + |
| 289 | + def test_converts_naive_utc_to_berlin_cet_in_winter(self): |
| 290 | + naive_utc = datetime.datetime(2026, 1, 15, 12, 0) |
| 291 | + with patch("ref.core.util.SystemSettingsManager") as ssm: |
| 292 | + ssm.TIMEZONE.value = "Europe/Berlin" |
| 293 | + local = utc_datetime_to_local_tz(naive_utc) |
| 294 | + assert local.strftime("%Y-%m-%dT%H:%M") == "2026-01-15T13:00" |
| 295 | + |
| 296 | + def test_utc_timezone_is_identity(self): |
| 297 | + naive_utc = datetime.datetime(2026, 4, 26, 15, 59) |
| 298 | + with patch("ref.core.util.SystemSettingsManager") as ssm: |
| 299 | + ssm.TIMEZONE.value = "UTC" |
| 300 | + local = utc_datetime_to_local_tz(naive_utc) |
| 301 | + assert local.strftime("%Y-%m-%dT%H:%M") == "2026-04-26T15:59" |
| 302 | + |
| 303 | + def test_crosses_day_boundary(self): |
| 304 | + naive_utc = datetime.datetime(2026, 4, 26, 23, 30) |
| 305 | + with patch("ref.core.util.SystemSettingsManager") as ssm: |
| 306 | + ssm.TIMEZONE.value = "Europe/Berlin" |
| 307 | + local = utc_datetime_to_local_tz(naive_utc) |
| 308 | + assert local.strftime("%Y-%m-%dT%H:%M") == "2026-04-27T01:30" |
| 309 | + |
| 310 | + |
| 311 | +@pytest.mark.offline |
| 312 | +class TestDatetimeToString: |
| 313 | + """Human-readable formatting of DB-stored (naive UTC) datetimes.""" |
| 314 | + |
| 315 | + def test_naive_utc_formatted_in_system_tz(self): |
| 316 | + naive_utc = datetime.datetime(2026, 4, 26, 15, 59, 0) |
| 317 | + with patch("ref.core.util.SystemSettingsManager") as ssm: |
| 318 | + ssm.TIMEZONE.value = "Europe/Berlin" |
| 319 | + s = datetime_to_string(naive_utc) |
| 320 | + assert s == "26/04/2026 17:59:00" |
| 321 | + |
| 322 | + def test_aware_datetime_preserves_its_tz(self): |
| 323 | + from dateutil import tz as _tz |
| 324 | + |
| 325 | + aware_utc = datetime.datetime(2026, 4, 26, 15, 59, 0, tzinfo=_tz.gettz("UTC")) |
| 326 | + # Aware datetimes pass through without re-conversion. |
| 327 | + s = datetime_to_string(aware_utc) |
| 328 | + assert s == "26/04/2026 15:59:00" |
0 commit comments