Skip to content

Commit b9ddfba

Browse files
committed
Add unit tests for datetime, file, and YAML utility functions
1 parent 14b5280 commit b9ddfba

3 files changed

Lines changed: 725 additions & 0 deletions

File tree

test/test_datetime_utils.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Tests for datetime utility functions.
4+
"""
5+
6+
import pytest
7+
from util.datetime_utils import format_datetime, format_datetime_for_git, MIN_GIT_YEAR
8+
9+
10+
# ===========================================================================
11+
# format_datetime Tests
12+
# ===========================================================================
13+
14+
@pytest.mark.unit
15+
class TestFormatDatetime:
16+
"""Test the format_datetime function."""
17+
18+
def test_format_datetime_with_time(self):
19+
"""Test formatting datetime with time component (should strip time)."""
20+
result = format_datetime("2024-03-15T14:30:00")
21+
assert result == "2024-03-15"
22+
23+
def test_format_datetime_date_only(self):
24+
"""Test formatting date without time component."""
25+
result = format_datetime("2024-03-15")
26+
assert result == "2024-03-15"
27+
28+
def test_format_datetime_with_timezone(self):
29+
"""Test formatting datetime with timezone (should strip it)."""
30+
result = format_datetime("2024-03-15T14:30:00+01:00")
31+
assert result == "2024-03-15"
32+
33+
def test_format_datetime_none(self):
34+
"""Test that None input returns None."""
35+
result = format_datetime(None)
36+
assert result is None
37+
38+
def test_format_datetime_empty_string(self):
39+
"""Test that empty string returns None."""
40+
result = format_datetime("")
41+
assert result is None
42+
43+
def test_format_datetime_invalid_format(self):
44+
"""Test invalid datetime format returns original string."""
45+
invalid_input = "not-a-valid-date"
46+
result = format_datetime(invalid_input)
47+
assert result == invalid_input
48+
49+
50+
# ===========================================================================
51+
# format_datetime_for_git Tests
52+
# ===========================================================================
53+
54+
@pytest.mark.unit
55+
class TestFormatDatetimeForGit:
56+
"""Test the format_datetime_for_git function."""
57+
58+
def test_valid_datetime_with_time(self):
59+
"""Test formatting valid datetime with time component."""
60+
result = format_datetime_for_git("2024-03-15T14:30:00")
61+
assert result == "2024-03-15T14:30:00"
62+
63+
def test_datetime_with_z_timezone(self):
64+
"""Test formatting datetime with Z (Zulu/UTC) timezone."""
65+
result = format_datetime_for_git("2024-03-15T14:30:00Z")
66+
assert result == "2024-03-15T14:30:00"
67+
68+
def test_date_only_adds_midnight_time(self):
69+
"""Test that date without time gets midnight time added."""
70+
result = format_datetime_for_git("2024-03-15")
71+
assert result == "2024-03-15T00:00:00"
72+
73+
def test_date_before_min_git_year(self):
74+
"""Test that dates before MIN_GIT_YEAR are clamped to MIN_GIT_YEAR."""
75+
# 1969 < MIN_GIT_YEAR (1980)
76+
result = format_datetime_for_git("1969-01-01")
77+
assert result == f"{MIN_GIT_YEAR}-01-01T00:00:00"
78+
assert result.startswith("1980")
79+
80+
def test_date_before_min_git_year_with_time(self):
81+
"""Test that datetime before MIN_GIT_YEAR is clamped (with time)."""
82+
result = format_datetime_for_git("1975-06-15T12:30:00")
83+
assert result == f"{MIN_GIT_YEAR}-01-01T00:00:00"
84+
85+
def test_very_old_date(self):
86+
"""Test very old dates (e.g., 1800s) are clamped to MIN_GIT_YEAR."""
87+
result = format_datetime_for_git("1850-01-01")
88+
assert result == f"{MIN_GIT_YEAR}-01-01T00:00:00"
89+
90+
def test_none_value(self):
91+
"""Test that None input returns None."""
92+
result = format_datetime_for_git(None)
93+
assert result is None
94+
95+
def test_empty_string(self):
96+
"""Test that empty string returns None."""
97+
result = format_datetime_for_git("")
98+
assert result is None
99+
100+
def test_invalid_format_fallback(self):
101+
"""Test invalid datetime format raises ValueError."""
102+
# If it's not a valid ISO format and can't be parsed, raises ValueError
103+
with pytest.raises(ValueError):
104+
format_datetime_for_git("not-a-date")
105+
106+
def test_date_at_min_git_year_boundary(self):
107+
"""Test date exactly at MIN_GIT_YEAR boundary."""
108+
result = format_datetime_for_git(f"{MIN_GIT_YEAR}-01-01")
109+
assert result == f"{MIN_GIT_YEAR}-01-01T00:00:00"
110+
111+
def test_date_one_year_before_min(self):
112+
"""Test date one year before MIN_GIT_YEAR."""
113+
result = format_datetime_for_git(f"{MIN_GIT_YEAR - 1}-12-31")
114+
assert result == f"{MIN_GIT_YEAR}-01-01T00:00:00"
115+
116+
117+
# ===========================================================================
118+
# Edge Cases
119+
# ===========================================================================
120+
121+
@pytest.mark.unit
122+
class TestDatetimeEdgeCases:
123+
"""Test edge cases for datetime utilities."""
124+
125+
def test_leap_year_date(self):
126+
"""Test handling of leap year dates."""
127+
result = format_datetime("2024-02-29")
128+
assert result == "2024-02-29"
129+
130+
result_git = format_datetime_for_git("2024-02-29T23:59:59")
131+
assert result_git == "2024-02-29T23:59:59"
132+
133+
def test_end_of_year_date(self):
134+
"""Test handling of end-of-year dates."""
135+
result = format_datetime_for_git("2024-12-31T23:59:59")
136+
assert result == "2024-12-31T23:59:59"
137+
138+
def test_various_datetime_formats(self):
139+
"""Test various valid ISO datetime formats."""
140+
test_cases = [
141+
("2024-01-01", "2024-01-01"),
142+
("2024-06-15T12:00:00", "2024-06-15"),
143+
("2024-12-31T23:59:59Z", "2024-12-31"),
144+
]
145+
146+
for input_dt, expected in test_cases:
147+
result = format_datetime(input_dt)
148+
assert result == expected

0 commit comments

Comments
 (0)