Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_identifiers.py
Show First 20 Lines • Show All 757 Lines • ▼ Show 20 Lines | def test_unresolved_force(self): | ||||
) | ) | ||||
def test_all_types(self): | def test_all_types(self): | ||||
self.assertEqual( | self.assertEqual( | ||||
identifiers.snapshot_identifier(remove_id(self.all_types)), | identifiers.snapshot_identifier(remove_id(self.all_types)), | ||||
identifiers.identifier_to_str(self.all_types["id"]), | identifiers.identifier_to_str(self.all_types["id"]), | ||||
) | ) | ||||
class OriginIdentifier(unittest.TestCase): | |||||
def setUp(self): | |||||
self.origin = { | |||||
"url": "https://github.com/torvalds/linux", | |||||
} | |||||
def test_content_identifier(self): | |||||
self.assertEqual( | |||||
identifiers.origin_identifier(self.origin), | |||||
"b63a575fe3faab7692c9f38fb09d4bb45651bb0f", | |||||
) | |||||
TS_DICTS = [ | |||||
( | |||||
{"timestamp": 12345, "offset": 0}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "offset": 0, "negative_utc": False}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "offset": 0, "negative_utc": False}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "offset": 0, "negative_utc": None}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{"timestamp": {"seconds": 12345}, "offset": 0, "negative_utc": None}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": None, | |||||
}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 100}, | |||||
"offset": 0, | |||||
"negative_utc": None, | |||||
}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 100}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "offset": 0, "negative_utc": True}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": True, | |||||
}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "offset": 0, "negative_utc": None}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
] | |||||
@pytest.mark.parametrize("dict_input,expected", TS_DICTS) | |||||
def test_normalize_timestamp_dict(dict_input, expected): | |||||
assert normalize_timestamp(dict_input) == expected | |||||
TS_DICTS_INVALID_TIMESTAMP = [ | |||||
{"timestamp": 1.2, "offset": 0}, | |||||
{"timestamp": "1", "offset": 0}, | |||||
# these below should really also trigger a ValueError... | |||||
# {"timestamp": {"seconds": "1"}, "offset": 0}, | |||||
# {"timestamp": {"seconds": 1.2}, "offset": 0}, | |||||
# {"timestamp": {"seconds": 1.2}, "offset": 0}, | |||||
] | |||||
@pytest.mark.parametrize("dict_input", TS_DICTS_INVALID_TIMESTAMP) | |||||
def test_normalize_timestamp_dict_invalid_timestamp(dict_input): | |||||
with pytest.raises(ValueError, match="non-integer timestamp"): | |||||
normalize_timestamp(dict_input) | |||||
class TestSwhid(unittest.TestCase): | |||||
def test_swhid(self): | def test_swhid(self): | ||||
_snapshot_id = _x("c7c108084bc0bf3d81436bf980b46e98bd338453") | _snapshot_id = _x("c7c108084bc0bf3d81436bf980b46e98bd338453") | ||||
_release_id = "22ece559cc7cc2364edc5e5593d63ae8bd229f9f" | _release_id = "22ece559cc7cc2364edc5e5593d63ae8bd229f9f" | ||||
_revision_id = "309cf2674ee7a0749978cf8265ab91a60aea0f7d" | _revision_id = "309cf2674ee7a0749978cf8265ab91a60aea0f7d" | ||||
_directory_id = "d198bc9d7a6bcf6db04f476d29314f157507d505" | _directory_id = "d198bc9d7a6bcf6db04f476d29314f157507d505" | ||||
_content_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | _content_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | ||||
_snapshot = {"id": _snapshot_id} | _snapshot = {"id": _snapshot_id} | ||||
_release = {"id": _release_id} | _release = {"id": _release_id} | ||||
▲ Show 20 Lines • Show All 176 Lines • ▼ Show 20 Lines | def test_parse_swhid(self): | ||||
"scheme_version": _version, | "scheme_version": _version, | ||||
"object_type": _type, | "object_type": _type, | ||||
"object_id": _hash, | "object_id": _hash, | ||||
"metadata": _metadata, | "metadata": _metadata, | ||||
}, | }, | ||||
) | ) | ||||
class OriginIdentifier(unittest.TestCase): | |||||
def setUp(self): | |||||
self.origin = { | |||||
"url": "https://github.com/torvalds/linux", | |||||
} | |||||
def test_content_identifier(self): | |||||
self.assertEqual( | |||||
identifiers.origin_identifier(self.origin), | |||||
"b63a575fe3faab7692c9f38fb09d4bb45651bb0f", | |||||
) | |||||
TS_DICTS = [ | |||||
( | |||||
{"timestamp": 12345, "offset": 0}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "offset": 0, "negative_utc": False}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "offset": 0, "negative_utc": False}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "offset": 0, "negative_utc": None}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{"timestamp": {"seconds": 12345}, "offset": 0, "negative_utc": None}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": None, | |||||
}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 100}, | |||||
"offset": 0, | |||||
"negative_utc": None, | |||||
}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 100}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "offset": 0, "negative_utc": True}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": True, | |||||
}, | |||||
), | |||||
( | |||||
{"timestamp": 12345, "offset": 0, "negative_utc": None}, | |||||
{ | |||||
"timestamp": {"seconds": 12345, "microseconds": 0}, | |||||
"offset": 0, | |||||
"negative_utc": False, | |||||
}, | |||||
), | |||||
] | |||||
@pytest.mark.parametrize("dict_input,expected", TS_DICTS) | |||||
def test_normalize_timestamp_dict(dict_input, expected): | |||||
assert normalize_timestamp(dict_input) == expected | |||||
TS_DICTS_INVALID_TIMESTAMP = [ | |||||
{"timestamp": 1.2, "offset": 0}, | |||||
{"timestamp": "1", "offset": 0}, | |||||
# these below should really also trigger a ValueError... | |||||
# {"timestamp": {"seconds": "1"}, "offset": 0}, | |||||
# {"timestamp": {"seconds": 1.2}, "offset": 0}, | |||||
# {"timestamp": {"seconds": 1.2}, "offset": 0}, | |||||
] | |||||
@pytest.mark.parametrize("dict_input", TS_DICTS_INVALID_TIMESTAMP) | |||||
def test_normalize_timestamp_dict_invalid_timestamp(dict_input): | |||||
with pytest.raises(ValueError, match="non-integer timestamp"): | |||||
normalize_timestamp(dict_input) | |||||
@pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||
"invalid_swhid", | "invalid_swhid", | ||||
[ | [ | ||||
"swh:1:cnt", | "swh:1:cnt", | ||||
"swh:1:", | "swh:1:", | ||||
"swh:", | "swh:", | ||||
"swh:1:cnt:", | "swh:1:cnt:", | ||||
"foo:1:cnt:abc8bc9d7a6bcf6db04f476d29314f157507d505", | "foo:1:cnt:abc8bc9d7a6bcf6db04f476d29314f157507d505", | ||||
▲ Show 20 Lines • Show All 99 Lines • Show Last 20 Lines |