Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/common/test_converters.py
# Copyright (C) 2015-2021 The Software Heritage developers | # Copyright (C) 2015-2021 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 Affero General Public License version 3, or any later version | # License: GNU Affero 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 datetime | import datetime | ||||
import hashlib | import hashlib | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
ObjectType, | ObjectType, | ||||
Person, | Person, | ||||
Release, | Release, | ||||
Revision, | Revision, | ||||
RevisionType, | RevisionType, | ||||
Timestamp, | |||||
TimestampWithTimezone, | TimestampWithTimezone, | ||||
) | ) | ||||
from swh.web.common import converters | from swh.web.common import converters | ||||
def test_fmap(): | def test_fmap(): | ||||
assert [2, 3, None, 4] == converters.fmap(lambda x: x + 1, [1, 2, None, 3]) | assert [2, 3, None, 4] == converters.fmap(lambda x: x + 1, [1, 2, None, 3]) | ||||
assert [11, 12, 13] == list( | assert [11, 12, 13] == list( | ||||
▲ Show 20 Lines • Show All 181 Lines • ▼ Show 20 Lines | for snap in [snap_hash, None]: | ||||
actual_visit = converters.from_origin_visit(visit) | actual_visit = converters.from_origin_visit(visit) | ||||
assert actual_visit == expected_visit | assert actual_visit == expected_visit | ||||
def test_from_release(): | def test_from_release(): | ||||
"""Convert release model object to a dict should be ok""" | """Convert release model object to a dict should be ok""" | ||||
ts = int( | |||||
datetime.datetime( | |||||
2015, 1, 1, 22, 0, 0, tzinfo=datetime.timezone.utc | |||||
).timestamp() | |||||
) | |||||
release_input = Release( | release_input = Release( | ||||
id=hashutil.hash_to_bytes("aad23fa492a0c5fed0708a6703be875448c86884"), | id=hashutil.hash_to_bytes("aad23fa492a0c5fed0708a6703be875448c86884"), | ||||
target=hashutil.hash_to_bytes("5e46d564378afc44b31bb89f99d5675195fbdf67"), | target=hashutil.hash_to_bytes("5e46d564378afc44b31bb89f99d5675195fbdf67"), | ||||
target_type=ObjectType.REVISION, | target_type=ObjectType.REVISION, | ||||
date=TimestampWithTimezone( | date=TimestampWithTimezone.from_datetime( | ||||
timestamp=Timestamp(seconds=ts, microseconds=0), | datetime.datetime(2015, 1, 1, 22, 0, 0, tzinfo=datetime.timezone.utc) | ||||
offset=0, | |||||
negative_utc=False, | |||||
), | ), | ||||
ardumont: might as well inline this, date seems only used once. | |||||
author=Person( | author=Person( | ||||
name=b"author name", | name=b"author name", | ||||
fullname=b"Author Name author@email", | fullname=b"Author Name author@email", | ||||
email=b"author@email", | email=b"author@email", | ||||
), | ), | ||||
name=b"v0.0.1", | name=b"v0.0.1", | ||||
message=b"some comment on release", | message=b"some comment on release", | ||||
synthetic=True, | synthetic=True, | ||||
Show All 16 Lines | def test_from_release(): | ||||
} | } | ||||
actual_release = converters.from_release(release_input) | actual_release = converters.from_release(release_input) | ||||
assert actual_release == expected_release | assert actual_release == expected_release | ||||
def test_from_revision_model_object(): | def test_from_revision_model_object(): | ||||
ts = int( | date = TimestampWithTimezone.from_datetime( | ||||
datetime.datetime( | datetime.datetime(2000, 1, 17, 11, 23, 54, tzinfo=datetime.timezone.utc) | ||||
2000, 1, 17, 11, 23, 54, tzinfo=datetime.timezone.utc | |||||
).timestamp() | |||||
) | ) | ||||
revision_input = Revision( | revision_input = Revision( | ||||
directory=hashutil.hash_to_bytes("7834ef7e7c357ce2af928115c6c6a42b7e2a44e6"), | directory=hashutil.hash_to_bytes("7834ef7e7c357ce2af928115c6c6a42b7e2a44e6"), | ||||
author=Person( | author=Person( | ||||
name=b"Software Heritage", | name=b"Software Heritage", | ||||
fullname=b"robot robot@softwareheritage.org", | fullname=b"robot robot@softwareheritage.org", | ||||
email=b"robot@softwareheritage.org", | email=b"robot@softwareheritage.org", | ||||
), | ), | ||||
committer=Person( | committer=Person( | ||||
name=b"Software Heritage", | name=b"Software Heritage", | ||||
fullname=b"robot robot@softwareheritage.org", | fullname=b"robot robot@softwareheritage.org", | ||||
email=b"robot@softwareheritage.org", | email=b"robot@softwareheritage.org", | ||||
), | ), | ||||
message=b"synthetic revision message", | message=b"synthetic revision message", | ||||
date=TimestampWithTimezone( | date=date, | ||||
timestamp=Timestamp(seconds=ts, microseconds=0), | committer_date=date, | ||||
offset=0, | |||||
negative_utc=False, | |||||
), | |||||
committer_date=TimestampWithTimezone( | |||||
timestamp=Timestamp(seconds=ts, microseconds=0), | |||||
offset=0, | |||||
negative_utc=False, | |||||
), | |||||
synthetic=True, | synthetic=True, | ||||
type=RevisionType.TAR, | type=RevisionType.TAR, | ||||
parents=tuple( | parents=tuple( | ||||
[ | [ | ||||
hashutil.hash_to_bytes("29d8be353ed3480476f032475e7c244eff7371d5"), | hashutil.hash_to_bytes("29d8be353ed3480476f032475e7c244eff7371d5"), | ||||
hashutil.hash_to_bytes("30d8be353ed3480476f032475e7c244eff7371d5"), | hashutil.hash_to_bytes("30d8be353ed3480476f032475e7c244eff7371d5"), | ||||
] | ] | ||||
), | ), | ||||
▲ Show 20 Lines • Show All 463 Lines • Show Last 20 Lines |
might as well inline this, date seems only used once.