Skip to content

Commit

Permalink
replace freezegun with time_machine (#7656)
Browse files Browse the repository at this point in the history
  • Loading branch information
gruebel authored Oct 18, 2023
1 parent e2b059f commit a7bc5e9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
16 changes: 13 additions & 3 deletions requirements/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ exceptiongroup==1.1.2
# via pytest
filelock==3.12.2
# via virtualenv
freezegun==1.2.2
# via -r requirements/test.in
frozenlist==1.4.0
# via
# -r requirements/runtime-deps.in
Expand All @@ -88,6 +86,10 @@ idna==3.4
# yarl
imagesize==1.4.1
# via sphinx
importlib-metadata==6.8.0
# via sphinx
importlib-resources==6.1.0
# via towncrier
incremental==22.10.0
# via towncrier
iniconfig==2.0.0
Expand Down Expand Up @@ -156,9 +158,11 @@ pytest-cov==4.1.0
pytest-mock==3.11.1
# via -r requirements/test.in
python-dateutil==2.8.2
# via freezegun
# via time-machine
python-on-whales==0.65.0
# via -r requirements/test.in
pytz==2023.3.post1
# via babel
pyyaml==6.0.1
# via pre-commit
re-assert==1.1.0
Expand Down Expand Up @@ -202,6 +206,8 @@ sphinxcontrib-spelling==8.0.0 ; platform_system != "Windows"
# via -r requirements/doc-spelling.in
sphinxcontrib-towncrier==0.3.2a0
# via -r requirements/doc.in
time-machine==2.13.0 ; implementation_name == "cpython"
# via -r requirements/test.in
tomli==2.0.1
# via
# build
Expand Down Expand Up @@ -249,6 +255,10 @@ wheel==0.41.0
# via pip-tools
yarl==1.9.2
# via -r requirements/runtime-deps.in
zipp==3.17.0
# via
# importlib-metadata
# importlib-resources

# The following packages are considered to be unsafe in a requirements file:
pip==23.2.1
Expand Down
16 changes: 13 additions & 3 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ exceptiongroup==1.1.2
# via pytest
filelock==3.12.2
# via virtualenv
freezegun==1.2.2
# via -r requirements/test.in
frozenlist==1.4.0
# via
# -r requirements/runtime-deps.in
Expand All @@ -86,6 +84,10 @@ idna==3.4
# yarl
imagesize==1.4.1
# via sphinx
importlib-metadata==6.8.0
# via sphinx
importlib-resources==6.1.0
# via towncrier
incremental==22.10.0
# via towncrier
iniconfig==2.0.0
Expand Down Expand Up @@ -151,9 +153,11 @@ pytest-cov==4.1.0
pytest-mock==3.11.1
# via -r requirements/test.in
python-dateutil==2.8.2
# via freezegun
# via time-machine
python-on-whales==0.65.0
# via -r requirements/test.in
pytz==2023.3.post1
# via babel
pyyaml==6.0.1
# via pre-commit
re-assert==1.1.0
Expand Down Expand Up @@ -194,6 +198,8 @@ sphinxcontrib-serializinghtml==1.1.5
# via sphinx
sphinxcontrib-towncrier==0.3.2a0
# via -r requirements/doc.in
time-machine==2.13.0 ; implementation_name == "cpython"
# via -r requirements/test.in
tomli==2.0.1
# via
# build
Expand Down Expand Up @@ -241,6 +247,10 @@ wheel==0.41.0
# via pip-tools
yarl==1.9.2
# via -r requirements/runtime-deps.in
zipp==3.17.0
# via
# importlib-metadata
# importlib-resources

# The following packages are considered to be unsafe in a requirements file:
pip==23.2.1
Expand Down
2 changes: 1 addition & 1 deletion requirements/test.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
-c broken-projects.in

coverage
freezegun
mypy; implementation_name == "cpython"
proxy.py
pytest
Expand All @@ -11,5 +10,6 @@ pytest-mock
python-on-whales
re-assert
setuptools-git
time-machine; implementation_name == "cpython"
trustme; platform_machine != "i686" # no 32-bit wheels
wait-for-it
6 changes: 3 additions & 3 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ cryptography==41.0.3
# via trustme
exceptiongroup==1.1.2
# via pytest
freezegun==1.2.2
# via -r requirements/test.in
frozenlist==1.4.0
# via
# -r requirements/runtime-deps.in
Expand Down Expand Up @@ -79,7 +77,7 @@ pytest-cov==4.1.0
pytest-mock==3.11.1
# via -r requirements/test.in
python-dateutil==2.8.2
# via freezegun
# via time-machine
python-on-whales==0.65.0
# via -r requirements/test.in
re-assert==1.1.0
Expand All @@ -92,6 +90,8 @@ setuptools-git==1.2
# via -r requirements/test.in
six==1.16.0
# via python-dateutil
time-machine==2.13.0 ; implementation_name == "cpython"
# via -r requirements/test.in
tomli==2.0.1
# via
# coverage
Expand Down
27 changes: 22 additions & 5 deletions tests/test_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
import itertools
import pathlib
import pickle
import sys
import unittest
from http.cookies import BaseCookie, Morsel, SimpleCookie
from typing import Any
from unittest import mock

import pytest
from freezegun import freeze_time
from yarl import URL

from aiohttp import CookieJar, DummyCookieJar

try:
from time_machine import travel
except ImportError:
travel = None # type: ignore[assignment]


def dump_cookiejar() -> bytes: # pragma: no cover
"""Create pickled data for test_pickle_format()."""
Expand Down Expand Up @@ -418,10 +423,10 @@ def timed_request(self, url: Any, update_time: Any, send_time: Any):
elif isinstance(send_time, float):
send_time = datetime.datetime.fromtimestamp(send_time)

with freeze_time(update_time):
with travel(update_time, tick=False):
self.jar.update_cookies(self.cookies_to_send)

with freeze_time(send_time):
with travel(send_time, tick=False):
cookies_sent = self.jar.filter_cookies(URL(url))

self.jar.clear()
Expand Down Expand Up @@ -608,6 +613,10 @@ def test_path_value(self) -> None:
self.assertEqual(cookies_received["path-cookie"]["path"], "/somepath")
self.assertEqual(cookies_received["wrong-path-cookie"]["path"], "/")

@unittest.skipIf(
sys.implementation.name != "cpython",
reason="time_machine leverages CPython specific pointers https://github.com/adamchainz/time-machine/issues/305",
)
def test_expires(self) -> None:
ts_before = datetime.datetime(
1975, 1, 1, tzinfo=datetime.timezone.utc
Expand All @@ -629,6 +638,10 @@ def test_expires(self) -> None:

self.assertEqual(set(cookies_sent.keys()), {"shared-cookie"})

@unittest.skipIf(
sys.implementation.name != "cpython",
reason="time_machine leverages CPython specific pointers https://github.com/adamchainz/time-machine/issues/305",
)
def test_max_age(self) -> None:
cookies_sent = self.timed_request("http://maxagetest.com/", 1000, 1000)

Expand Down Expand Up @@ -776,6 +789,10 @@ async def test_cookie_jar_clear_all() -> None:
assert len(sut) == 0


@pytest.mark.skipif(
sys.implementation.name != "cpython",
reason="time_machine leverages CPython specific pointers https://github.com/adamchainz/time-machine/issues/305",
)
async def test_cookie_jar_clear_expired():
sut = CookieJar()

Expand All @@ -784,11 +801,11 @@ async def test_cookie_jar_clear_expired():
cookie["foo"] = "bar"
cookie["foo"]["expires"] = "Tue, 1 Jan 1990 12:00:00 GMT"

with freeze_time("1980-01-01"):
with travel("1980-01-01", tick=False):
sut.update_cookies(cookie)

sut.clear(lambda x: False)
with freeze_time("1980-01-01"):
with travel("1980-01-01", tick=False):
assert len(sut) == 0


Expand Down

0 comments on commit a7bc5e9

Please sign in to comment.