Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/tests/test_postgresql_converters.py
Show All 17 Lines | |||||
) | ) | ||||
from swh.model.swhids import ExtendedSWHID | from swh.model.swhids import ExtendedSWHID | ||||
from swh.storage.postgresql import converters | from swh.storage.postgresql import converters | ||||
@pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||
"model_date,db_date", | "model_date,db_date", | ||||
[ | [ | ||||
(None, {"timestamp": None, "offset": 0, "neg_utc_offset": None}), | ( | ||||
None, | |||||
{ | |||||
"timestamp": None, | |||||
"offset": 0, | |||||
"neg_utc_offset": None, | |||||
"offset_bytes": None, | |||||
}, | |||||
), | |||||
( | ( | ||||
TimestampWithTimezone( | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1234567890, microseconds=0,), | timestamp=Timestamp(seconds=1234567890, microseconds=0,), | ||||
offset=120, | offset=120, | ||||
negative_utc=False, | negative_utc=False, | ||||
offset_bytes=b"+0200", | |||||
), | ), | ||||
{ | { | ||||
"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, | ||||
"offset_bytes": b"+0200", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
TimestampWithTimezone( | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1123456789, microseconds=0,), | timestamp=Timestamp(seconds=1123456789, microseconds=0,), | ||||
offset=0, | offset=0, | ||||
negative_utc=True, | negative_utc=True, | ||||
offset_bytes=b"-0000", | |||||
), | ), | ||||
{ | { | ||||
"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, | ||||
"offset_bytes": b"-0000", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
TimestampWithTimezone( | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1234567890, microseconds=0,), | timestamp=Timestamp(seconds=1234567890, microseconds=0,), | ||||
offset=42, | offset=42, | ||||
negative_utc=False, | negative_utc=False, | ||||
offset_bytes=b"+0042", | |||||
), | ), | ||||
{ | { | ||||
"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, | ||||
"offset_bytes": b"+0042", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
TimestampWithTimezone( | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1634366813, microseconds=0,), | timestamp=Timestamp(seconds=1634366813, microseconds=0,), | ||||
offset=-120, | offset=-120, | ||||
negative_utc=False, | negative_utc=False, | ||||
offset_bytes=b"-0200", | |||||
), | ), | ||||
{ | { | ||||
"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, | ||||
"offset_bytes": b"-0200", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
TimestampWithTimezone( | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=0, microseconds=0,), | timestamp=Timestamp(seconds=0, microseconds=0,), | ||||
offset=-120, | offset=-120, | ||||
negative_utc=False, | negative_utc=False, | ||||
offset_bytes=b"-0200", | |||||
), | ), | ||||
{ | { | ||||
"timestamp": "1970-01-01T00:00:00+00:00", | "timestamp": "1970-01-01T00:00:00+00:00", | ||||
"offset": -120, | "offset": -120, | ||||
"neg_utc_offset": False, | "neg_utc_offset": False, | ||||
"offset_bytes": b"-0200", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
TimestampWithTimezone( | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=0, microseconds=1,), | timestamp=Timestamp(seconds=0, microseconds=1,), | ||||
offset=-120, | offset=-120, | ||||
negative_utc=False, | negative_utc=False, | ||||
offset_bytes=b"-0200", | |||||
), | ), | ||||
{ | { | ||||
"timestamp": "1970-01-01T00:00:00.000001+00:00", | "timestamp": "1970-01-01T00:00:00.000001+00:00", | ||||
"offset": -120, | "offset": -120, | ||||
"neg_utc_offset": False, | "neg_utc_offset": False, | ||||
"offset_bytes": b"-0200", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
TimestampWithTimezone( | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=-1, microseconds=0,), | timestamp=Timestamp(seconds=-1, microseconds=0,), | ||||
offset=-120, | offset=-120, | ||||
negative_utc=False, | negative_utc=False, | ||||
offset_bytes=b"-0200", | |||||
), | ), | ||||
{ | { | ||||
"timestamp": "1969-12-31T23:59:59+00:00", | "timestamp": "1969-12-31T23:59:59+00:00", | ||||
"offset": -120, | "offset": -120, | ||||
"neg_utc_offset": False, | "neg_utc_offset": False, | ||||
"offset_bytes": b"-0200", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
TimestampWithTimezone( | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=-1, microseconds=1,), | timestamp=Timestamp(seconds=-1, microseconds=1,), | ||||
offset=-120, | offset=-120, | ||||
negative_utc=False, | negative_utc=False, | ||||
offset_bytes=b"-0200", | |||||
), | ), | ||||
{ | { | ||||
"timestamp": "1969-12-31T23:59:59.000001+00:00", | "timestamp": "1969-12-31T23:59:59.000001+00:00", | ||||
"offset": -120, | "offset": -120, | ||||
"neg_utc_offset": False, | "neg_utc_offset": False, | ||||
"offset_bytes": b"-0200", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
TimestampWithTimezone( | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=-3600, microseconds=0,), | timestamp=Timestamp(seconds=-3600, microseconds=0,), | ||||
offset=-120, | offset=-120, | ||||
negative_utc=False, | negative_utc=False, | ||||
offset_bytes=b"-0200", | |||||
), | ), | ||||
{ | { | ||||
"timestamp": "1969-12-31T23:00:00+00:00", | "timestamp": "1969-12-31T23:00:00+00:00", | ||||
"offset": -120, | "offset": -120, | ||||
"neg_utc_offset": False, | "neg_utc_offset": False, | ||||
"offset_bytes": b"-0200", | |||||
}, | }, | ||||
), | ), | ||||
( | ( | ||||
TimestampWithTimezone( | TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=-3600, microseconds=1,), | timestamp=Timestamp(seconds=-3600, microseconds=1,), | ||||
offset=-120, | offset=-120, | ||||
negative_utc=False, | negative_utc=False, | ||||
offset_bytes=b"-0200", | |||||
), | ), | ||||
{ | { | ||||
"timestamp": "1969-12-31T23:00:00.000001+00:00", | "timestamp": "1969-12-31T23:00:00.000001+00:00", | ||||
"offset": -120, | "offset": -120, | ||||
"neg_utc_offset": False, | "neg_utc_offset": False, | ||||
"offset_bytes": b"-0200", | |||||
}, | |||||
), | |||||
( | |||||
TimestampWithTimezone( | |||||
timestamp=Timestamp(seconds=1234567890, microseconds=0,), | |||||
offset=120, | |||||
negative_utc=False, | |||||
offset_bytes=b"+200", | |||||
), | |||||
{ | |||||
"timestamp": "2009-02-13T23:31:30+00:00", | |||||
"offset": 120, | |||||
"neg_utc_offset": False, | |||||
"offset_bytes": b"+200", | |||||
}, | }, | ||||
), | ), | ||||
], | ], | ||||
) | ) | ||||
def test_date(model_date, db_date): | def test_date(model_date, db_date): | ||||
assert converters.date_to_db(model_date) == db_date | assert converters.date_to_db(model_date) == db_date | ||||
assert ( | assert ( | ||||
converters.db_to_date( | converters.db_to_date( | ||||
date=None | date=None | ||||
if db_date["timestamp"] is None | if db_date["timestamp"] is None | ||||
else datetime.datetime.fromisoformat(db_date["timestamp"]), | else datetime.datetime.fromisoformat(db_date["timestamp"]), | ||||
offset=db_date["offset"], | offset=db_date["offset"], | ||||
neg_utc_offset=db_date["neg_utc_offset"], | neg_utc_offset=db_date["neg_utc_offset"], | ||||
offset_bytes=db_date["offset_bytes"], | |||||
) | ) | ||||
== model_date | == model_date | ||||
) | ) | ||||
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") | ||||
Show All 13 Lines | |||||
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": b"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, | ||||
"date_offset_bytes": 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, | ||||
"committer_date_offset_bytes": None, | |||||
"type": "git", | "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": (), | ||||
"raw_manifest": None, | |||||
"parents": [b"123", b"456"], | "parents": [b"123", b"456"], | ||||
} | } | ||||
) | ) | ||||
# then | # then | ||||
assert actual_revision == Revision( | assert actual_revision == Revision( | ||||
id=b"revision-id", | id=b"revision-id", | ||||
author=Person( | author=Person( | ||||
Show All 19 Lines | def test_db_to_release(): | ||||
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, | ||||
"date_offset_bytes": 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", | ||||
"raw_manifest": None, | |||||
} | } | ||||
) | ) | ||||
# then | # then | ||||
assert actual_release == Release( | assert actual_release == Release( | ||||
author=Person( | author=Person( | ||||
fullname=b"auth-fullname", name=b"auth-name", email=b"auth-email", | fullname=b"auth-fullname", name=b"auth-name", email=b"auth-email", | ||||
), | ), | ||||
Show All 37 Lines |