Skip to content

Commit

Permalink
better
Browse files Browse the repository at this point in the history
  • Loading branch information
sazikov-a committed Feb 14, 2024
1 parent 1ae0d5c commit e98b04b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/other_plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ used in a particular test: ``@pytest.mark.now('2016-12-01T12:00:00')`` or
``@pytest.mark.now(enabled=True)``.

If time is not specified in ``@pytest.mark.now``, then
``datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)`` value at the start of test is used as time value
current time at UTC timezone is used at the start of test as time value
until it is modified by calling ``mocked_time.set(...)`` or
``mocked_time.sleep(...)``

Expand Down
4 changes: 1 addition & 3 deletions tests/plugins/test_mocked_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ def test_disabled_mocked_time_raises_on_usage_attempt(mocked_time):
assert not mocked_time.is_enabled

with pytest.raises(mocked_time_module.DisabledUsageError):
mocked_time.set(
datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
)
mocked_time.set(datetime.datetime.now(datetime.timezone.utc))

with pytest.raises(mocked_time_module.DisabledUsageError):
mocked_time.sleep(2)
4 changes: 1 addition & 3 deletions testsuite/mockserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,7 @@ def _log_request(self, started, request, response=None, exc=None):
return
fields = {
'_type': 'mockserver_request',
'timestamp': datetime.datetime.now(datetime.timezone.utc).replace(
tzinfo=None
),
'timestamp': compat.utcnow(),
'method': request.method,
'url': request.rel_url,
}
Expand Down
7 changes: 6 additions & 1 deletion testsuite/plugins/mocked_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ def now(self) -> datetime.datetime:
""":returns: current value of mock time"""
if self._is_enabled:
return self._now
return datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
return utils.compat.utcnow()

def set(self, time: datetime.datetime):
"""Set mock time value"""
if not self._is_enabled:
raise DisabledUsageError(MOCK_TIME_DISABLED_MESSAGE)
self._now = time

if self._now.tzinfo is not None:
self._now = self._now.astimezone(datetime.timezone.utc).replace(
tzinfo=None
)

@property
def is_enabled(self) -> bool:
return self._is_enabled
Expand Down
6 changes: 6 additions & 0 deletions testsuite/utils/compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import contextlib
import datetime


def utcnow() -> datetime.datetime:
return datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)


# Required for python3.6 compatibility
if not hasattr(contextlib, 'asynccontextmanager'):
Expand Down
5 changes: 2 additions & 3 deletions testsuite/utils/json_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from bson import json_util

from testsuite.utils import object_hook as object_hook_util
from testsuite.utils import compat as compat


def loads(string, *args, **kwargs):
Expand Down Expand Up @@ -58,8 +59,6 @@ def default(obj):
def relative_dates_default(obj):
"""Add ``$dateDiff`` hook to ``bson.json_util.default``."""
if isinstance(obj, datetime.datetime):
diff = obj.replace(tzinfo=None) - datetime.datetime.now(
datetime.timezone.utc
).replace(tzinfo=None)
diff = obj.replace(tzinfo=None) - compat.utcnow()
return {'$dateDiff': diff.total_seconds()}
return default(obj)

0 comments on commit e98b04b

Please sign in to comment.