Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_identifiers.py
Show First 20 Lines • Show All 212 Lines • ▼ Show 20 Lines | "entries": [ | ||||
"type": "file", | "type": "file", | ||||
"perms": 16384, | "perms": 16384, | ||||
"name": b"order0", | "name": b"order0", | ||||
"target": _x("bbe960a25ea311d21d40669e93df2003ba9b90a2"), | "target": _x("bbe960a25ea311d21d40669e93df2003ba9b90a2"), | ||||
}, | }, | ||||
], | ], | ||||
} | } | ||||
dummy_qualifiers = {"origin": "https://example.com", "lines": "42"} | |||||
class DirectoryIdentifier(unittest.TestCase): | class DirectoryIdentifier(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
self.directory = directory_example | self.directory = directory_example | ||||
self.empty_directory = { | self.empty_directory = { | ||||
"id": "4b825dc642cb6eb9a060e54bf8d69288fbee4904", | "id": "4b825dc642cb6eb9a060e54bf8d69288fbee4904", | ||||
"entries": [], | "entries": [], | ||||
▲ Show 20 Lines • Show All 609 Lines • ▼ Show 20 Lines | class SnapshotIdentifier(unittest.TestCase): | ||||
def test_swhid_wrong_input(self): | def test_swhid_wrong_input(self): | ||||
_snapshot_id = "notahash4bc0bf3d81436bf980b46e98bd338453" | _snapshot_id = "notahash4bc0bf3d81436bf980b46e98bd338453" | ||||
_snapshot = {"id": _snapshot_id} | _snapshot = {"id": _snapshot_id} | ||||
for _type, _hash in [ | for _type, _hash in [ | ||||
(SNAPSHOT, _snapshot_id), | (SNAPSHOT, _snapshot_id), | ||||
(SNAPSHOT, _snapshot), | (SNAPSHOT, _snapshot), | ||||
("foo", ""), | ("lines", "42"), | ||||
]: | ]: | ||||
with self.assertRaises(ValidationError): | with self.assertRaises(ValidationError): | ||||
identifiers.swhid(_type, _hash) | identifiers.swhid(_type, _hash) | ||||
def test_parse_swhid(self): | def test_parse_swhid(self): | ||||
for swhid, _type, _version, _hash in [ | for swhid, _type, _version, _hash in [ | ||||
( | ( | ||||
"swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | ||||
▲ Show 20 Lines • Show All 257 Lines • ▼ Show 20 Lines | |||||
def test_swhid_hash(): | def test_swhid_hash(): | ||||
object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | ||||
assert hash(SWHID(object_type="directory", object_id=object_id)) == hash( | assert hash(SWHID(object_type="directory", object_id=object_id)) == hash( | ||||
SWHID(object_type="directory", object_id=object_id) | SWHID(object_type="directory", object_id=object_id) | ||||
) | ) | ||||
assert hash( | assert hash( | ||||
SWHID( | SWHID(object_type="directory", object_id=object_id, metadata=dummy_qualifiers,) | ||||
object_type="directory", | |||||
object_id=object_id, | |||||
metadata={"foo": "bar", "baz": "qux"}, | |||||
) | |||||
) == hash( | ) == hash( | ||||
SWHID( | SWHID(object_type="directory", object_id=object_id, metadata=dummy_qualifiers,) | ||||
object_type="directory", | |||||
object_id=object_id, | |||||
metadata={"foo": "bar", "baz": "qux"}, | |||||
) | |||||
) | ) | ||||
# Different order of the dictionary, so the underlying order of the tuple in | # Different order of the dictionary, so the underlying order of the tuple in | ||||
# ImmutableDict is different. | # ImmutableDict is different. | ||||
assert hash( | assert hash( | ||||
SWHID( | SWHID( | ||||
object_type="directory", | object_type="directory", | ||||
object_id=object_id, | object_id=object_id, | ||||
metadata={"foo": "bar", "baz": "qux"}, | metadata={"origin": "https://example.com", "lines": "42"}, | ||||
) | ) | ||||
) == hash( | ) == hash( | ||||
SWHID( | SWHID( | ||||
object_type="directory", | object_type="directory", | ||||
object_id=object_id, | object_id=object_id, | ||||
metadata={"baz": "qux", "foo": "bar"}, | metadata={"lines": "42", "origin": "https://example.com"}, | ||||
) | ) | ||||
) | ) | ||||
def test_swhid_eq(): | def test_swhid_eq(): | ||||
object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | ||||
assert SWHID(object_type="directory", object_id=object_id) == SWHID( | assert SWHID(object_type="directory", object_id=object_id) == SWHID( | ||||
object_type="directory", object_id=object_id | object_type="directory", object_id=object_id | ||||
) | ) | ||||
assert SWHID( | assert SWHID( | ||||
object_type="directory", | object_type="directory", object_id=object_id, metadata=dummy_qualifiers, | ||||
object_id=object_id, | ) == SWHID(object_type="directory", object_id=object_id, metadata=dummy_qualifiers,) | ||||
metadata={"foo": "bar", "baz": "qux"}, | |||||
) == SWHID( | |||||
object_type="directory", | |||||
object_id=object_id, | |||||
metadata={"foo": "bar", "baz": "qux"}, | |||||
) | |||||
assert SWHID( | assert SWHID( | ||||
object_type="directory", | object_type="directory", object_id=object_id, metadata=dummy_qualifiers, | ||||
object_id=object_id, | ) == SWHID(object_type="directory", object_id=object_id, metadata=dummy_qualifiers,) | ||||
metadata={"foo": "bar", "baz": "qux"}, | |||||
) == SWHID( | |||||
object_type="directory", | |||||
object_id=object_id, | |||||
metadata={"baz": "qux", "foo": "bar"}, | |||||
) |