-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_datetime_parsing.py
More file actions
131 lines (94 loc) · 3.71 KB
/
test_datetime_parsing.py
File metadata and controls
131 lines (94 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""Tests for core.utils.datetime_parsing."""
import re
from datetime import datetime, timedelta, timezone
import pytest
from django.utils import timezone as django_timezone
from core.utils.datetime_parsing import (
CANONICAL_INSTANT_UTC_Z_PATTERN,
ensure_aware_utc,
format_instant_iso_z,
parse_iso_datetime,
parse_iso_datetime_lenient,
)
def test_ensure_aware_utc_none():
assert ensure_aware_utc(None) is None
def test_ensure_aware_utc_naive_becomes_utc():
naive = datetime(2024, 6, 1, 12, 0, 0)
assert django_timezone.is_naive(naive)
out = ensure_aware_utc(naive)
assert out is not None
assert out.tzinfo == timezone.utc
def test_ensure_aware_utc_aware_converted_to_utc():
dt = datetime(2024, 1, 1, 15, 0, 0, tzinfo=timezone(timedelta(hours=2)))
out = ensure_aware_utc(dt)
assert out is not None
assert out.tzinfo == timezone.utc
assert out.hour == 13
def test_parse_iso_datetime_empty():
assert parse_iso_datetime(None) is None
assert parse_iso_datetime("") is None
assert parse_iso_datetime(" ") is None
def test_parse_iso_datetime_z_suffix():
dt = parse_iso_datetime("2024-03-15T10:30:00Z")
assert dt is not None
assert dt.tzinfo is None
assert dt.year == 2024
assert dt.month == 3
assert dt.day == 15
assert dt.hour == 10
assert dt.minute == 30
assert dt.second == 0
def test_parse_iso_datetime_date_only():
dt = parse_iso_datetime("2024-12-25")
assert dt is not None
assert dt.tzinfo is None
def test_parse_iso_datetime_with_offset_strips_tz_to_naive_utc():
dt = parse_iso_datetime("2024-01-01T00:00:00+05:00")
assert dt is not None
assert dt.tzinfo is None
assert dt.year == 2023
assert dt.month == 12
assert dt.day == 31
assert dt.hour == 19
assert dt.minute == 0
assert dt.second == 0
def test_parse_iso_datetime_invalid_raises():
with pytest.raises(ValueError, match="Invalid ISO datetime"):
parse_iso_datetime("not-a-date")
def test_format_instant_iso_z_empty():
assert format_instant_iso_z(None) == ""
assert format_instant_iso_z("") == ""
assert format_instant_iso_z(" ") == ""
def test_format_instant_iso_z_z_suffix_utc():
assert format_instant_iso_z("2024-03-15T10:30:00Z") == "2024-03-15T10:30:00Z"
def test_format_instant_iso_z_offset_to_z():
assert format_instant_iso_z("2024-01-01T00:00:00+05:00") == "2023-12-31T19:00:00Z"
def test_format_instant_iso_z_invalid_returns_original():
assert format_instant_iso_z("not-a-date") == "not-a-date"
def test_parse_iso_datetime_lenient_empty():
assert parse_iso_datetime_lenient(None) is None
assert parse_iso_datetime_lenient("") is None
assert parse_iso_datetime_lenient(" ") is None
def test_parse_iso_datetime_lenient_z_utc_aware():
dt = parse_iso_datetime_lenient("2024-01-15T10:30:00Z")
assert dt == datetime(2024, 1, 15, 10, 30, 0, tzinfo=timezone.utc)
def test_parse_iso_datetime_lenient_invalid_returns_none():
assert parse_iso_datetime_lenient("not-a-date") is None
@pytest.mark.parametrize(
"date_str,expected_year",
[
("2023-06-01T00:00:00Z", 2023),
("2025-12-31T23:59:59Z", 2025),
],
)
def test_parse_iso_datetime_lenient_parametrized(date_str, expected_year):
result = parse_iso_datetime_lenient(date_str)
assert result is not None
assert result.year == expected_year
def test_canonical_instant_utc_z_pattern():
pat = re.compile(CANONICAL_INSTANT_UTC_Z_PATTERN)
assert pat.fullmatch("2026-01-01T00:00:00Z")
assert pat.fullmatch("2026-01-01T00:00:00.5Z")
assert pat.fullmatch("2026-01-01T00:00:00.123456Z")
assert pat.fullmatch("2026-01-01T00:00:00+00:00") is None
assert pat.fullmatch("") is None