Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/tests/test_converters.py
# Copyright (C) 2015 The Software Heritage developers | # Copyright (C) 2015 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 | ||||
from swh.model.model import ( | |||||
ObjectType, | |||||
Person, | |||||
Release, | |||||
Revision, | |||||
RevisionType, | |||||
Timestamp, | |||||
TimestampWithTimezone, | |||||
) | |||||
from swh.storage import converters | from swh.storage import converters | ||||
def test_date_to_db(): | def test_date_to_db(): | ||||
date_to_db = converters.date_to_db | date_to_db = converters.date_to_db | ||||
assert date_to_db(None) == {"timestamp": None, "offset": 0, "neg_utc_offset": None} | assert date_to_db(None) == {"timestamp": None, "offset": 0, "neg_utc_offset": None} | ||||
assert date_to_db( | assert date_to_db( | ||||
{"timestamp": 1234567890, "offset": 120, "negative_utc": False,} | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1234567890, microseconds=0,), | |||||
offset=120, | |||||
negative_utc=False, | |||||
) | |||||
) == { | ) == { | ||||
"timestamp": "2009-02-13T23:31:30+00:00", | "timestamp": "2009-02-13T23:31:30+00:00", | ||||
"offset": 120, | "offset": 120, | ||||
"neg_utc_offset": False, | "neg_utc_offset": False, | ||||
} | } | ||||
assert date_to_db( | assert date_to_db( | ||||
{"timestamp": 1123456789, "offset": 0, "negative_utc": True,} | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1123456789, microseconds=0,), | |||||
offset=0, | |||||
negative_utc=True, | |||||
) | |||||
) == { | ) == { | ||||
"timestamp": "2005-08-07T23:19:49+00:00", | "timestamp": "2005-08-07T23:19:49+00:00", | ||||
"offset": 0, | "offset": 0, | ||||
"neg_utc_offset": True, | "neg_utc_offset": True, | ||||
} | } | ||||
assert date_to_db( | assert date_to_db( | ||||
{"timestamp": 1234567890, "offset": 42, "negative_utc": False,} | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1234567890, microseconds=0,), | |||||
offset=42, | |||||
negative_utc=False, | |||||
) | |||||
) == { | ) == { | ||||
"timestamp": "2009-02-13T23:31:30+00:00", | "timestamp": "2009-02-13T23:31:30+00:00", | ||||
"offset": 42, | "offset": 42, | ||||
"neg_utc_offset": False, | "neg_utc_offset": False, | ||||
} | } | ||||
assert date_to_db( | assert date_to_db( | ||||
{"timestamp": 1634366813, "offset": -120, "negative_utc": False,} | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1634366813, microseconds=0,), | |||||
offset=-120, | |||||
negative_utc=False, | |||||
) | |||||
) == { | ) == { | ||||
"timestamp": "2021-10-16T06:46:53+00:00", | "timestamp": "2021-10-16T06:46:53+00:00", | ||||
"offset": -120, | "offset": -120, | ||||
"neg_utc_offset": False, | "neg_utc_offset": False, | ||||
} | } | ||||
def test_db_to_author(): | def test_db_to_author(): | ||||
# when | # when | ||||
actual_author = converters.db_to_author(b"fullname", b"name", b"email") | actual_author = converters.db_to_author(b"fullname", b"name", b"email") | ||||
# then | # then | ||||
assert actual_author == { | assert actual_author == Person(fullname=b"fullname", name=b"name", email=b"email",) | ||||
"fullname": b"fullname", | |||||
"name": b"name", | |||||
"email": b"email", | |||||
} | |||||
def test_db_to_author_none(): | def test_db_to_author_none(): | ||||
# when | # when | ||||
actual_author = converters.db_to_author(None, None, None) | actual_author = converters.db_to_author(None, None, None) | ||||
# then | # then | ||||
assert actual_author is None | assert actual_author is None | ||||
def test_db_to_revision(): | def test_db_to_revision(): | ||||
# when | # when | ||||
actual_revision = converters.db_to_revision( | actual_revision = converters.db_to_revision( | ||||
{ | { | ||||
"id": "revision-id", | "id": b"revision-id", | ||||
"date": None, | "date": None, | ||||
"date_offset": None, | "date_offset": None, | ||||
"date_neg_utc_offset": None, | "date_neg_utc_offset": None, | ||||
"committer_date": None, | "committer_date": None, | ||||
"committer_date_offset": None, | "committer_date_offset": None, | ||||
"committer_date_neg_utc_offset": None, | "committer_date_neg_utc_offset": None, | ||||
"type": "rev", | "type": "git", | ||||
"directory": b"dir-sha1", | "directory": b"dir-sha1", | ||||
"message": b"commit message", | "message": b"commit message", | ||||
"author_fullname": b"auth-fullname", | "author_fullname": b"auth-fullname", | ||||
"author_name": b"auth-name", | "author_name": b"auth-name", | ||||
"author_email": b"auth-email", | "author_email": b"auth-email", | ||||
"committer_fullname": b"comm-fullname", | "committer_fullname": b"comm-fullname", | ||||
"committer_name": b"comm-name", | "committer_name": b"comm-name", | ||||
"committer_email": b"comm-email", | "committer_email": b"comm-email", | ||||
"metadata": {}, | "metadata": {}, | ||||
"synthetic": False, | "synthetic": False, | ||||
"extra_headers": (), | "extra_headers": (), | ||||
"parents": [123, 456], | "parents": [b"123", b"456"], | ||||
} | } | ||||
) | ) | ||||
# then | # then | ||||
assert actual_revision == { | assert actual_revision == Revision( | ||||
"id": "revision-id", | id=b"revision-id", | ||||
"author": { | author=Person( | ||||
"fullname": b"auth-fullname", | fullname=b"auth-fullname", name=b"auth-name", email=b"auth-email", | ||||
"name": b"auth-name", | ), | ||||
"email": b"auth-email", | date=None, | ||||
}, | committer=Person( | ||||
"date": None, | fullname=b"comm-fullname", name=b"comm-name", email=b"comm-email", | ||||
"committer": { | ), | ||||
"fullname": b"comm-fullname", | committer_date=None, | ||||
"name": b"comm-name", | type=RevisionType.GIT, | ||||
"email": b"comm-email", | directory=b"dir-sha1", | ||||
}, | message=b"commit message", | ||||
"committer_date": None, | metadata={}, | ||||
"type": "rev", | synthetic=False, | ||||
"directory": b"dir-sha1", | extra_headers=(), | ||||
"message": b"commit message", | parents=(b"123", b"456"), | ||||
"metadata": {}, | ) | ||||
"synthetic": False, | |||||
"extra_headers": (), | |||||
"parents": [123, 456], | |||||
} | |||||
def test_db_to_release(): | def test_db_to_release(): | ||||
# when | # when | ||||
actual_release = converters.db_to_release( | actual_release = converters.db_to_release( | ||||
{ | { | ||||
"id": b"release-id", | "id": b"release-id", | ||||
"target": b"revision-id", | "target": b"revision-id", | ||||
"target_type": "revision", | "target_type": "revision", | ||||
"date": None, | "date": None, | ||||
"date_offset": None, | "date_offset": None, | ||||
"date_neg_utc_offset": None, | "date_neg_utc_offset": None, | ||||
"name": b"release-name", | "name": b"release-name", | ||||
"comment": b"release comment", | "comment": b"release comment", | ||||
"synthetic": True, | "synthetic": True, | ||||
"author_fullname": b"auth-fullname", | "author_fullname": b"auth-fullname", | ||||
"author_name": b"auth-name", | "author_name": b"auth-name", | ||||
"author_email": b"auth-email", | "author_email": b"auth-email", | ||||
} | } | ||||
) | ) | ||||
# then | # then | ||||
assert actual_release == { | assert actual_release == Release( | ||||
"author": { | author=Person( | ||||
"fullname": b"auth-fullname", | fullname=b"auth-fullname", name=b"auth-name", email=b"auth-email", | ||||
"name": b"auth-name", | ), | ||||
"email": b"auth-email", | date=None, | ||||
}, | id=b"release-id", | ||||
"date": None, | name=b"release-name", | ||||
"id": b"release-id", | message=b"release comment", | ||||
"name": b"release-name", | synthetic=True, | ||||
"message": b"release comment", | target=b"revision-id", | ||||
"synthetic": True, | target_type=ObjectType.REVISION, | ||||
"target": b"revision-id", | ) | ||||
"target_type": "revision", | |||||
} |