-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from MartinGotelli/timestamp_matcher
feat: Add timestamp matcher
- Loading branch information
Showing
11 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
is_number, | ||
is_strict_dict, | ||
is_string, | ||
is_timestamp, | ||
is_uuid, | ||
not_empty_string, | ||
one_of, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from datetime import datetime | ||
from typing import Any | ||
|
||
from pytest_matchers.matchers import Matcher, Number | ||
from pytest_matchers.matchers.between import between_matcher | ||
from pytest_matchers.matchers.matcher_factory import matcher | ||
from pytest_matchers.utils.matcher_utils import matches_or_none | ||
from pytest_matchers.utils.repr_utils import concat_reprs | ||
|
||
|
||
def _as_timestamp(value: float | datetime) -> float: | ||
if isinstance(value, datetime): | ||
return value.timestamp() | ||
return value | ||
|
||
|
||
@matcher | ||
class Timestamp(Matcher): | ||
def __init__(self, *, min_value: float | datetime = None, max_value: float | datetime = None): | ||
super().__init__() | ||
self._instance_matcher = Number() | ||
self._limit_matcher = between_matcher( | ||
_as_timestamp(min_value), | ||
_as_timestamp(max_value), | ||
None, | ||
None, | ||
None, | ||
) | ||
|
||
def matches(self, value: Any) -> bool: | ||
return self._instance_matcher == value and matches_or_none(self._limit_matcher, value) | ||
|
||
def __repr__(self) -> str: | ||
return concat_reprs("To be a timestamp", self._limit_matcher) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from datetime import datetime, timedelta | ||
|
||
from pytest_matchers.matchers import Timestamp | ||
|
||
|
||
def test_create(): | ||
matcher = Timestamp() | ||
assert isinstance(matcher, Timestamp) | ||
matcher = Timestamp(min_value=0, max_value=1) | ||
assert isinstance(matcher, Timestamp) | ||
matcher = Timestamp(min_value=0) | ||
assert isinstance(matcher, Timestamp) | ||
|
||
|
||
def test_repr(): | ||
matcher = Timestamp() | ||
assert repr(matcher) == "To be a timestamp" | ||
matcher = Timestamp(min_value=0) | ||
assert repr(matcher) == "To be a timestamp greater or equal than 0" | ||
matcher = Timestamp(max_value=1) | ||
assert repr(matcher) == "To be a timestamp lower or equal than 1" | ||
matcher = Timestamp(min_value=0, max_value=1) | ||
assert repr(matcher) == "To be a timestamp between 0 and 1" | ||
|
||
|
||
def test_matches(): | ||
matcher = Timestamp() | ||
assert matcher == 0.0 | ||
assert matcher == 1.0 | ||
assert matcher != "string" | ||
assert matcher == 20 | ||
assert matcher != ["string"] | ||
assert matcher != datetime.now() | ||
assert matcher == datetime.now().timestamp() | ||
|
||
|
||
def test_matches_with_limit(): | ||
now_timestamp = datetime.now().timestamp() | ||
tomorrow_timestamp = datetime.now().timestamp() + 86400 | ||
matcher = Timestamp(min_value=now_timestamp, max_value=tomorrow_timestamp) | ||
assert matcher == now_timestamp | ||
assert matcher == now_timestamp + 2000 | ||
assert matcher == tomorrow_timestamp | ||
assert matcher == int(now_timestamp + 2000) | ||
assert matcher != now_timestamp - 86400 | ||
assert matcher != tomorrow_timestamp + 86400 | ||
|
||
|
||
def test_matches_with_datetime_limit(): | ||
now = datetime.now() | ||
tomorrow = now + timedelta(days=1) | ||
matcher = Timestamp(min_value=now, max_value=tomorrow) | ||
assert matcher == now.timestamp() | ||
assert matcher == now.timestamp() + 2000 | ||
assert matcher == tomorrow.timestamp() | ||
assert matcher == int(now.timestamp() + 2000) | ||
assert matcher != now.timestamp() - 86400 | ||
assert matcher != tomorrow.timestamp() + 86400 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters