diff --git a/swh/model/hypothesis_strategies.py b/swh/model/hypothesis_strategies.py index d193852283d94e0c365a54dab64f37adda1315cd..448487a8ea2e1e2bc79a51529c64402f17cccdd6 100644 --- a/swh/model/hypothesis_strategies.py +++ b/swh/model/hypothesis_strategies.py @@ -72,7 +72,17 @@ def sha1(): def aware_datetimes(): - return datetimes(timezones=timezones()) + # datetimes in Software Heritage are not used for software artifacts + # (which may be much older than 2000), but only for objects like scheduler + # task runs, and origin visits, which were created by Software Heritage, + # so at least in 2015. + # We're forbidding old datetimes, because until 1956, many timezones had seconds + # in their "UTC offsets" (see + # <https://en.wikipedia.org/wiki/Time_zone#Worldwide_time_zones>), which is not + # encodable in ISO8601; and we need our datetimes to be ISO8601-encodable in the + # RPC protocol + min_value = datetime.datetime(2000, 1, 1, 0, 0, 0) + return datetimes(min_value=min_value, timezones=timezones()) @composite diff --git a/swh/model/tests/test_hypothesis_strategies.py b/swh/model/tests/test_hypothesis_strategies.py index 2df2d7a21b48ad68f4fb425208c92a374fc19197..1622b3c71ba47542f4cec845d3a2094f3f152dfd 100644 --- a/swh/model/tests/test_hypothesis_strategies.py +++ b/swh/model/tests/test_hypothesis_strategies.py @@ -6,10 +6,12 @@ import datetime import attr +import iso8601 from hypothesis import given, settings from swh.model.hashutil import DEFAULT_ALGORITHMS from swh.model.hypothesis_strategies import ( + aware_datetimes, objects, object_dicts, contents, @@ -93,6 +95,14 @@ def test_dicts_generation(obj_type_and_obj): assert branch is None or branch["target_type"] in target_types +@given(aware_datetimes()) +def test_datetimes(dt): + # Checks this doesn't raise an error, eg. about seconds in the TZ offset + iso8601.parse_date(dt.isoformat()) + + assert dt.tzinfo is not None + + @given(object_dicts(split_content=False)) def test_dicts_generation_merged_content(obj_type_and_obj): # we should never generate a "skipped_content" here