Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_model.py
Show First 20 Lines • Show All 813 Lines • ▼ Show 20 Lines | _content_swhid = ExtendedSWHID.from_string( | ||||
"swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2" | "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2" | ||||
) | ) | ||||
_origin_url = "https://forge.softwareheritage.org/source/swh-model.git" | _origin_url = "https://forge.softwareheritage.org/source/swh-model.git" | ||||
_origin_swhid = ExtendedSWHID.from_string( | _origin_swhid = ExtendedSWHID.from_string( | ||||
"swh:1:ori:94a9ed024d3859793618152ea559a168bbcbb5e2" | "swh:1:ori:94a9ed024d3859793618152ea559a168bbcbb5e2" | ||||
) | ) | ||||
_dummy_qualifiers = {"origin": "https://example.com", "lines": "42"} | _dummy_qualifiers = {"origin": "https://example.com", "lines": "42"} | ||||
_common_metadata_fields = dict( | _common_metadata_fields = dict( | ||||
discovery_date=datetime.datetime.now(tz=datetime.timezone.utc), | discovery_date=datetime.datetime( | ||||
2021, 1, 29, 13, 57, 9, tzinfo=datetime.timezone.utc | |||||
), | |||||
authority=_metadata_authority, | authority=_metadata_authority, | ||||
fetcher=_metadata_fetcher, | fetcher=_metadata_fetcher, | ||||
format="json", | format="json", | ||||
metadata=b'{"origin": "https://example.com", "lines": "42"}', | metadata=b'{"origin": "https://example.com", "lines": "42"}', | ||||
) | ) | ||||
def test_metadata_valid(): | def test_metadata_valid(): | ||||
Show All 17 Lines | common_fields = { | ||||
"discovery_date": _common_metadata_fields["discovery_date"], | "discovery_date": _common_metadata_fields["discovery_date"], | ||||
"format": "json", | "format": "json", | ||||
"metadata": b'{"origin": "https://example.com", "lines": "42"}', | "metadata": b'{"origin": "https://example.com", "lines": "42"}', | ||||
} | } | ||||
m = RawExtrinsicMetadata(target=_origin_swhid, **_common_metadata_fields,) | m = RawExtrinsicMetadata(target=_origin_swhid, **_common_metadata_fields,) | ||||
assert m.to_dict() == { | assert m.to_dict() == { | ||||
"target": str(_origin_swhid), | "target": str(_origin_swhid), | ||||
"id": b"\xeck\x9cQ\xf1\x1f\xeb\xde\x85{\x7f\xf0\x83\x9c\x8a\xd5\xfb\x8e2\xef", | |||||
**common_fields, | **common_fields, | ||||
} | } | ||||
assert RawExtrinsicMetadata.from_dict(m.to_dict()) == m | assert RawExtrinsicMetadata.from_dict(m.to_dict()) == m | ||||
m = RawExtrinsicMetadata(target=_content_swhid, **_common_metadata_fields,) | m = RawExtrinsicMetadata(target=_content_swhid, **_common_metadata_fields,) | ||||
assert m.to_dict() == { | assert m.to_dict() == { | ||||
"target": "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | "target": "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | ||||
"id": b"\x8e:_;eb\xe6\xf1Y\xd9\xa5aG[\rt\x89\xa1\x0b\xe4", | |||||
**common_fields, | **common_fields, | ||||
} | } | ||||
assert RawExtrinsicMetadata.from_dict(m.to_dict()) == m | assert RawExtrinsicMetadata.from_dict(m.to_dict()) == m | ||||
hash_hex = "6162" * 10 | hash_hex = "6162" * 10 | ||||
hash_bin = b"ab" * 10 | hash_bin = b"ab" * 10 | ||||
m = RawExtrinsicMetadata( | m = RawExtrinsicMetadata( | ||||
target=_content_swhid, | target=_content_swhid, | ||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
origin="https://example.org/", | origin="https://example.org/", | ||||
snapshot=CoreSWHID(object_type=ObjectType.SNAPSHOT, object_id=hash_bin), | snapshot=CoreSWHID(object_type=ObjectType.SNAPSHOT, object_id=hash_bin), | ||||
release=CoreSWHID(object_type=ObjectType.RELEASE, object_id=hash_bin), | release=CoreSWHID(object_type=ObjectType.RELEASE, object_id=hash_bin), | ||||
revision=CoreSWHID(object_type=ObjectType.REVISION, object_id=hash_bin), | revision=CoreSWHID(object_type=ObjectType.REVISION, object_id=hash_bin), | ||||
path=b"/foo/bar", | path=b"/foo/bar", | ||||
directory=CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=hash_bin), | directory=CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=hash_bin), | ||||
) | ) | ||||
assert m.to_dict() == { | assert m.to_dict() == { | ||||
"target": "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | "target": "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | ||||
"id": b"\x07\xf1aS\xbe\xda\xd2\xf2\xd7\xaf:\xc7\xb7\x91C\x87W\x85R\x19", | |||||
**common_fields, | **common_fields, | ||||
"origin": "https://example.org/", | "origin": "https://example.org/", | ||||
"snapshot": f"swh:1:snp:{hash_hex}", | "snapshot": f"swh:1:snp:{hash_hex}", | ||||
"release": f"swh:1:rel:{hash_hex}", | "release": f"swh:1:rel:{hash_hex}", | ||||
"revision": f"swh:1:rev:{hash_hex}", | "revision": f"swh:1:rev:{hash_hex}", | ||||
"path": b"/foo/bar", | "path": b"/foo/bar", | ||||
"directory": f"swh:1:dir:{hash_hex}", | "directory": f"swh:1:dir:{hash_hex}", | ||||
} | } | ||||
▲ Show 20 Lines • Show All 223 Lines • Show Last 20 Lines |