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

tests: Use freezegun for time mocking to fix pypy3 compatibility #2716

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.7
mypy==1.11.2

# Required for type stubs
freezegun==1.5.1
Comment on lines +12 to +13
Copy link
Contributor Author

Choose a reason for hiding this comment

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

By the way, for a similar reason (and especially that types can effectively be different across Python versions), I've switched typing checks in my private projects from "linting" environment to the main test environment, and therefore I run them for every impl separately.

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