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 242 Lines • ▼ Show 20 Lines | -----END PGP SIGNATURE-----""" | ||||
} | } | ||||
self.synthetic_revision = { | self.synthetic_revision = { | ||||
"id": _x("b2a7e1260492e344fab3cbf91bc13c91e05426fd"), | "id": _x("b2a7e1260492e344fab3cbf91bc13c91e05426fd"), | ||||
"author": { | "author": { | ||||
"name": b"Software Heritage", | "name": b"Software Heritage", | ||||
"email": b"robot@softwareheritage.org", | "email": b"robot@softwareheritage.org", | ||||
}, | }, | ||||
"date": { | "date": {"timestamp": {"seconds": 1437047495}, "offset_bytes": b"+0000",}, | ||||
"timestamp": {"seconds": 1437047495}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
"type": "tar", | "type": "tar", | ||||
"committer": { | "committer": { | ||||
"name": b"Software Heritage", | "name": b"Software Heritage", | ||||
"email": b"robot@softwareheritage.org", | "email": b"robot@softwareheritage.org", | ||||
}, | }, | ||||
"committer_date": 1437047495, | "committer_date": 1437047495, | ||||
"synthetic": True, | "synthetic": True, | ||||
"parents": [], | "parents": [], | ||||
▲ Show 20 Lines • Show All 249 Lines • ▼ Show 20 Lines | """, | ||||
"synthetic": False, | "synthetic": False, | ||||
} | } | ||||
self.release_negative_utc = { | self.release_negative_utc = { | ||||
"id": _x("97c8d2573a001f88e72d75f596cf86b12b82fd01"), | "id": _x("97c8d2573a001f88e72d75f596cf86b12b82fd01"), | ||||
"name": b"20081029", | "name": b"20081029", | ||||
"target": _x("54e9abca4c77421e2921f5f156c9fe4a9f7441c7"), | "target": _x("54e9abca4c77421e2921f5f156c9fe4a9f7441c7"), | ||||
"target_type": "revision", | "target_type": "revision", | ||||
"date": { | "date": {"timestamp": {"seconds": 1225281976}, "offset_bytes": b"-0000",}, | ||||
"timestamp": {"seconds": 1225281976}, | |||||
"offset": 0, | |||||
"negative_utc": True, | |||||
}, | |||||
"author": {"name": b"Otavio Salvador", "email": b"otavio@debian.org",}, | "author": {"name": b"Otavio Salvador", "email": b"otavio@debian.org",}, | ||||
"synthetic": False, | "synthetic": False, | ||||
"message": b"tagging version 20081029\n\nr56558\n", | "message": b"tagging version 20081029\n\nr56558\n", | ||||
} | } | ||||
self.release_newline_in_author = { | self.release_newline_in_author = { | ||||
"author": { | "author": { | ||||
"email": b"esycat@gmail.com", | "email": b"esycat@gmail.com", | ||||
"fullname": b"Eugene Janusov\n<esycat@gmail.com>", | "fullname": b"Eugene Janusov\n<esycat@gmail.com>", | ||||
"name": b"Eugene Janusov\n", | "name": b"Eugene Janusov\n", | ||||
}, | }, | ||||
"date": { | "date": { | ||||
"negative_utc": None, | "offset_bytes": b"+1000", | ||||
"offset": 600, | |||||
"timestamp": {"microseconds": 0, "seconds": 1377480558,}, | "timestamp": {"microseconds": 0, "seconds": 1377480558,}, | ||||
}, | }, | ||||
"id": _x("5c98f559d034162de22d3ebeb95433e6f8885231"), | "id": _x("5c98f559d034162de22d3ebeb95433e6f8885231"), | ||||
"message": b"Release of v0.3.2.", | "message": b"Release of v0.3.2.", | ||||
"name": b"0.3.2", | "name": b"0.3.2", | ||||
"synthetic": False, | "synthetic": False, | ||||
"target": _x("c06aa3d93b78a2865c4935170030f8c2d7396fd3"), | "target": _x("c06aa3d93b78a2865c4935170030f8c2d7396fd3"), | ||||
"target_type": "revision", | "target_type": "revision", | ||||
▲ Show 20 Lines • Show All 445 Lines • ▼ Show 20 Lines | |||||
class OriginIdentifier(unittest.TestCase): | class OriginIdentifier(unittest.TestCase): | ||||
def test_content_identifier(self): | def test_content_identifier(self): | ||||
self.assertEqual( | self.assertEqual( | ||||
Origin.from_dict(origin_example).id, | Origin.from_dict(origin_example).id, | ||||
_x("b63a575fe3faab7692c9f38fb09d4bb45651bb0f"), | _x("b63a575fe3faab7692c9f38fb09d4bb45651bb0f"), | ||||
) | ) | ||||
# Format: [ | |||||
# ( | |||||
# input1, | |||||
# expected_output1, | |||||
# ), | |||||
# ( | |||||
# input2, | |||||
# expected_output2, | |||||
# ), | |||||
# ... | |||||
# ] | |||||
TS_DICTS = [ | TS_DICTS = [ | ||||
# with current input dict format (offset_bytes) | |||||
( | ( | ||||
{"timestamp": 12345, "offset": 0}, | {"timestamp": 12345, "offset_bytes": b"+0000"}, | ||||
{ | { | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | "timestamp": {"seconds": 12345, "microseconds": 0}, | ||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | "offset": 0, | ||||
"negative_utc": False, | "negative_utc": False, | ||||
"offset_bytes": b"+0000", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
{"timestamp": 12345, "offset": 0, "negative_utc": False}, | {"timestamp": 12345, "offset_bytes": b"-0000"}, | ||||
{ | { | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | "timestamp": {"seconds": 12345, "microseconds": 0}, | ||||
"offset_bytes": b"-0000", | |||||
"offset": 0, | "offset": 0, | ||||
"negative_utc": True, | |||||
}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "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": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"-0200", | |||||
"offset": -120, | |||||
"negative_utc": False, | "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): | |||||
( | |||||
{"timestamp": 12345, "offset": 0}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"+0000", | "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}, | "timestamp": {"seconds": 12345, "microseconds": 0}, | ||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | "offset": 0, | ||||
"negative_utc": False, | "negative_utc": False, | ||||
}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "offset": 0, "negative_utc": False}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset_bytes": b"+0000", | "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}, | "timestamp": {"seconds": 12345, "microseconds": 0}, | ||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | "offset": 0, | ||||
"negative_utc": False, | "negative_utc": False, | ||||
"offset_bytes": b"+0000", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
{"timestamp": {"seconds": 12345}, "offset": 0, "negative_utc": None}, | {"timestamp": {"seconds": 12345}, "offset": 0, "negative_utc": None}, | ||||
{ | { | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | "timestamp": {"seconds": 12345, "microseconds": 0}, | ||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | "offset": 0, | ||||
"negative_utc": False, | "negative_utc": False, | ||||
"offset_bytes": b"+0000", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
{ | { | ||||
"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}, | "timestamp": {"seconds": 12345, "microseconds": 0}, | ||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | "offset": 0, | ||||
"negative_utc": False, | "negative_utc": False, | ||||
"offset_bytes": b"+0000", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
{ | { | ||||
"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": 0, | "offset": 0, | ||||
"negative_utc": False, | "negative_utc": False, | ||||
"offset_bytes": b"+0000", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
{"timestamp": 12345, "offset": 0, "negative_utc": True}, | {"timestamp": 12345, "offset": 0, "negative_utc": True}, | ||||
{ | { | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | "timestamp": {"seconds": 12345, "microseconds": 0}, | ||||
"offset_bytes": b"-0000", | |||||
"offset": 0, | "offset": 0, | ||||
"negative_utc": True, | "negative_utc": True, | ||||
"offset_bytes": b"-0000", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
{"timestamp": 12345, "offset": 0, "negative_utc": None}, | {"timestamp": 12345, "offset": 0, "negative_utc": None}, | ||||
{ | { | ||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | "timestamp": {"seconds": 12345, "microseconds": 0}, | ||||
"offset_bytes": b"+0000", | |||||
"offset": 0, | "offset": 0, | ||||
"negative_utc": False, | "negative_utc": False, | ||||
"offset_bytes": b"+0000", | |||||
}, | }, | ||||
), | ), | ||||
] | ] | ||||
@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 29 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": offset, | "offset": offset, | ||||
"negative_utc": False, | "negative_utc": False, | ||||
"offset_bytes": offset_bytes, | |||||
} | } | ||||
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 |