Skip to content

Commit 1d76ff6

Browse files
committed
Add consistency tests
This is one of those helpful situations where we have two implementations of the same logic that should have exactly the same result, so we can feed it any arbitrary input and test that the two are consistent. This is very useful for PEP 399 compliance.
1 parent 714f31a commit 1d76ff6

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Diff for: tests/test_zoneinfo_property.py

+47
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
import datetime
23
import os
34
import pickle
45
import unittest
@@ -175,3 +176,49 @@ def test_nocache(self, key):
175176

176177
class CZoneInfoCacheTest(ZoneInfoCacheTest):
177178
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

Comments
 (0)