Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_identifiers.py
Show All 15 Lines | from swh.model.model import ( | ||||
Content, | Content, | ||||
Directory, | Directory, | ||||
ExtID, | ExtID, | ||||
Origin, | Origin, | ||||
RawExtrinsicMetadata, | RawExtrinsicMetadata, | ||||
Release, | Release, | ||||
Revision, | Revision, | ||||
Snapshot, | Snapshot, | ||||
Timestamp, | |||||
TimestampWithTimezone, | TimestampWithTimezone, | ||||
) | ) | ||||
def remove_id(d: Dict) -> Dict: | def remove_id(d: Dict) -> Dict: | ||||
"""Returns a (shallow) copy of a dict with the 'id' key removed.""" | """Returns a (shallow) copy of a dict with the 'id' key removed.""" | ||||
d = d.copy() | d = d.copy() | ||||
if "id" in d: | if "id" in d: | ||||
▲ Show 20 Lines • Show All 998 Lines • ▼ Show 20 Lines | |||||
# expected_output2, | # expected_output2, | ||||
# ), | # ), | ||||
# ... | # ... | ||||
# ] | # ] | ||||
TS_DICTS = [ | TS_DICTS = [ | ||||
# with current input dict format (offset_bytes) | # with current input dict format (offset_bytes) | ||||
( | ( | ||||
{"timestamp": 12345, "offset_bytes": b"+0000"}, | {"timestamp": 12345, "offset_bytes": b"+0000"}, | ||||
{ | {"timestamp": {"seconds": 12345, "microseconds": 0}, "offset_bytes": b"+0000",}, | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | ), | ||||
( | ( | ||||
{"timestamp": 12345, "offset_bytes": b"-0000"}, | {"timestamp": 12345, "offset_bytes": b"-0000"}, | ||||
{ | {"timestamp": {"seconds": 12345, "microseconds": 0}, "offset_bytes": b"-0000",}, | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"-0000", | |||||
"offset": 0, | |||||
"negative_utc": True, | |||||
}, | |||||
), | ), | ||||
( | ( | ||||
{"timestamp": 12345, "offset_bytes": b"+0200"}, | {"timestamp": 12345, "offset_bytes": b"+0200"}, | ||||
{ | {"timestamp": {"seconds": 12345, "microseconds": 0}, "offset_bytes": b"+0200",}, | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"+0200", | |||||
"offset": 120, | |||||
"negative_utc": False, | |||||
}, | |||||
), | ), | ||||
( | ( | ||||
{"timestamp": 12345, "offset_bytes": b"-0200"}, | {"timestamp": 12345, "offset_bytes": b"-0200"}, | ||||
{"timestamp": {"seconds": 12345, "microseconds": 0}, "offset_bytes": b"-0200",}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "offset_bytes": b"--700"}, | |||||
ardumont: --700? | |||||
Done Inline ActionsYes. I kid you not, there are a few hundred commits with that specific timezone: T75#71479 Dulwich had issues with it as well. https://bugs.launchpad.net/dulwich/+bug/697828 vlorentz: Yes. I kid you not, there are a few hundred commits with that specific timezone: T75#71479… | |||||
{"timestamp": {"seconds": 12345, "microseconds": 0}, "offset_bytes": b"--700",}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "offset_bytes": b"1234567"}, | |||||
{ | { | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | "timestamp": {"seconds": 12345, "microseconds": 0}, | ||||
"offset_bytes": b"-0200", | "offset_bytes": b"1234567", | ||||
"offset": -120, | |||||
"negative_utc": False, | |||||
}, | }, | ||||
), | ), | ||||
# not working yet: | |||||
# ( | |||||
# {"timestamp": 12345, "offset_bytes": b"--700"}, | |||||
# { | |||||
# "timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
# "offset_bytes": b"--700", | |||||
# "offset": 0, | |||||
# "negative_utc": False, | |||||
# }, | |||||
# ), | |||||
# ( | |||||
# {"timestamp": 12345, "offset_bytes": b"1234567"}, | |||||
# { | |||||
# "timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
# "offset_bytes": b"1234567", | |||||
# "offset": 0, | |||||
# "negative_utc": False, | |||||
# }, | |||||
# ), | |||||
# with old-style input dicts (numeric offset + optional negative_utc): | # with old-style input dicts (numeric offset + optional negative_utc): | ||||
( | ( | ||||
{"timestamp": 12345, "offset": 0}, | {"timestamp": 12345, "offset": 0}, | ||||
{ | {"timestamp": {"seconds": 12345, "microseconds": 0}, "offset_bytes": b"+0000",}, | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | ), | ||||
( | ( | ||||
{"timestamp": 12345, "offset": 0, "negative_utc": False}, | {"timestamp": 12345, "offset": 0, "negative_utc": False}, | ||||
{ | {"timestamp": {"seconds": 12345, "microseconds": 0}, "offset_bytes": b"+0000",}, | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | ), | ||||
( | ( | ||||
{"timestamp": 12345, "offset": 0, "negative_utc": False}, | {"timestamp": 12345, "offset": 0, "negative_utc": False}, | ||||
{ | {"timestamp": {"seconds": 12345, "microseconds": 0}, "offset_bytes": b"+0000",}, | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | ), | ||||
( | ( | ||||
{"timestamp": 12345, "offset": 0, "negative_utc": None}, | {"timestamp": 12345, "offset": 0, "negative_utc": None}, | ||||
{ | {"timestamp": {"seconds": 12345, "microseconds": 0}, "offset_bytes": b"+0000",}, | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | ), | ||||
( | ( | ||||
{"timestamp": {"seconds": 12345}, "offset": 0, "negative_utc": None}, | {"timestamp": {"seconds": 12345}, "offset": 0, "negative_utc": None}, | ||||
{ | {"timestamp": {"seconds": 12345, "microseconds": 0}, "offset_bytes": b"+0000",}, | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | ), | ||||
( | ( | ||||
{ | { | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | "timestamp": {"seconds": 12345, "microseconds": 0}, | ||||
"offset": 0, | "offset": 0, | ||||
"negative_utc": None, | "negative_utc": None, | ||||
}, | }, | ||||
{ | {"timestamp": {"seconds": 12345, "microseconds": 0}, "offset_bytes": b"+0000",}, | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | ), | ||||
( | ( | ||||
{ | { | ||||
"timestamp": {"seconds": 12345, "microseconds": 100}, | "timestamp": {"seconds": 12345, "microseconds": 100}, | ||||
"offset": 0, | "offset": 0, | ||||
"negative_utc": None, | "negative_utc": None, | ||||
}, | }, | ||||
{ | { | ||||
"timestamp": {"seconds": 12345, "microseconds": 100}, | "timestamp": {"seconds": 12345, "microseconds": 100}, | ||||
"offset_bytes": b"+0000", | "offset_bytes": b"+0000", | ||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
{"timestamp": 12345, "offset": 0, "negative_utc": True}, | {"timestamp": 12345, "offset": 0, "negative_utc": True}, | ||||
{ | {"timestamp": {"seconds": 12345, "microseconds": 0}, "offset_bytes": b"-0000",}, | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"-0000", | |||||
"offset": 0, | |||||
"negative_utc": True, | |||||
}, | |||||
), | ), | ||||
( | ( | ||||
{"timestamp": 12345, "offset": 0, "negative_utc": None}, | {"timestamp": 12345, "offset": 0, "negative_utc": None}, | ||||
{ | {"timestamp": {"seconds": 12345, "microseconds": 0}, "offset_bytes": b"+0000",}, | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | ), | ||||
] | ] | ||||
@pytest.mark.parametrize("dict_input,expected", TS_DICTS) | @pytest.mark.parametrize("dict_input,expected", TS_DICTS) | ||||
def test_normalize_timestamp_dict(dict_input, expected): | def test_normalize_timestamp_dict(dict_input, expected): | ||||
assert TimestampWithTimezone.from_dict(dict_input).to_dict() == expected | assert TimestampWithTimezone.from_dict(dict_input).to_dict() == expected | ||||
def test_timestampwithtimezone_init(): | |||||
ts = Timestamp(seconds=1234567, microseconds=0) | |||||
tstz = TimestampWithTimezone( | |||||
timestamp=ts, offset=120, negative_utc=False, offset_bytes=b"+0200" | |||||
) | |||||
assert tstz.timestamp == ts | |||||
assert tstz.offset == 120 | |||||
assert tstz.negative_utc is False | |||||
assert tstz.offset_bytes == b"+0200" | |||||
assert tstz == TimestampWithTimezone(timestamp=ts, offset=120, negative_utc=False) | |||||
assert tstz == TimestampWithTimezone(timestamp=ts, offset_bytes=b"+0200") | |||||
assert tstz != TimestampWithTimezone(timestamp=ts, offset_bytes=b"+0100") | |||||
tstz = TimestampWithTimezone( | |||||
timestamp=ts, offset=0, negative_utc=True, offset_bytes=b"-0000" | |||||
) | |||||
assert tstz.timestamp == ts | |||||
assert tstz.offset == 0 | |||||
assert tstz.negative_utc is True | |||||
assert tstz.offset_bytes == b"-0000" | |||||
assert tstz == TimestampWithTimezone(timestamp=ts, offset=0, negative_utc=True) | |||||
assert tstz == TimestampWithTimezone(timestamp=ts, offset_bytes=b"-0000") | |||||
assert tstz != TimestampWithTimezone(timestamp=ts, offset_bytes=b"+0000") | |||||
TS_DICTS_INVALID_TIMESTAMP = [ | TS_DICTS_INVALID_TIMESTAMP = [ | ||||
{"timestamp": 1.2, "offset": 0}, | {"timestamp": 1.2, "offset": 0}, | ||||
{"timestamp": "1", "offset": 0}, | {"timestamp": "1", "offset": 0}, | ||||
# these below should really also trigger a ValueError... | # these below should really also trigger a ValueError... | ||||
# {"timestamp": {"seconds": "1"}, "offset": 0}, | # {"timestamp": {"seconds": "1"}, "offset": 0}, | ||||
# {"timestamp": {"seconds": 1.2}, "offset": 0}, | # {"timestamp": {"seconds": 1.2}, "offset": 0}, | ||||
# {"timestamp": {"seconds": 1.2}, "offset": 0}, | # {"timestamp": {"seconds": 1.2}, "offset": 0}, | ||||
] | ] | ||||
Show All 30 Lines | |||||
@pytest.mark.parametrize("microsecond", [0, 1, 10, 100, 1000, 999999]) | @pytest.mark.parametrize("microsecond", [0, 1, 10, 100, 1000, 999999]) | ||||
def test_normalize_timestamp_datetime( | def test_normalize_timestamp_datetime( | ||||
date, seconds, tz, offset, offset_bytes, microsecond | date, seconds, tz, offset, offset_bytes, microsecond | ||||
): | ): | ||||
date = date.astimezone(tz).replace(microsecond=microsecond) | date = date.astimezone(tz).replace(microsecond=microsecond) | ||||
assert TimestampWithTimezone.from_dict(date).to_dict() == { | assert TimestampWithTimezone.from_dict(date).to_dict() == { | ||||
"timestamp": {"seconds": seconds, "microseconds": microsecond}, | "timestamp": {"seconds": seconds, "microseconds": microsecond}, | ||||
"offset_bytes": offset_bytes, | "offset_bytes": offset_bytes, | ||||
"offset": offset, | |||||
"negative_utc": False, | |||||
} | } | ||||
def test_extid_identifier_bwcompat(): | def test_extid_identifier_bwcompat(): | ||||
extid_dict = { | extid_dict = { | ||||
"extid_type": "test-type", | "extid_type": "test-type", | ||||
"extid": b"extid", | "extid": b"extid", | ||||
"target": "swh:1:dir:" + "00" * 20, | "target": "swh:1:dir:" + "00" * 20, | ||||
Show All 15 Lines |
--700?