Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make datetime tests pass without requiring UTC system timezone #81

Merged
merged 7 commits into from
Nov 14, 2023
Merged
13 changes: 9 additions & 4 deletions dirty_equals/_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ def __init__(
Examples of basic usage:

```py title="IsDatetime"
from datetime import datetime
from datetime import datetime, timezone

from dirty_equals import IsDatetime

y2k = datetime(2000, 1, 1)
assert datetime(2000, 1, 1) == IsDatetime(approx=y2k)
# Note: this requires the system timezone to be UTC
assert 946684800.123 == IsDatetime(approx=y2k, unix_number=True)
assert 946684800.123 == IsDatetime(
approx=datetime(2000, 1, 1, tzinfo=timezone.utc), unix_number=True
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some feedback on the devex with pytest-examples linting: It was annoying that I had to fix linting before I could see the actual test failure. I think that test_docstrings should run the actual test before linting.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can automate the updates uses --update-examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it took me a while to figure that out, and it's an extra obstacle in development, e.g. configuring my IDE. So I think reordering test_docstrings might still be slightly helpful. It could also be good for the Makefile to have an entry which runs tests with --update-examples, probably without coverage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the reordering, opened #83 to discuss the rest, so I think this can be resolved now.

assert datetime(2000, 1, 1, 0, 0, 9) == IsDatetime(approx=y2k, delta=10)
assert '2000-01-01T00:00' == IsDatetime(approx=y2k, iso_string=True)

Expand Down Expand Up @@ -85,7 +86,11 @@ def prepare(self, other: Any) -> datetime:
dt = other
elif isinstance(other, (float, int)):
if self.unix_number:
dt = datetime.fromtimestamp(other)
if self.approx is not None and self.approx.tzinfo is not None:
alexmojaki marked this conversation as resolved.
Show resolved Hide resolved
tz = self.approx.tzinfo
else:
tz = None
dt = datetime.fromtimestamp(other, tz=tz)
else:
raise TypeError('numbers not allowed')
elif isinstance(other, str):
Expand Down
2 changes: 1 addition & 1 deletion requirements/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ packaging==23.1
# mkdocs
pathspec==0.11.1
# via black
platformdirs==3.4.0
platformdirs==3.5.0
samuelcolvin marked this conversation as resolved.
Show resolved Hide resolved
# via black
pygments==2.15.1
# via mkdocs-material
Expand Down
2 changes: 1 addition & 1 deletion requirements/linting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ packaging==23.1
# via black
pathspec==0.11.1
# via black
platformdirs==3.4.0
platformdirs==3.5.0
# via
# black
# virtualenv
Expand Down
16 changes: 12 additions & 4 deletions tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@
'value,dirty,expect_match',
[
pytest.param(datetime(2000, 1, 1), IsDatetime(approx=datetime(2000, 1, 1)), True, id='same'),
# Note: this requires the system timezone to be UTC
pytest.param(946684800, IsDatetime(approx=datetime(2000, 1, 1), unix_number=True), True, id='unix-int'),
# Note: this requires the system timezone to be UTC
pytest.param(946684800.123, IsDatetime(approx=datetime(2000, 1, 1), unix_number=True), True, id='unix-float'),
pytest.param(
946684800,
IsDatetime(approx=datetime(2000, 1, 1, tzinfo=timezone.utc), unix_number=True),
True,
id='unix-int',
alexmojaki marked this conversation as resolved.
Show resolved Hide resolved
),
pytest.param(
946684800.123,
IsDatetime(approx=datetime(2000, 1, 1, tzinfo=timezone.utc), unix_number=True),
True,
id='unix-float',
),
pytest.param(946684800, IsDatetime(approx=datetime(2000, 1, 1)), False, id='unix-different'),
pytest.param(
'2000-01-01T00:00', IsDatetime(approx=datetime(2000, 1, 1), iso_string=True), True, id='iso-string-true'
Expand Down
10 changes: 9 additions & 1 deletion tests/test_docs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import platform
from pathlib import Path

import pytest
from pytest_examples import CodeExample, EvalExample, find_examples

root_dir = Path(__file__).parent.parent

examples = find_examples(
str(root_dir / 'dirty_equals'),
alexmojaki marked this conversation as resolved.
Show resolved Hide resolved
str(root_dir / 'docs'),
)


@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason='PyPy does not allow metaclass dunder methods')
@pytest.mark.parametrize('example', find_examples('dirty_equals', 'docs'), ids=str)
@pytest.mark.parametrize('example', examples, ids=str)
def test_docstrings(example: CodeExample, eval_example: EvalExample):
prefix_settings = example.prefix_settings()
# E711 and E712 refer to `== True` and `== None` and need to be ignored
Expand Down