Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_model.py
Show All 37 Lines | from swh.model.model import ( | ||||
RawExtrinsicMetadata, | RawExtrinsicMetadata, | ||||
Release, | Release, | ||||
Revision, | Revision, | ||||
SkippedContent, | SkippedContent, | ||||
Snapshot, | Snapshot, | ||||
TargetType, | TargetType, | ||||
Timestamp, | Timestamp, | ||||
TimestampWithTimezone, | TimestampWithTimezone, | ||||
type_validator, | optimized_validator, | ||||
) | ) | ||||
import swh.model.swhids | import swh.model.swhids | ||||
from swh.model.swhids import CoreSWHID, ExtendedSWHID, ObjectType | from swh.model.swhids import CoreSWHID, ExtendedSWHID, ObjectType | ||||
from swh.model.tests.swh_model_data import TEST_OBJECTS | from swh.model.tests.swh_model_data import TEST_OBJECTS | ||||
from swh.model.tests.test_identifiers import ( | from swh.model.tests.test_identifiers import ( | ||||
TS_DATETIMES, | TS_DATETIMES, | ||||
TS_TIMEZONES, | TS_TIMEZONES, | ||||
directory_example, | directory_example, | ||||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Lines | _TYPE_VALIDATOR_PARAMETERS: List[Tuple[Any, List[Any], List[Any]]] = [ | ||||
), | ), | ||||
# tuples | # tuples | ||||
( | ( | ||||
Tuple[str, str], | Tuple[str, str], | ||||
[("foo", "bar"), ("", ""), _custom_namedtuple("", ""), _custom_tuple(("", ""))], | [("foo", "bar"), ("", ""), _custom_namedtuple("", ""), _custom_tuple(("", ""))], | ||||
[("foo",), ("foo", "bar", "baz"), ("foo", 42), (42, "foo")], | [("foo",), ("foo", "bar", "baz"), ("foo", 42), (42, "foo")], | ||||
), | ), | ||||
( | ( | ||||
Tuple[bytes, bytes], | |||||
[ | |||||
(b"foo", b"bar"), | |||||
(b"", b""), | |||||
_custom_namedtuple(b"", b""), | |||||
_custom_tuple((b"", b"")), | |||||
], | |||||
[(b"foo",), (b"foo", b"bar", b"baz"), (b"foo", 42), (42, b"foo")], | |||||
), | |||||
( | |||||
Tuple[str, ...], | Tuple[str, ...], | ||||
[ | [ | ||||
("foo",), | ("foo",), | ||||
("foo", "bar"), | ("foo", "bar"), | ||||
("", ""), | ("", ""), | ||||
("foo", "bar", "baz"), | ("foo", "bar", "baz"), | ||||
_custom_namedtuple("", ""), | _custom_namedtuple("", ""), | ||||
_custom_tuple(("", "")), | _custom_tuple(("", "")), | ||||
▲ Show 20 Lines • Show All 91 Lines • ▼ Show 20 Lines | |||||
@pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||
"type_,value", | "type_,value", | ||||
[ | [ | ||||
pytest.param(type_, value, id=f"type={type_}, value={value}") | pytest.param(type_, value, id=f"type={type_}, value={value}") | ||||
for (type_, values, _) in _TYPE_VALIDATOR_PARAMETERS | for (type_, values, _) in _TYPE_VALIDATOR_PARAMETERS | ||||
for value in values | for value in values | ||||
], | ], | ||||
) | ) | ||||
def test_type_validator_valid(type_, value): | def test_optimized_type_validator_valid(type_, value): | ||||
type_validator()(None, attr.ib(type=type_), value) | validator = optimized_validator(type_) | ||||
validator(None, attr.ib(type=type_), value) | |||||
@pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||
"type_,value", | "type_,value", | ||||
[ | [ | ||||
pytest.param(type_, value, id=f"type={type_}, value={value}") | pytest.param(type_, value, id=f"type={type_}, value={value}") | ||||
for (type_, _, values) in _TYPE_VALIDATOR_PARAMETERS | for (type_, _, values) in _TYPE_VALIDATOR_PARAMETERS | ||||
for value in values | for value in values | ||||
], | ], | ||||
) | ) | ||||
def test_type_validator_invalid(type_, value): | def test_optimized_type_validator_invalid(type_, value): | ||||
validator = optimized_validator(type_) | |||||
with pytest.raises(AttributeTypeError): | with pytest.raises(AttributeTypeError): | ||||
type_validator()(None, attr.ib(type=type_), value) | validator(None, attr.ib(type=type_), value) | ||||
@pytest.mark.parametrize("object_type, objects", TEST_OBJECTS.items()) | @pytest.mark.parametrize("object_type, objects", TEST_OBJECTS.items()) | ||||
def test_swh_model_todict_fromdict(object_type, objects): | def test_swh_model_todict_fromdict(object_type, objects): | ||||
"""checks model objects in swh_model_data are in correct shape""" | """checks model objects in swh_model_data are in correct shape""" | ||||
assert objects | assert objects | ||||
for obj in objects: | for obj in objects: | ||||
# Check the composition of from_dict and to_dict is the identity | # Check the composition of from_dict and to_dict is the identity | ||||
▲ Show 20 Lines • Show All 1,521 Lines • Show Last 20 Lines |