Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_model.py
Show First 20 Lines • Show All 444 Lines • ▼ Show 20 Lines | with pytest.raises(ValueError): | ||||
Timestamp.from_dict({"seconds": 0, "microseconds": 10 ** 6}) | Timestamp.from_dict({"seconds": 0, "microseconds": 10 ** 6}) | ||||
# TimestampWithTimezone | # TimestampWithTimezone | ||||
def test_timestampwithtimezone(): | def test_timestampwithtimezone(): | ||||
ts = Timestamp(seconds=0, microseconds=0) | ts = Timestamp(seconds=0, microseconds=0) | ||||
tstz = TimestampWithTimezone(timestamp=ts, offset=0, negative_utc=False) | tstz = TimestampWithTimezone(timestamp=ts, offset_bytes=b"+0000") | ||||
attr.validate(tstz) | attr.validate(tstz) | ||||
assert tstz.negative_utc is False | assert tstz.offset == 0 | ||||
assert tstz.offset_bytes == b"+0000" | assert tstz.offset_bytes == b"+0000" | ||||
tstz = TimestampWithTimezone(timestamp=ts, offset=10, negative_utc=False) | tstz = TimestampWithTimezone(timestamp=ts, offset_bytes=b"+0010") | ||||
attr.validate(tstz) | attr.validate(tstz) | ||||
assert tstz.offset == 10 | |||||
assert tstz.offset_bytes == b"+0010" | assert tstz.offset_bytes == b"+0010" | ||||
tstz = TimestampWithTimezone(timestamp=ts, offset=-10, negative_utc=False) | tstz = TimestampWithTimezone(timestamp=ts, offset_bytes=b"-0010") | ||||
attr.validate(tstz) | attr.validate(tstz) | ||||
assert tstz.offset == -10 | |||||
assert tstz.offset_bytes == b"-0010" | assert tstz.offset_bytes == b"-0010" | ||||
tstz = TimestampWithTimezone(timestamp=ts, offset=0, negative_utc=True) | tstz = TimestampWithTimezone(timestamp=ts, offset_bytes=b"-0000") | ||||
attr.validate(tstz) | attr.validate(tstz) | ||||
assert tstz.negative_utc is True | assert tstz.offset == 0 | ||||
assert tstz.offset_bytes == b"-0000" | assert tstz.offset_bytes == b"-0000" | ||||
tstz = TimestampWithTimezone(timestamp=ts, offset=-630, negative_utc=False) | tstz = TimestampWithTimezone(timestamp=ts, offset_bytes=b"-1030") | ||||
attr.validate(tstz) | attr.validate(tstz) | ||||
assert tstz.negative_utc is False | assert tstz.offset == -630 | ||||
assert tstz.offset_bytes == b"-1030" | assert tstz.offset_bytes == b"-1030" | ||||
tstz = TimestampWithTimezone(timestamp=ts, offset=800, negative_utc=False) | tstz = TimestampWithTimezone(timestamp=ts, offset_bytes=b"+1320") | ||||
attr.validate(tstz) | attr.validate(tstz) | ||||
assert tstz.negative_utc is False | assert tstz.offset == 800 | ||||
assert tstz.offset_bytes == b"+1320" | assert tstz.offset_bytes == b"+1320" | ||||
with pytest.raises(AttributeTypeError): | with pytest.raises(AttributeTypeError): | ||||
TimestampWithTimezone( | TimestampWithTimezone(timestamp=datetime.datetime.now(), offset_bytes=b"+0000") | ||||
timestamp=datetime.datetime.now(), offset=0, negative_utc=False | |||||
) | |||||
with pytest.raises((AttributeTypeError, TypeError)): | |||||
TimestampWithTimezone(timestamp=ts, offset="0", negative_utc=False) | |||||
with pytest.raises(AttributeTypeError): | with pytest.raises((AttributeTypeError, AttributeError, TypeError)): | ||||
TimestampWithTimezone(timestamp=ts, offset=1.0, negative_utc=False) | TimestampWithTimezone(timestamp=ts, offset_bytes=0) | ||||
with pytest.raises(AttributeTypeError): | |||||
TimestampWithTimezone(timestamp=ts, offset=1, negative_utc=0) | |||||
with pytest.raises(ValueError): | |||||
TimestampWithTimezone(timestamp=ts, offset=1, negative_utc=True) | |||||
with pytest.raises(ValueError): | |||||
TimestampWithTimezone(timestamp=ts, offset=-1, negative_utc=True) | |||||
def test_timestampwithtimezone_from_datetime(): | def test_timestampwithtimezone_from_datetime(): | ||||
# Typical case | # Typical case | ||||
tz = datetime.timezone(datetime.timedelta(minutes=+60)) | tz = datetime.timezone(datetime.timedelta(minutes=+60)) | ||||
date = datetime.datetime(2020, 2, 27, 14, 39, 19, tzinfo=tz) | date = datetime.datetime(2020, 2, 27, 14, 39, 19, tzinfo=tz) | ||||
tstz = TimestampWithTimezone.from_datetime(date) | tstz = TimestampWithTimezone.from_datetime(date) | ||||
assert tstz == TimestampWithTimezone( | assert tstz == TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1582810759, microseconds=0,), | timestamp=Timestamp(seconds=1582810759, microseconds=0,), offset_bytes=b"+0100" | ||||
offset=60, | |||||
negative_utc=False, | |||||
) | ) | ||||
# Typical case (close to epoch) | # Typical case (close to epoch) | ||||
tz = datetime.timezone(datetime.timedelta(minutes=+60)) | tz = datetime.timezone(datetime.timedelta(minutes=+60)) | ||||
date = datetime.datetime(1970, 1, 1, 1, 0, 5, tzinfo=tz) | date = datetime.datetime(1970, 1, 1, 1, 0, 5, tzinfo=tz) | ||||
tstz = TimestampWithTimezone.from_datetime(date) | tstz = TimestampWithTimezone.from_datetime(date) | ||||
assert tstz == TimestampWithTimezone( | assert tstz == TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=5, microseconds=0,), offset=60, negative_utc=False, | timestamp=Timestamp(seconds=5, microseconds=0,), offset_bytes=b"+0100" | ||||
) | ) | ||||
# non-integer number of seconds before UNIX epoch | # non-integer number of seconds before UNIX epoch | ||||
date = datetime.datetime( | date = datetime.datetime( | ||||
1969, 12, 31, 23, 59, 59, 100000, tzinfo=datetime.timezone.utc | 1969, 12, 31, 23, 59, 59, 100000, tzinfo=datetime.timezone.utc | ||||
) | ) | ||||
tstz = TimestampWithTimezone.from_datetime(date) | tstz = TimestampWithTimezone.from_datetime(date) | ||||
assert tstz == TimestampWithTimezone( | assert tstz == TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=-1, microseconds=100000,), | timestamp=Timestamp(seconds=-1, microseconds=100000,), offset_bytes=b"+0000" | ||||
offset=0, | |||||
negative_utc=False, | |||||
) | ) | ||||
# non-integer number of seconds in both the timestamp and the offset | # non-integer number of seconds in both the timestamp and the offset | ||||
tz = datetime.timezone(datetime.timedelta(microseconds=-600000)) | tz = datetime.timezone(datetime.timedelta(microseconds=-600000)) | ||||
date = datetime.datetime(1969, 12, 31, 23, 59, 59, 600000, tzinfo=tz) | date = datetime.datetime(1969, 12, 31, 23, 59, 59, 600000, tzinfo=tz) | ||||
tstz = TimestampWithTimezone.from_datetime(date) | tstz = TimestampWithTimezone.from_datetime(date) | ||||
assert tstz == TimestampWithTimezone( | assert tstz == TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=0, microseconds=200000,), | timestamp=Timestamp(seconds=0, microseconds=200000,), offset_bytes=b"+0000" | ||||
offset=0, | |||||
negative_utc=False, | |||||
) | ) | ||||
# timezone offset with non-integer number of seconds, for dates before epoch | # timezone offset with non-integer number of seconds, for dates before epoch | ||||
# we round down to the previous second, so it should be the same as | # we round down to the previous second, so it should be the same as | ||||
# 1969-01-01T23:59:59Z | # 1969-01-01T23:59:59Z | ||||
tz = datetime.timezone(datetime.timedelta(microseconds=900000)) | tz = datetime.timezone(datetime.timedelta(microseconds=900000)) | ||||
date = datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=tz) | date = datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=tz) | ||||
tstz = TimestampWithTimezone.from_datetime(date) | tstz = TimestampWithTimezone.from_datetime(date) | ||||
assert tstz == TimestampWithTimezone( | assert tstz == TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=-1, microseconds=100000,), | timestamp=Timestamp(seconds=-1, microseconds=100000,), offset_bytes=b"+0000" | ||||
offset=0, | |||||
negative_utc=False, | |||||
) | ) | ||||
def test_timestampwithtimezone_from_naive_datetime(): | def test_timestampwithtimezone_from_naive_datetime(): | ||||
date = datetime.datetime(2020, 2, 27, 14, 39, 19) | date = datetime.datetime(2020, 2, 27, 14, 39, 19) | ||||
with pytest.raises(ValueError, match="datetime without timezone"): | with pytest.raises(ValueError, match="datetime without timezone"): | ||||
TimestampWithTimezone.from_datetime(date) | TimestampWithTimezone.from_datetime(date) | ||||
def test_timestampwithtimezone_from_iso8601(): | def test_timestampwithtimezone_from_iso8601(): | ||||
date = "2020-02-27 14:39:19.123456+0100" | date = "2020-02-27 14:39:19.123456+0100" | ||||
tstz = TimestampWithTimezone.from_iso8601(date) | tstz = TimestampWithTimezone.from_iso8601(date) | ||||
assert tstz == TimestampWithTimezone( | assert tstz == TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1582810759, microseconds=123456,), | timestamp=Timestamp(seconds=1582810759, microseconds=123456,), | ||||
offset=60, | offset_bytes=b"+0100", | ||||
negative_utc=False, | |||||
) | ) | ||||
def test_timestampwithtimezone_from_iso8601_negative_utc(): | def test_timestampwithtimezone_from_iso8601_negative_utc(): | ||||
date = "2020-02-27 13:39:19-0000" | date = "2020-02-27 13:39:19-0000" | ||||
tstz = TimestampWithTimezone.from_iso8601(date) | tstz = TimestampWithTimezone.from_iso8601(date) | ||||
assert tstz == TimestampWithTimezone( | assert tstz == TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1582810759, microseconds=0,), | timestamp=Timestamp(seconds=1582810759, microseconds=0,), offset_bytes=b"-0000" | ||||
offset=0, | |||||
negative_utc=True, | |||||
) | ) | ||||
@pytest.mark.parametrize("date", TS_DATETIMES) | @pytest.mark.parametrize("date", TS_DATETIMES) | ||||
@pytest.mark.parametrize("tz", TS_TIMEZONES) | @pytest.mark.parametrize("tz", TS_TIMEZONES) | ||||
@pytest.mark.parametrize("microsecond", [0, 1, 10, 100, 1000, 999999]) | @pytest.mark.parametrize("microsecond", [0, 1, 10, 100, 1000, 999999]) | ||||
def test_timestampwithtimezone_to_datetime(date, tz, microsecond): | def test_timestampwithtimezone_to_datetime(date, tz, microsecond): | ||||
date = date.replace(tzinfo=tz, microsecond=microsecond) | date = date.replace(tzinfo=tz, microsecond=microsecond) | ||||
▲ Show 20 Lines • Show All 897 Lines • Show Last 20 Lines |