Skip to content

Commit 9f06561

Browse files
committed
tests: migrate from freezegun to time-machine
Replace freezegun usage in tests with time-machine decorators/context and switch test dependency to time-machine. This avoids freezegun’s time.time limitations and aligns with recommended migration path.
1 parent 3ac0ae5 commit 9f06561

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Twitter = "https://twitter.com/hynek"
4141

4242
[dependency-groups]
4343
tests = [
44-
"freezegun>=0.2.8",
44+
"time-machine>=2.14.1",
4545
"pretend",
4646
"pytest-asyncio>=0.17",
4747
"pytest>=6.0",

tests/processors/test_renderers.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
import pickle
1111

1212
import pytest
13-
14-
from freezegun import freeze_time
13+
import time_machine
1514

1615
from structlog.processors import (
1716
ExceptionRenderer,
@@ -390,10 +389,9 @@ def test_inserts_utc_unix_timestamp_by_default(self):
390389
ts = TimeStamper()
391390
d = ts(None, None, {})
392391

393-
# freezegun doesn't work with time.time. :(
394392
assert isinstance(d["timestamp"], float)
395393

396-
@freeze_time("1980-03-25 16:00:00")
394+
@time_machine.travel("1980-03-25 16:00:00")
397395
def test_local(self):
398396
"""
399397
Timestamp in local timezone work. Due to historic reasons, the default
@@ -404,7 +402,7 @@ def test_local(self):
404402

405403
assert "1980-03-25T16:00:00" == d["timestamp"]
406404

407-
@freeze_time("1980-03-25 16:00:00")
405+
@time_machine.travel("1980-03-25 16:00:00")
408406
def test_formats(self):
409407
"""
410408
The fmt string is respected.
@@ -414,37 +412,31 @@ def test_formats(self):
414412

415413
assert "1980" == d["timestamp"]
416414

417-
@freeze_time("1980-03-25 16:00:00")
415+
@time_machine.travel(
416+
datetime.datetime(1980, 3, 25, 16, 0, 0, tzinfo=datetime.timezone.utc)
417+
)
418418
def test_inserts_formatted_utc(self):
419419
"""
420420
The fmt string in UTC timezone works.
421-
422-
The exact hours calculated here maybe incorrect because of freezegun bugs:
423-
https://github.com/spulec/freezegun/issues/348
424-
https://github.com/spulec/freezegun/issues/494
425421
"""
426422

427423
ts = TimeStamper(fmt="%Y-%m-%d %H:%M:%S %Z")
428424
d = ts(None, None, {})
429425

430426
assert "1980-03-25 16:00:00 UTC" == d["timestamp"]
431427

432-
@freeze_time("1980-03-25 16:00:00")
428+
@time_machine.travel("1980-03-25 16:00:00")
433429
def test_inserts_formatted_local(self):
434430
"""
435431
The fmt string in local timezone works.
436-
437-
The exact hours calculated here maybe incorrect because of freezegun bugs:
438-
https://github.com/spulec/freezegun/issues/348
439-
https://github.com/spulec/freezegun/issues/494
440432
"""
441433
local_tz = datetime.datetime.now().astimezone().tzname()
442434
ts = TimeStamper(fmt="%Y-%m-%d %H:%M:%S %Z", utc=False)
443435
d = ts(None, None, {})
444436

445437
assert f"1980-03-25 16:00:00 {local_tz}" == d["timestamp"]
446438

447-
@freeze_time("1980-03-25 16:00:00")
439+
@time_machine.travel("1980-03-25 16:00:00")
448440
def test_tz_aware(self):
449441
"""
450442
The timestamp that is used for formatting is timezone-aware.
@@ -455,7 +447,9 @@ def test_tz_aware(self):
455447
assert "" == datetime.datetime.now().strftime("%z") # noqa: DTZ005
456448
assert "" != d["timestamp"]
457449

458-
@freeze_time("1980-03-25 16:00:00")
450+
@time_machine.travel(
451+
datetime.datetime(1980, 3, 25, 16, 0, 0, tzinfo=datetime.timezone.utc)
452+
)
459453
def test_adds_Z_to_iso(self):
460454
"""
461455
stdlib's isoformat is buggy, so we fix it.
@@ -465,7 +459,7 @@ def test_adds_Z_to_iso(self):
465459

466460
assert "1980-03-25T16:00:00Z" == d["timestamp"]
467461

468-
@freeze_time("1980-03-25 16:00:00")
462+
@time_machine.travel("1980-03-25 16:00:00")
469463
def test_key_can_be_specified(self):
470464
"""
471465
Timestamp is stored with the specified key.
@@ -475,7 +469,7 @@ def test_key_can_be_specified(self):
475469

476470
assert "03" == d["month"]
477471

478-
@freeze_time("1980-03-25 16:00:00")
472+
@time_machine.travel("1980-03-25 16:00:00", tick=False)
479473
@pytest.mark.parametrize("fmt", [None, "%Y"])
480474
@pytest.mark.parametrize("utc", [True, False])
481475
@pytest.mark.parametrize("key", [None, "other-key"])
@@ -494,13 +488,15 @@ def test_pickle(self, fmt, utc, key, proto):
494488
None, None, {}
495489
)
496490

497-
def test_apply_freezegun_after_instantiation(self):
491+
def test_apply_time_machine_after_instantiation(self):
498492
"""
499493
Freezing time after instantiation of TimeStamper works.
500494
"""
501495
ts = TimeStamper(fmt="iso", utc=False)
502496

503-
with freeze_time("1980-03-25 16:00:00", tz_offset=1):
497+
# Simulate a different local time by traveling to a different timestamp
498+
# after the stamper was created.
499+
with time_machine.travel("1980-03-25 17:00:00"):
504500
d = ts(None, None, {})
505501

506502
assert "1980-03-25T17:00:00" == d["timestamp"]

tests/test_generic.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import pickle
77

88
import pytest
9-
10-
from freezegun import freeze_time
9+
import time_machine
1110

1211
from structlog._config import _CONFIG
1312
from structlog._generic import BoundLogger
@@ -40,7 +39,7 @@ def test_caches(self):
4039
assert "msg" in b.__dict__
4140

4241
@pytest.mark.parametrize("proto", range(3, pickle.HIGHEST_PROTOCOL + 1))
43-
@freeze_time("2023-05-22 17:00")
42+
@time_machine.travel("2023-05-22 17:00", tick=False)
4443
def test_pickle(self, proto):
4544
"""
4645
Can be pickled and unpickled.

0 commit comments

Comments
 (0)