Skip to content

Commit

Permalink
Drop bespoke time-freezing fixture and adopt FreezeGun (#5097)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb authored Oct 28, 2024
1 parent 6f01e6d commit 34f4256
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 44 deletions.
1 change: 1 addition & 0 deletions catalog/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
-r requirements-prod.txt

flaky==3.8.1
freezegun==1.5.1
ipython
pook==2.0.0
pytest-env
Expand Down
42 changes: 0 additions & 42 deletions catalog/tests/dags/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,45 +166,3 @@ def requests_get_mock():
with mock.patch("common.urls.requests_get", autospec=True) as mock_get:
mock_get.side_effect = _make_response
yield


@pytest.fixture
def freeze_time(monkeypatch):
"""
Patch the `datetime.datetime.now` function to return a fixed, settable time.
This effectively freezes time.
https://stackoverflow.com/a/28073449 CC BY-SA 3.0
"""
import datetime

original = datetime.datetime

class FreezeMeta(type):
def __instancecheck__(self, instance):
if isinstance(instance, (original, Freeze)):
return True

class Freeze(datetime.datetime):
__metaclass__ = FreezeMeta

@classmethod
def freeze(cls, val):
cls.frozen = val

@classmethod
def now(cls):
return cls.frozen

@classmethod
def delta(cls, timedelta=None, **kwargs):
"""Move time fwd/bwd by the delta."""
from datetime import timedelta as td

if not timedelta:
timedelta = td(**kwargs)
cls.frozen += timedelta

monkeypatch.setattr(datetime, "datetime", Freeze)
Freeze.freeze(original.now())
return Freeze
6 changes: 4 additions & 2 deletions catalog/tests/dags/maintenance/test_pr_review_reminders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime, timedelta

import pytest
from freezegun import freeze_time
from requests import HTTPError, Request, Response

from maintenance.pr_review_reminders.pr_review_reminders import (
Expand Down Expand Up @@ -147,8 +148,9 @@ def patch_gh_fn(fn, impl):


@pytest.fixture(autouse=True)
def freeze_friday(freeze_time):
freeze_time.freeze(FRIDAY)
def freeze_friday():
with freeze_time(FRIDAY):
yield


def _setup_branch_protection(github: dict, pr: dict, min_required_approvals: int = 2):
Expand Down

0 comments on commit 34f4256

Please sign in to comment.