Skip to content

Commit

Permalink
Merge pull request #2716 from mgorny/freezegun
Browse files Browse the repository at this point in the history
tests: Use freezegun for time mocking to fix pypy3 compatibility
  • Loading branch information
jku authored Oct 10, 2024
2 parents 014f6f0 + ee27bcc commit cf05c11
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
3 changes: 3 additions & 0 deletions requirements/lint.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
# are pinned to prevent unexpected linting failures when tools update)
ruff==0.6.9
mypy==1.11.2

# Required for type stubs
freezegun==1.5.1
1 change: 1 addition & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

# coverage measurement
coverage==7.6.1
freezegun==1.5.1
34 changes: 15 additions & 19 deletions tests/test_updater_top_level_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import unittest
from datetime import timezone
from typing import Iterable, Optional
from unittest.mock import MagicMock, Mock, call, patch
from unittest.mock import MagicMock, call, patch

import freezegun

from tests import utils
from tests.repository_simulator import RepositorySimulator
Expand Down Expand Up @@ -306,8 +308,7 @@ def test_new_timestamp_unsigned(self) -> None:

self._assert_files_exist([Root.type])

@patch.object(datetime, "datetime", wraps=datetime.datetime)
def test_expired_timestamp_version_rollback(self, mock_time: Mock) -> None:
def test_expired_timestamp_version_rollback(self) -> None:
"""Verifies that local timestamp is used in rollback checks even if it is expired.
The timestamp updates and rollback checks are performed
Expand All @@ -331,19 +332,17 @@ def test_expired_timestamp_version_rollback(self, mock_time: Mock) -> None:

self.sim.timestamp.version = 1

mock_time.now.return_value = datetime.datetime.now(
timezone.utc
) + datetime.timedelta(days=18)
patcher = patch("datetime.datetime", mock_time)
patcher = freezegun.freeze_time(
datetime.datetime.now(timezone.utc) + datetime.timedelta(days=18)
)
# Check that a rollback protection is performed even if
# local timestamp has expired
with patcher, self.assertRaises(BadVersionNumberError):
self._run_refresh()

self._assert_version_equals(Timestamp.type, 2)

@patch.object(datetime, "datetime", wraps=datetime.datetime)
def test_expired_timestamp_snapshot_rollback(self, mock_time: Mock) -> None:
def test_expired_timestamp_snapshot_rollback(self) -> None:
"""Verifies that rollback protection is done even if local timestamp has expired.
The snapshot updates and rollback protection checks are performed
Expand All @@ -370,10 +369,9 @@ def test_expired_timestamp_snapshot_rollback(self, mock_time: Mock) -> None:
self.sim.update_snapshot()
self.sim.timestamp.expires = now + datetime.timedelta(days=21)

mock_time.now.return_value = datetime.datetime.now(
timezone.utc
) + datetime.timedelta(days=18)
patcher = patch("datetime.datetime", mock_time)
patcher = freezegun.freeze_time(
datetime.datetime.now(timezone.utc) + datetime.timedelta(days=18)
)
# Assert that rollback protection is done even if
# local timestamp has expired
with patcher, self.assertRaises(BadVersionNumberError):
Expand Down Expand Up @@ -736,8 +734,7 @@ def test_load_metadata_from_cache(self, wrapped_open: MagicMock) -> None:
expected_calls = [("root", 2), ("timestamp", None)]
self.assertListEqual(self.sim.fetch_tracker.metadata, expected_calls)

@patch.object(datetime, "datetime", wraps=datetime.datetime)
def test_expired_metadata(self, mock_time: Mock) -> None:
def test_expired_metadata(self) -> None:
"""Verifies that expired local timestamp/snapshot can be used for
updating from remote.
Expand All @@ -761,10 +758,9 @@ def test_expired_metadata(self, mock_time: Mock) -> None:

# Mocking time so that local timestam has expired
# but the new timestamp has not
mock_time.now.return_value = datetime.datetime.now(
timezone.utc
) + datetime.timedelta(days=18)
with patch("datetime.datetime", mock_time):
with freezegun.freeze_time(
datetime.datetime.now(timezone.utc) + datetime.timedelta(days=18)
):
self._run_refresh()

# Assert that the final version of timestamp/snapshot is version 2
Expand Down

0 comments on commit cf05c11

Please sign in to comment.