Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_model.py
Show All 12 Lines | |||||
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, Person | 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 ( | from swh.model.hypothesis_strategies import ( | ||||
objects, origins, origin_visits, origin_visit_updates | objects, origins, origin_visits, origin_visit_updates, 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 | ||||
) | ) | ||||
Show All 35 Lines | |||||
@given(origin_visit_updates()) | @given(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 | |||||
@given(timestamps()) | |||||
def test_timestamps_strategy(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) | ||||
attr.validate(Timestamp(seconds=2**63-1, microseconds=0)) | attr.validate(Timestamp(seconds=2**63-1, microseconds=0)) | ||||
with pytest.raises(ValueError): | with pytest.raises(ValueError): | ||||
Timestamp(seconds=2**63, microseconds=0) | Timestamp(seconds=2**63, microseconds=0) | ||||
Show All 11 Lines | def test_timestamp_microseconds(): | ||||
attr.validate(Timestamp(seconds=0, microseconds=10**6-1)) | attr.validate(Timestamp(seconds=0, microseconds=10**6-1)) | ||||
with pytest.raises(ValueError): | with pytest.raises(ValueError): | ||||
Timestamp(seconds=0, microseconds=10**6) | Timestamp(seconds=0, microseconds=10**6) | ||||
with pytest.raises(ValueError): | with pytest.raises(ValueError): | ||||
Timestamp(seconds=0, microseconds=-1) | Timestamp(seconds=0, microseconds=-1) | ||||
def test_timestamp_from_dict(): | |||||
assert Timestamp.from_dict({'seconds': 10, 'microseconds': 5}) | |||||
with pytest.raises(AttributeTypeError): | |||||
Timestamp.from_dict({'seconds': '10', 'microseconds': 5}) | |||||
with pytest.raises(AttributeTypeError): | |||||
Timestamp.from_dict({'seconds': 10, 'microseconds': '5'}) | |||||
with pytest.raises(ValueError): | |||||
Timestamp.from_dict({'seconds': 0, 'microseconds': -1}) | |||||
Timestamp.from_dict({'seconds': 0, 'microseconds': 10**6 - 1}) | |||||
with pytest.raises(ValueError): | |||||
Timestamp.from_dict({'seconds': 0, 'microseconds': 10**6}) | |||||
# TimestampWithTimezone | |||||
def test_timestampwithtimezone(): | |||||
ts = Timestamp(seconds=0, microseconds=0) | |||||
tstz = TimestampWithTimezone( | |||||
timestamp=ts, | |||||
offset=0, | |||||
negative_utc=False) | |||||
attr.validate(tstz) | |||||
assert tstz.negative_utc is False | |||||
attr.validate(TimestampWithTimezone( | |||||
timestamp=ts, | |||||
offset=10, | |||||
negative_utc=False)) | |||||
attr.validate(TimestampWithTimezone( | |||||
timestamp=ts, | |||||
offset=-10, | |||||
negative_utc=False)) | |||||
tstz = TimestampWithTimezone( | |||||
timestamp=ts, | |||||
offset=0, | |||||
negative_utc=True) | |||||
attr.validate(tstz) | |||||
assert tstz.negative_utc is True | |||||
with pytest.raises(AttributeTypeError): | |||||
TimestampWithTimezone( | |||||
timestamp=datetime.datetime.now(), | |||||
offset=0, | |||||
negative_utc=False) | |||||
with pytest.raises(AttributeTypeError): | |||||
TimestampWithTimezone( | |||||
timestamp=ts, | |||||
offset='0', | |||||
negative_utc=False) | |||||
with pytest.raises(AttributeTypeError): | |||||
TimestampWithTimezone( | |||||
timestamp=ts, | |||||
offset=1.0, | |||||
negative_utc=False) | |||||
with pytest.raises(AttributeTypeError): | |||||
TimestampWithTimezone( | |||||
timestamp=ts, | |||||
offset=1, | |||||
negative_utc=0) | |||||
with pytest.raises(ValueError): | |||||
TimestampWithTimezone( | |||||
timestamp=ts, | |||||
offset=1, | |||||
negative_utc=True) | |||||
with pytest.raises(ValueError): | |||||
TimestampWithTimezone( | |||||
timestamp=ts, | |||||
offset=-1, | |||||
negative_utc=True) | |||||
def test_timestampwithtimezone_from_datetime(): | def test_timestampwithtimezone_from_datetime(): | ||||
tz = datetime.timezone(datetime.timedelta(minutes=+60)) | tz = datetime.timezone(datetime.timedelta(minutes=+60)) | ||||
date = datetime.datetime( | date = datetime.datetime( | ||||
2020, 2, 27, 14, 39, 19, tzinfo=tz) | 2020, 2, 27, 14, 39, 19, tzinfo=tz) | ||||
tstz = TimestampWithTimezone.from_datetime(date) | tstz = TimestampWithTimezone.from_datetime(date) | ||||
assert tstz == TimestampWithTimezone( | assert tstz == TimestampWithTimezone( | ||||
▲ Show 20 Lines • Show All 240 Lines • Show Last 20 Lines |