Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_model.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import copy | import copy | ||||
import datetime | import datetime | ||||
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.model import ( | from swh.model.model import ( | ||||
Content, SkippedContent, Directory, Revision, Release, Snapshot, | Content, SkippedContent, Directory, Revision, Release, Snapshot, | ||||
Timestamp, TimestampWithTimezone, | Timestamp, TimestampWithTimezone, | ||||
MissingData, | MissingData, Person | ||||
) | ) | ||||
from swh.model.hashutil import hash_to_bytes, MultiHash | from swh.model.hashutil import hash_to_bytes, MultiHash | ||||
from swh.model.hypothesis_strategies import objects, origins, origin_visits | from swh.model.hypothesis_strategies import objects, origins, origin_visits | ||||
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 ( | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | assert tstz == TimestampWithTimezone( | ||||
seconds=1582810759, | seconds=1582810759, | ||||
microseconds=0, | microseconds=0, | ||||
), | ), | ||||
offset=0, | offset=0, | ||||
negative_utc=True, | negative_utc=True, | ||||
) | ) | ||||
def test_person_from_fullname(): | |||||
"""The author should have name, email and fullname filled. | |||||
""" | |||||
actual_person = Person.from_fullname(b'tony <ynot@dagobah>') | |||||
assert actual_person == Person( | |||||
fullname=b'tony <ynot@dagobah>', | |||||
name=b'tony', | |||||
email=b'ynot@dagobah', | |||||
) | |||||
def test_person_from_fullname_no_email(): | |||||
"""The author and fullname should be the same as the input (author). | |||||
""" | |||||
actual_person = Person.from_fullname(b'tony') | |||||
assert actual_person == Person( | |||||
fullname=b'tony', | |||||
name=b'tony', | |||||
email=None, | |||||
) | |||||
def test_person_from_fullname_empty_person(): | |||||
"""Empty person has only its fullname filled with the empty | |||||
byte-string. | |||||
""" | |||||
actual_person = Person.from_fullname(b'') | |||||
assert actual_person == Person( | |||||
fullname=b'', | |||||
name=None, | |||||
email=None, | |||||
) | |||||
def test_git_author_line_to_author(): | |||||
# edge case out of the way | |||||
with pytest.raises(TypeError): | |||||
Person.from_fullname(None) | |||||
tests = { | |||||
b'a <b@c.com>': Person( | |||||
name=b'a', | |||||
email=b'b@c.com', | |||||
fullname=b'a <b@c.com>', | |||||
), | |||||
b'<foo@bar.com>': Person( | |||||
name=None, | |||||
email=b'foo@bar.com', | |||||
fullname=b'<foo@bar.com>', | |||||
), | |||||
b'malformed <email': Person( | |||||
name=b'malformed', | |||||
email=b'email', | |||||
fullname=b'malformed <email' | |||||
), | |||||
b'malformed <"<br"@ckets>': Person( | |||||
name=b'malformed', | |||||
email=b'"<br"@ckets', | |||||
fullname=b'malformed <"<br"@ckets>', | |||||
), | |||||
b'trailing <sp@c.e> ': Person( | |||||
name=b'trailing', | |||||
email=b'sp@c.e', | |||||
fullname=b'trailing <sp@c.e> ', | |||||
), | |||||
b'no<sp@c.e>': Person( | |||||
name=b'no', | |||||
email=b'sp@c.e', | |||||
fullname=b'no<sp@c.e>', | |||||
), | |||||
b' more <sp@c.es>': Person( | |||||
name=b'more', | |||||
email=b'sp@c.es', | |||||
fullname=b' more <sp@c.es>', | |||||
), | |||||
b' <>': Person( | |||||
name=None, | |||||
email=None, | |||||
fullname=b' <>', | |||||
), | |||||
} | |||||
for person in sorted(tests): | |||||
expected_person = tests[person] | |||||
assert expected_person == Person.from_fullname(person) | |||||
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 20 Lines • Show All 102 Lines • Show Last 20 Lines |