Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_model.py
Show All 11 Lines | |||||
from hypothesis.strategies import binary | from hypothesis.strategies import binary | ||||
import pytest | import pytest | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
Content, SkippedContent, Directory, Revision, Release, Snapshot, | Content, SkippedContent, Directory, Revision, Release, Snapshot, | ||||
Origin, Timestamp, TimestampWithTimezone, | Origin, Timestamp, TimestampWithTimezone, | ||||
MissingData, Person | MissingData, Person | ||||
) | ) | ||||
from swh.model.hashutil import hash_to_bytes, MultiHash | from swh.model.hashutil import hash_to_bytes, MultiHash | ||||
import swh.model.hypothesis_strategies as strategies | |||||
vlorentz: s/hypos/strategies/ | |||||
from swh.model.hypothesis_strategies import ( | |||||
objects, origins, origin_visits, origin_visit_updates, | |||||
skipped_contents_d, timestamps | |||||
) | |||||
from swh.model.identifiers import ( | from swh.model.identifiers import ( | ||||
directory_identifier, revision_identifier, release_identifier, | directory_identifier, revision_identifier, release_identifier, | ||||
snapshot_identifier | snapshot_identifier | ||||
) | ) | ||||
from swh.model.tests.test_identifiers import ( | from swh.model.tests.test_identifiers import ( | ||||
directory_example, revision_example, release_example, snapshot_example | directory_example, revision_example, release_example, snapshot_example | ||||
) | ) | ||||
@given(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 | ||||
obj_as_dict = obj.to_dict() | obj_as_dict = obj.to_dict() | ||||
obj_as_dict_copy = copy.deepcopy(obj_as_dict) | obj_as_dict_copy = copy.deepcopy(obj_as_dict) | ||||
# Check the composition of to_dict and from_dict is the identity | # Check the composition of to_dict and from_dict is the identity | ||||
assert obj == type(obj).from_dict(obj_as_dict) | assert obj == type(obj).from_dict(obj_as_dict) | ||||
# Check from_dict() does not change the input dict | # Check from_dict() does not change the input dict | ||||
assert obj_as_dict == obj_as_dict_copy | assert obj_as_dict == obj_as_dict_copy | ||||
# Check the composition of from_dict and to_dict is the identity | # Check the composition of from_dict and to_dict is the identity | ||||
assert obj_as_dict == type(obj).from_dict(obj_as_dict).to_dict() | assert obj_as_dict == type(obj).from_dict(obj_as_dict).to_dict() | ||||
@given(origins()) | @given(strategies.origins()) | ||||
def test_todict_origins(origin): | def test_todict_origins(origin): | ||||
obj = origin.to_dict() | obj = origin.to_dict() | ||||
assert 'type' not in obj | assert 'type' not in obj | ||||
assert type(origin)(url=origin.url) == type(origin).from_dict(obj) | assert type(origin)(url=origin.url) == type(origin).from_dict(obj) | ||||
@given(origin_visits()) | @given(strategies.origin_visits()) | ||||
def test_todict_origin_visits(origin_visit): | def test_todict_origin_visits(origin_visit): | ||||
obj = origin_visit.to_dict() | obj = origin_visit.to_dict() | ||||
assert origin_visit == type(origin_visit).from_dict(obj) | assert origin_visit == type(origin_visit).from_dict(obj) | ||||
@given(origin_visit_updates()) | @given(strategies.origin_visit_updates()) | ||||
def test_todict_origin_visit_updates(origin_visit_update): | def test_todict_origin_visit_updates(origin_visit_update): | ||||
obj = origin_visit_update.to_dict() | obj = origin_visit_update.to_dict() | ||||
assert origin_visit_update == type(origin_visit_update).from_dict(obj) | assert origin_visit_update == type(origin_visit_update).from_dict(obj) | ||||
# Timestamp | # Timestamp | ||||
@given(timestamps()) | @given(strategies.timestamps()) | ||||
def test_timestamps_strategy(timestamp): | def test_timestamps_strategy(timestamp): | ||||
attr.validate(timestamp) | attr.validate(timestamp) | ||||
def test_timestamp_seconds(): | def test_timestamp_seconds(): | ||||
attr.validate(Timestamp(seconds=0, microseconds=0)) | attr.validate(Timestamp(seconds=0, microseconds=0)) | ||||
with pytest.raises(AttributeTypeError): | with pytest.raises(AttributeTypeError): | ||||
Timestamp(seconds='0', microseconds=0) | Timestamp(seconds='0', microseconds=0) | ||||
▲ Show 20 Lines • Show All 233 Lines • ▼ Show 20 Lines | tests = { | ||||
), | ), | ||||
} | } | ||||
for person in sorted(tests): | for person in sorted(tests): | ||||
expected_person = tests[person] | expected_person = tests[person] | ||||
assert expected_person == Person.from_fullname(person) | assert expected_person == Person.from_fullname(person) | ||||
# Content | |||||
def test_content_get_hash(): | def test_content_get_hash(): | ||||
hashes = dict( | hashes = dict( | ||||
sha1=b'foo', sha1_git=b'bar', sha256=b'baz', blake2s256=b'qux') | sha1=b'foo', sha1_git=b'bar', sha256=b'baz', blake2s256=b'qux') | ||||
c = Content(length=42, status='visible', **hashes) | c = Content(length=42, status='visible', **hashes) | ||||
for (hash_name, hash_) in hashes.items(): | for (hash_name, hash_) in hashes.items(): | ||||
assert c.get_hash(hash_name) == hash_ | assert c.get_hash(hash_name) == hash_ | ||||
Show All 14 Lines | |||||
def test_content_data_missing(): | def test_content_data_missing(): | ||||
c = Content( | c = Content( | ||||
length=42, status='visible', | length=42, status='visible', | ||||
sha1=b'foo', sha1_git=b'bar', sha256=b'baz', blake2s256=b'qux') | sha1=b'foo', sha1_git=b'bar', sha256=b'baz', blake2s256=b'qux') | ||||
with pytest.raises(MissingData): | with pytest.raises(MissingData): | ||||
c.with_data() | c.with_data() | ||||
@given(strategies.present_contents_d()) | |||||
def test_content_from_dict(content_d): | |||||
c = Content.from_data(**content_d) | |||||
assert c | |||||
assert c.ctime == content_d['ctime'] | |||||
content_d2 = c.to_dict() | |||||
c2 = Content.from_dict(content_d2) | |||||
Not Done Inline ActionsPlease, add one case where the ctime value is a string and that should cover my previous comment ;) ardumont: Please, add one case where the ctime value is a string and that should cover my previous… | |||||
assert c2.ctime == c.ctime | |||||
def test_content_from_dict_str_ctime(): | |||||
# test with ctime as a string | |||||
n = datetime.datetime(2020, 5, 6, 12, 34) | |||||
content_d = { | |||||
'ctime': n.isoformat(), | |||||
'data': b'', | |||||
'length': 0, | |||||
'sha1': b'\x00', | |||||
'sha256': b'\x00', | |||||
'sha1_git': b'\x00', | |||||
'blake2s256': b'\x00', | |||||
} | |||||
c = Content.from_dict(content_d) | |||||
assert c.ctime == n | |||||
@given(binary(max_size=4096)) | @given(binary(max_size=4096)) | ||||
def test_content_from_data(data): | def test_content_from_data(data): | ||||
c = Content.from_data(data) | c = Content.from_data(data) | ||||
assert c.data == data | assert c.data == data | ||||
assert c.length == len(data) | assert c.length == len(data) | ||||
assert c.status == 'visible' | assert c.status == 'visible' | ||||
for key, value in MultiHash.from_data(data).digest().items(): | for key, value in MultiHash.from_data(data).digest().items(): | ||||
assert getattr(c, key) == value | assert getattr(c, key) == value | ||||
@given(binary(max_size=4096)) | @given(binary(max_size=4096)) | ||||
def test_hidden_content_from_data(data): | def test_hidden_content_from_data(data): | ||||
c = Content.from_data(data, status='hidden') | c = Content.from_data(data, status='hidden') | ||||
assert c.data == data | assert c.data == data | ||||
assert c.length == len(data) | assert c.length == len(data) | ||||
assert c.status == 'hidden' | assert c.status == 'hidden' | ||||
for key, value in MultiHash.from_data(data).digest().items(): | for key, value in MultiHash.from_data(data).digest().items(): | ||||
assert getattr(c, key) == value | assert getattr(c, key) == value | ||||
# SkippedContent | |||||
@given(binary(max_size=4096)) | @given(binary(max_size=4096)) | ||||
def test_skipped_content_from_data(data): | def test_skipped_content_from_data(data): | ||||
c = SkippedContent.from_data(data, reason='reason') | c = SkippedContent.from_data(data, reason='reason') | ||||
assert c.reason == 'reason' | assert c.reason == 'reason' | ||||
assert c.length == len(data) | assert c.length == len(data) | ||||
assert c.status == 'absent' | assert c.status == 'absent' | ||||
for key, value in MultiHash.from_data(data).digest().items(): | for key, value in MultiHash.from_data(data).digest().items(): | ||||
assert getattr(c, key) == value | assert getattr(c, key) == value | ||||
@given(skipped_contents_d()) | @given(strategies.skipped_contents_d()) | ||||
def test_skipped_content_origin_is_str(skipped_content_d): | def test_skipped_content_origin_is_str(skipped_content_d): | ||||
assert SkippedContent.from_dict(skipped_content_d) | assert SkippedContent.from_dict(skipped_content_d) | ||||
skipped_content_d['origin'] = 'http://path/to/origin' | skipped_content_d['origin'] = 'http://path/to/origin' | ||||
assert SkippedContent.from_dict(skipped_content_d) | assert SkippedContent.from_dict(skipped_content_d) | ||||
skipped_content_d['origin'] = Origin(url='http://path/to/origin') | skipped_content_d['origin'] = Origin(url='http://path/to/origin') | ||||
with pytest.raises(ValueError, match='origin'): | with pytest.raises(ValueError, match='origin'): | ||||
▲ Show 20 Lines • Show All 56 Lines • Show Last 20 Lines |
s/hypos/strategies/