Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_model.py
Show All 9 Lines | |||||
from attrs_strict import AttributeTypeError | from attrs_strict import AttributeTypeError | ||||
from hypothesis import given | from hypothesis import given | ||||
from hypothesis.strategies import binary | from hypothesis.strategies import binary | ||||
import pytest | import pytest | ||||
from swh.model.hashutil import MultiHash, hash_to_bytes, hash_to_hex | from swh.model.hashutil import MultiHash, hash_to_bytes, hash_to_hex | ||||
import swh.model.hypothesis_strategies as strategies | import swh.model.hypothesis_strategies as strategies | ||||
from swh.model.identifiers import ( | from swh.model.identifiers import ( | ||||
SWHID, | CoreSWHID, | ||||
ExtendedSWHID, | ExtendedSWHID, | ||||
ObjectType, | |||||
content_identifier, | content_identifier, | ||||
directory_identifier, | directory_identifier, | ||||
origin_identifier, | origin_identifier, | ||||
release_identifier, | release_identifier, | ||||
revision_identifier, | revision_identifier, | ||||
snapshot_identifier, | snapshot_identifier, | ||||
) | ) | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
Show All 20 Lines | from swh.model.tests.test_identifiers import ( | ||||
content_example, | content_example, | ||||
directory_example, | directory_example, | ||||
origin_example, | origin_example, | ||||
release_example, | release_example, | ||||
revision_example, | revision_example, | ||||
snapshot_example, | snapshot_example, | ||||
) | ) | ||||
EXAMPLE_HASH = hash_to_bytes("94a9ed024d3859793618152ea559a168bbcbb5e2") | |||||
@given(strategies.objects()) | @given(strategies.objects()) | ||||
def test_todict_inverse_fromdict(objtype_and_obj): | def test_todict_inverse_fromdict(objtype_and_obj): | ||||
(obj_type, obj) = objtype_and_obj | (obj_type, obj) = objtype_and_obj | ||||
if obj_type in ("origin", "origin_visit"): | if obj_type in ("origin", "origin_visit"): | ||||
return | return | ||||
▲ Show 20 Lines • Show All 792 Lines • ▼ Show 20 Lines | def test_metadata_to_dict(): | ||||
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", | ||||
**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_bin = b"ab" * 10 | |||||
m = RawExtrinsicMetadata( | |||||
target=_content_swhid, | |||||
**_common_metadata_fields, | |||||
origin="https://example.org/", | |||||
snapshot=CoreSWHID(object_type=ObjectType.SNAPSHOT, object_id=hash_bin), | |||||
release=CoreSWHID(object_type=ObjectType.RELEASE, object_id=hash_bin), | |||||
revision=CoreSWHID(object_type=ObjectType.REVISION, object_id=hash_bin), | |||||
path=b"/foo/bar", | |||||
directory=CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=hash_bin), | |||||
) | |||||
assert m.to_dict() == { | |||||
"target": "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
**common_fields, | |||||
"origin": "https://example.org/", | |||||
"snapshot": f"swh:1:snp:{hash_hex}", | |||||
"release": f"swh:1:rel:{hash_hex}", | |||||
"revision": f"swh:1:rev:{hash_hex}", | |||||
"path": b"/foo/bar", | |||||
"directory": f"swh:1:dir:{hash_hex}", | |||||
} | |||||
assert RawExtrinsicMetadata.from_dict(m.to_dict()) == m | |||||
def test_metadata_invalid_target(): | def test_metadata_invalid_target(): | ||||
"""Checks various invalid values for the 'target' field.""" | """Checks various invalid values for the 'target' field.""" | ||||
# SWHID passed as string instead of SWHID | # SWHID passed as string instead of SWHID | ||||
with pytest.raises(ValueError, match="target must be.*ExtendedSWHID"): | with pytest.raises(ValueError, match="target must be.*ExtendedSWHID"): | ||||
RawExtrinsicMetadata( | RawExtrinsicMetadata( | ||||
target="swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | target="swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | ||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | def test_metadata_validate_context_snapshot(): | ||||
"""Checks validation of RawExtrinsicMetadata.snapshot.""" | """Checks validation of RawExtrinsicMetadata.snapshot.""" | ||||
# Origins can't have a 'snapshot' context | # Origins can't have a 'snapshot' context | ||||
with pytest.raises( | with pytest.raises( | ||||
ValueError, match="Unexpected 'snapshot' context for origin object" | ValueError, match="Unexpected 'snapshot' context for origin object" | ||||
): | ): | ||||
RawExtrinsicMetadata( | RawExtrinsicMetadata( | ||||
target=_origin_swhid, | target=_origin_swhid, | ||||
snapshot=SWHID( | snapshot=CoreSWHID( | ||||
object_type="snapshot", | object_type=ObjectType.SNAPSHOT, object_id=EXAMPLE_HASH, | ||||
object_id="94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
), | ), | ||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
) | ) | ||||
# but content can | # but content can | ||||
RawExtrinsicMetadata( | RawExtrinsicMetadata( | ||||
target=_content_swhid, | target=_content_swhid, | ||||
snapshot=SWHID( | snapshot=CoreSWHID(object_type=ObjectType.SNAPSHOT, object_id=EXAMPLE_HASH), | ||||
object_type="snapshot", object_id="94a9ed024d3859793618152ea559a168bbcbb5e2" | |||||
), | |||||
**_common_metadata_fields, | |||||
) | |||||
# Non-core SWHID | |||||
with pytest.raises(ValueError, match="Expected core SWHID"): | |||||
RawExtrinsicMetadata( | |||||
target=_content_swhid, | |||||
snapshot=SWHID( | |||||
object_type="snapshot", | |||||
object_id="94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
metadata=_dummy_qualifiers, | |||||
), | |||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
) | ) | ||||
# SWHID type doesn't match the expected type of this context key | # SWHID type doesn't match the expected type of this context key | ||||
with pytest.raises( | with pytest.raises( | ||||
ValueError, match="Expected SWHID type 'snapshot', got 'content'" | ValueError, match="Expected SWHID type 'snapshot', got 'content'" | ||||
): | ): | ||||
RawExtrinsicMetadata( | RawExtrinsicMetadata( | ||||
target=_content_swhid, | target=_content_swhid, | ||||
snapshot=SWHID( | snapshot=CoreSWHID(object_type=ObjectType.CONTENT, object_id=EXAMPLE_HASH,), | ||||
object_type="content", | |||||
object_id="94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
), | |||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
) | ) | ||||
def test_metadata_validate_context_release(): | def test_metadata_validate_context_release(): | ||||
"""Checks validation of RawExtrinsicMetadata.release.""" | """Checks validation of RawExtrinsicMetadata.release.""" | ||||
# Origins can't have a 'release' context | # Origins can't have a 'release' context | ||||
with pytest.raises( | with pytest.raises( | ||||
ValueError, match="Unexpected 'release' context for origin object" | ValueError, match="Unexpected 'release' context for origin object" | ||||
): | ): | ||||
RawExtrinsicMetadata( | RawExtrinsicMetadata( | ||||
target=_origin_swhid, | target=_origin_swhid, | ||||
release=SWHID( | release=CoreSWHID(object_type=ObjectType.RELEASE, object_id=EXAMPLE_HASH,), | ||||
object_type="release", | |||||
object_id="94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
), | |||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
) | ) | ||||
# but content can | # but content can | ||||
RawExtrinsicMetadata( | RawExtrinsicMetadata( | ||||
target=_content_swhid, | target=_content_swhid, | ||||
release=SWHID( | release=CoreSWHID(object_type=ObjectType.RELEASE, object_id=EXAMPLE_HASH), | ||||
object_type="release", object_id="94a9ed024d3859793618152ea559a168bbcbb5e2" | |||||
), | |||||
**_common_metadata_fields, | |||||
) | |||||
# Non-core SWHID | |||||
with pytest.raises(ValueError, match="Expected core SWHID"): | |||||
RawExtrinsicMetadata( | |||||
target=_content_swhid, | |||||
release=SWHID( | |||||
object_type="release", | |||||
object_id="94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
metadata=_dummy_qualifiers, | |||||
), | |||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
) | ) | ||||
# SWHID type doesn't match the expected type of this context key | # SWHID type doesn't match the expected type of this context key | ||||
with pytest.raises( | with pytest.raises( | ||||
ValueError, match="Expected SWHID type 'release', got 'content'" | ValueError, match="Expected SWHID type 'release', got 'content'" | ||||
): | ): | ||||
RawExtrinsicMetadata( | RawExtrinsicMetadata( | ||||
target=_content_swhid, | target=_content_swhid, | ||||
release=SWHID( | release=CoreSWHID(object_type=ObjectType.CONTENT, object_id=EXAMPLE_HASH,), | ||||
object_type="content", | |||||
object_id="94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
), | |||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
) | ) | ||||
def test_metadata_validate_context_revision(): | def test_metadata_validate_context_revision(): | ||||
"""Checks validation of RawExtrinsicMetadata.revision.""" | """Checks validation of RawExtrinsicMetadata.revision.""" | ||||
# Origins can't have a 'revision' context | # Origins can't have a 'revision' context | ||||
with pytest.raises( | with pytest.raises( | ||||
ValueError, match="Unexpected 'revision' context for origin object" | ValueError, match="Unexpected 'revision' context for origin object" | ||||
): | ): | ||||
RawExtrinsicMetadata( | RawExtrinsicMetadata( | ||||
target=_origin_swhid, | target=_origin_swhid, | ||||
revision=SWHID( | revision=CoreSWHID( | ||||
object_type="revision", | object_type=ObjectType.REVISION, object_id=EXAMPLE_HASH, | ||||
object_id="94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
), | ), | ||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
) | ) | ||||
# but content can | # but content can | ||||
RawExtrinsicMetadata( | RawExtrinsicMetadata( | ||||
target=_content_swhid, | target=_content_swhid, | ||||
revision=SWHID( | revision=CoreSWHID(object_type=ObjectType.REVISION, object_id=EXAMPLE_HASH), | ||||
object_type="revision", object_id="94a9ed024d3859793618152ea559a168bbcbb5e2" | |||||
), | |||||
**_common_metadata_fields, | |||||
) | |||||
# Non-core SWHID | |||||
with pytest.raises(ValueError, match="Expected core SWHID"): | |||||
RawExtrinsicMetadata( | |||||
target=_content_swhid, | |||||
revision=SWHID( | |||||
object_type="revision", | |||||
object_id="94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
metadata=_dummy_qualifiers, | |||||
), | |||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
) | ) | ||||
# SWHID type doesn't match the expected type of this context key | # SWHID type doesn't match the expected type of this context key | ||||
with pytest.raises( | with pytest.raises( | ||||
ValueError, match="Expected SWHID type 'revision', got 'content'" | ValueError, match="Expected SWHID type 'revision', got 'content'" | ||||
): | ): | ||||
RawExtrinsicMetadata( | RawExtrinsicMetadata( | ||||
target=_content_swhid, | target=_content_swhid, | ||||
revision=SWHID( | revision=CoreSWHID(object_type=ObjectType.CONTENT, object_id=EXAMPLE_HASH,), | ||||
object_type="content", | |||||
object_id="94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
), | |||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
) | ) | ||||
def test_metadata_validate_context_path(): | def test_metadata_validate_context_path(): | ||||
"""Checks validation of RawExtrinsicMetadata.path.""" | """Checks validation of RawExtrinsicMetadata.path.""" | ||||
# Origins can't have a 'path' context | # Origins can't have a 'path' context | ||||
Show All 12 Lines | def test_metadata_validate_context_directory(): | ||||
"""Checks validation of RawExtrinsicMetadata.directory.""" | """Checks validation of RawExtrinsicMetadata.directory.""" | ||||
# Origins can't have a 'directory' context | # Origins can't have a 'directory' context | ||||
with pytest.raises( | with pytest.raises( | ||||
ValueError, match="Unexpected 'directory' context for origin object" | ValueError, match="Unexpected 'directory' context for origin object" | ||||
): | ): | ||||
RawExtrinsicMetadata( | RawExtrinsicMetadata( | ||||
target=_origin_swhid, | target=_origin_swhid, | ||||
directory=SWHID( | directory=CoreSWHID( | ||||
object_type="directory", | object_type=ObjectType.DIRECTORY, object_id=EXAMPLE_HASH, | ||||
object_id="94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
), | ), | ||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
) | ) | ||||
# but content can | # but content can | ||||
RawExtrinsicMetadata( | RawExtrinsicMetadata( | ||||
target=_content_swhid, | target=_content_swhid, | ||||
directory=SWHID( | directory=CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=EXAMPLE_HASH,), | ||||
object_type="directory", | |||||
object_id="94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
), | |||||
**_common_metadata_fields, | |||||
) | |||||
# Non-core SWHID | |||||
with pytest.raises(ValueError, match="Expected core SWHID"): | |||||
RawExtrinsicMetadata( | |||||
target=_content_swhid, | |||||
directory=SWHID( | |||||
object_type="directory", | |||||
object_id="94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
metadata=_dummy_qualifiers, | |||||
), | |||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
) | ) | ||||
# SWHID type doesn't match the expected type of this context key | # SWHID type doesn't match the expected type of this context key | ||||
with pytest.raises( | with pytest.raises( | ||||
ValueError, match="Expected SWHID type 'directory', got 'content'" | ValueError, match="Expected SWHID type 'directory', got 'content'" | ||||
): | ): | ||||
RawExtrinsicMetadata( | RawExtrinsicMetadata( | ||||
target=_content_swhid, | target=_content_swhid, | ||||
directory=SWHID( | directory=CoreSWHID( | ||||
object_type="content", | object_type=ObjectType.CONTENT, object_id=EXAMPLE_HASH, | ||||
object_id="94a9ed024d3859793618152ea559a168bbcbb5e2", | |||||
), | ), | ||||
**_common_metadata_fields, | **_common_metadata_fields, | ||||
) | ) |