|
1 | 1 | import contextlib
|
| 2 | +import datetime |
2 | 3 | import os
|
3 | 4 | import pickle
|
4 | 5 | import unittest
|
@@ -175,3 +176,49 @@ def test_nocache(self, key):
|
175 | 176 |
|
176 | 177 | class CZoneInfoCacheTest(ZoneInfoCacheTest):
|
177 | 178 | klass = c_zoneinfo.ZoneInfo
|
| 179 | + |
| 180 | + |
| 181 | +class PythonCConsistencyTest(unittest.TestCase): |
| 182 | + """Tests that the C and Python versions do the same thing.""" |
| 183 | + |
| 184 | + @hypothesis.given(dt=hypothesis.strategies.datetimes(), key=valid_keys()) |
| 185 | + def test_same_str(self, dt, key): |
| 186 | + py_dt = dt.replace(tzinfo=py_zoneinfo.ZoneInfo(key)) |
| 187 | + c_dt = dt.replace(tzinfo=c_zoneinfo.ZoneInfo(key)) |
| 188 | + |
| 189 | + self.assertEqual(str(py_dt), str(c_dt)) |
| 190 | + |
| 191 | + @hypothesis.given(dt=hypothesis.strategies.datetimes(), key=valid_keys()) |
| 192 | + def test_same_offsets_and_names(self, dt, key): |
| 193 | + py_dt = dt.replace(tzinfo=py_zoneinfo.ZoneInfo(key)) |
| 194 | + c_dt = dt.replace(tzinfo=c_zoneinfo.ZoneInfo(key)) |
| 195 | + |
| 196 | + self.assertEqual(py_dt.tzname(), c_dt.tzname()) |
| 197 | + self.assertEqual(py_dt.utcoffset(), c_dt.utcoffset()) |
| 198 | + self.assertEqual(py_dt.dst(), c_dt.dst()) |
| 199 | + |
| 200 | + @hypothesis.given( |
| 201 | + dt=hypothesis.strategies.datetimes( |
| 202 | + timezones=hypothesis.strategies.just(datetime.timezone.utc) |
| 203 | + ), |
| 204 | + key=valid_keys(), |
| 205 | + ) |
| 206 | + def test_same_from_utc(self, dt, key): |
| 207 | + py_dt = dt.astimezone(py_zoneinfo.ZoneInfo(key)) |
| 208 | + c_dt = dt.astimezone(c_zoneinfo.ZoneInfo(key)) |
| 209 | + |
| 210 | + self.assertEqual(py_dt, c_dt) # This is probably trivially true |
| 211 | + |
| 212 | + self.assertEqual(py_dt.tzname(), c_dt.tzname()) |
| 213 | + self.assertEqual(py_dt.utcoffset(), c_dt.utcoffset()) |
| 214 | + self.assertEqual(py_dt.dst(), c_dt.dst()) |
| 215 | + |
| 216 | + @hypothesis.given(dt=hypothesis.strategies.datetimes(), key=valid_keys()) |
| 217 | + def test_same_to_utc(self, dt, key): |
| 218 | + py_dt = dt.replace(tzinfo=py_zoneinfo.ZoneInfo(key)) |
| 219 | + c_dt = dt.replace(tzinfo=c_zoneinfo.ZoneInfo(key)) |
| 220 | + |
| 221 | + py_utc = py_dt.astimezone(datetime.timezone.utc) |
| 222 | + c_utc = c_dt.astimezone(datetime.timezone.utc) |
| 223 | + |
| 224 | + self.assertEqual(py_utc, c_utc) |
0 commit comments