| 
1 | 1 | """Test helpers module."""  | 
2 | 2 | from contextlib import nullcontext  | 
 | 3 | +from datetime import datetime, timedelta, timezone  | 
3 | 4 | 
 
  | 
4 | 5 | import pytest  | 
5 | 6 | 
 
  | 
@@ -81,3 +82,102 @@ def test_convert_letterboxed_bbox(  | 
81 | 82 |         assert converted_bbox == expected  | 
82 | 83 |     if message:  | 
83 | 84 |         assert str(exception.value) == message  | 
 | 85 | + | 
 | 86 | + | 
 | 87 | +def test_basic_conversion_zero_offset():  | 
 | 88 | +    """Test with zero UTC offset."""  | 
 | 89 | +    date = "2024-01-01"  | 
 | 90 | +    utc_offset = timedelta(hours=0)  | 
 | 91 | + | 
 | 92 | +    time_from, time_to = helpers.daterange_to_utc(date, utc_offset)  | 
 | 93 | + | 
 | 94 | +    assert time_from == datetime(2024, 1, 1, 0, 0, 0, 0, tzinfo=timezone.utc)  | 
 | 95 | +    assert time_to == datetime(2024, 1, 1, 23, 59, 59, 999999, tzinfo=timezone.utc)  | 
 | 96 | + | 
 | 97 | + | 
 | 98 | +def test_positive_utc_offset():  | 
 | 99 | +    """Test with positive UTC offset (e.g., UTC+5)."""  | 
 | 100 | +    date = "2024-01-01"  | 
 | 101 | +    utc_offset = timedelta(hours=5)  | 
 | 102 | + | 
 | 103 | +    time_from, time_to = helpers.daterange_to_utc(date, utc_offset)  | 
 | 104 | + | 
 | 105 | +    # With UTC+5, the UTC time should be 5 hours earlier  | 
 | 106 | +    assert time_from == datetime(2023, 12, 31, 19, 0, 0, 0, tzinfo=timezone.utc)  | 
 | 107 | +    assert time_to == datetime(2024, 1, 1, 18, 59, 59, 999999, tzinfo=timezone.utc)  | 
 | 108 | + | 
 | 109 | + | 
 | 110 | +def test_negative_utc_offset():  | 
 | 111 | +    """Test with negative UTC offset (e.g., UTC-5)."""  | 
 | 112 | +    date = "2024-01-01"  | 
 | 113 | +    utc_offset = timedelta(hours=-5)  | 
 | 114 | + | 
 | 115 | +    time_from, time_to = helpers.daterange_to_utc(date, utc_offset)  | 
 | 116 | + | 
 | 117 | +    # With UTC-5, the UTC time should be 5 hours later  | 
 | 118 | +    assert time_from == datetime(2024, 1, 1, 5, 0, 0, 0, tzinfo=timezone.utc)  | 
 | 119 | +    assert time_to == datetime(2024, 1, 2, 4, 59, 59, 999999, tzinfo=timezone.utc)  | 
 | 120 | + | 
 | 121 | + | 
 | 122 | +def test_fractional_hour_offset():  | 
 | 123 | +    """Test with UTC offset including minutes."""  | 
 | 124 | +    date = "2024-01-01"  | 
 | 125 | +    utc_offset = timedelta(hours=5, minutes=30)  # UTC+5:30 (like India)  | 
 | 126 | + | 
 | 127 | +    time_from, time_to = helpers.daterange_to_utc(date, utc_offset)  | 
 | 128 | + | 
 | 129 | +    assert time_from == datetime(2023, 12, 31, 18, 30, 0, 0, tzinfo=timezone.utc)  | 
 | 130 | +    assert time_to == datetime(2024, 1, 1, 18, 29, 59, 999999, tzinfo=timezone.utc)  | 
 | 131 | + | 
 | 132 | + | 
 | 133 | +def test_year_boundary():  | 
 | 134 | +    """Test date at year boundary with offset that crosses the year."""  | 
 | 135 | +    date = "2024-01-01"  | 
 | 136 | +    utc_offset = timedelta(hours=2)  | 
 | 137 | + | 
 | 138 | +    time_from, time_to = helpers.daterange_to_utc(date, utc_offset)  | 
 | 139 | + | 
 | 140 | +    assert time_from == datetime(2023, 12, 31, 22, 0, 0, 0, tzinfo=timezone.utc)  | 
 | 141 | +    assert time_to == datetime(2024, 1, 1, 21, 59, 59, 999999, tzinfo=timezone.utc)  | 
 | 142 | + | 
 | 143 | + | 
 | 144 | +def test_invalid_date_format():  | 
 | 145 | +    """Test that invalid date format raises ValueError."""  | 
 | 146 | +    date = "2024/01/01"  # Wrong format  | 
 | 147 | +    utc_offset = timedelta(hours=0)  | 
 | 148 | + | 
 | 149 | +    with pytest.raises(ValueError):  | 
 | 150 | +        helpers.daterange_to_utc(date, utc_offset)  | 
 | 151 | + | 
 | 152 | + | 
 | 153 | +def test_leap_year_date():  | 
 | 154 | +    """Test with February 29th on a leap year."""  | 
 | 155 | +    date = "2024-02-29"  # 2024 is a leap year  | 
 | 156 | +    utc_offset = timedelta(hours=0)  | 
 | 157 | + | 
 | 158 | +    time_from, time_to = helpers.daterange_to_utc(date, utc_offset)  | 
 | 159 | + | 
 | 160 | +    assert time_from == datetime(2024, 2, 29, 0, 0, 0, 0, tzinfo=timezone.utc)  | 
 | 161 | +    assert time_to == datetime(2024, 2, 29, 23, 59, 59, 999999, tzinfo=timezone.utc)  | 
 | 162 | + | 
 | 163 | + | 
 | 164 | +def test_extreme_offset():  | 
 | 165 | +    """Test with maximum possible UTC offset."""  | 
 | 166 | +    date = "2024-01-01"  | 
 | 167 | +    utc_offset = timedelta(hours=14)  # Maximum UTC offset (UTC+14)  | 
 | 168 | + | 
 | 169 | +    time_from, time_to = helpers.daterange_to_utc(date, utc_offset)  | 
 | 170 | + | 
 | 171 | +    assert time_from == datetime(2023, 12, 31, 10, 0, 0, 0, tzinfo=timezone.utc)  | 
 | 172 | +    assert time_to == datetime(2024, 1, 1, 9, 59, 59, 999999, tzinfo=timezone.utc)  | 
 | 173 | + | 
 | 174 | + | 
 | 175 | +def test_microsecond_precision():  | 
 | 176 | +    """Test that microsecond precision is maintained."""  | 
 | 177 | +    date = "2024-01-01"  | 
 | 178 | +    utc_offset = timedelta(hours=0)  | 
 | 179 | + | 
 | 180 | +    time_from, time_to = helpers.daterange_to_utc(date, utc_offset)  | 
 | 181 | + | 
 | 182 | +    assert time_from.microsecond == 0  | 
 | 183 | +    assert time_to.microsecond == 999999  | 
0 commit comments