From 748b552ca62829795a80b3b7f16bc7af53e625b9 Mon Sep 17 00:00:00 2001 From: Nicolas Drebenstedt Date: Fri, 15 Dec 2023 16:37:07 +0100 Subject: [PATCH] More cov for types --- mex/common/types/timestamp.py | 4 ++-- tests/types/test_link.py | 7 +----- tests/types/test_timestamp.py | 45 +++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/mex/common/types/timestamp.py b/mex/common/types/timestamp.py index a3e9d82a..326b3722 100644 --- a/mex/common/types/timestamp.py +++ b/mex/common/types/timestamp.py @@ -233,7 +233,7 @@ def __eq__(self, other: Any) -> bool: try: other = self.validate(other) except TypeError: - return NotImplemented + raise NotImplementedError() return bool( self.date_time == other.date_time and self.precision == other.precision ) @@ -243,7 +243,7 @@ def __gt__(self, other: Any) -> bool: try: other = self.validate(other) except TypeError: - return NotImplemented + raise NotImplementedError() return bool(self.date_time > other.date_time) def __str__(self) -> str: diff --git a/tests/types/test_link.py b/tests/types/test_link.py index e5492347..8e96d5c5 100644 --- a/tests/types/test_link.py +++ b/tests/types/test_link.py @@ -1,5 +1,4 @@ -import pytest -from pydantic import BaseModel, ValidationError +from pydantic import BaseModel from mex.common.types import Link, LinkLanguage @@ -40,10 +39,6 @@ class DummyModel(BaseModel): } } - # invalid data - with pytest.raises(ValidationError): - DummyModel.model_validate(["lists", "are", "not", "valid"]) - def test_rendering_as_string() -> None: # plain link diff --git a/tests/types/test_timestamp.py b/tests/types/test_timestamp.py index c1f4a4fb..cb24f793 100644 --- a/tests/types/test_timestamp.py +++ b/tests/types/test_timestamp.py @@ -2,6 +2,7 @@ from typing import Any import pytest +from pytz import timezone from mex.common.types import CET, UTC, Timestamp @@ -59,6 +60,11 @@ def test_timestamp_validation_errors(value: Any, message: str) -> None: {}, "1999-01-20T21:00:00Z", ), + ( + ("2016-06-10T21:42:24.76073899Z",), + {}, + "2016-06-10T21:42:24Z", + ), ( (date(2020, 3, 22),), {}, @@ -69,6 +75,15 @@ def test_timestamp_validation_errors(value: Any, message: str) -> None: {}, "2020-03-22T13:30:58Z", ), + ( + ( + datetime( + 2020, 3, 22, 14, 30, 58, tzinfo=timezone("America/Los_Angeles") + ), + ), + {}, + "2020-03-22T22:23:58Z", + ), ( (Timestamp(2004, 11),), {}, @@ -86,8 +101,10 @@ def test_timestamp_validation_errors(value: Any, message: str) -> None: "date string", "time string", "padded time", + "nano seconds", "date", "datetime", + "pacific time", "timestamp", ], ) @@ -96,3 +113,31 @@ def test_timestamp_parsing( ) -> None: timestamp = Timestamp(*args, **kwargs) assert str(timestamp) == expected + + +def test_timestamp_eq() -> None: + assert Timestamp(2004) == Timestamp("2004") + assert Timestamp(2004, 11) == Timestamp(2004, 11) + assert Timestamp(2004, 11, 2) == "2004-11-02" + assert Timestamp(2020, 3, 22, 14, 30, 58, 0) == datetime(2020, 3, 22, 14, 30, 58, 0) + + with pytest.raises(NotImplementedError): + assert Timestamp(2005) == object() + + +def test_timestamp_gt() -> None: + assert Timestamp(2004) > Timestamp("2003") + assert Timestamp(2004, 11) < "2013-10-02" + assert Timestamp(2004, 11) <= Timestamp(2004, 12) + assert Timestamp(2020, 3, 22, 14, 30, 58) >= datetime(2020, 3, 22, 14, 29) + + with pytest.raises(NotImplementedError): + assert Timestamp(2005) > object() + + +def test_timestamp_str() -> None: + assert str(Timestamp(2004, 11, 26)) == "2004-11-26" + + +def test_timestamp_repr() -> None: + assert repr(Timestamp(2018, 3, 2, 13, 0, 1)) == 'Timestamp("2018-03-02T12:00:01Z")'