Skip to content

Commit

Permalink
fix ignoring packages that are imported after freezing
Browse files Browse the repository at this point in the history
fixes #420
  • Loading branch information
pegler committed Nov 19, 2021
1 parent 8994558 commit 1c63e4c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
17 changes: 14 additions & 3 deletions freezegun/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,11 @@ def __sub__(self, other):

@classmethod
def today(cls):
result = cls._date_to_freeze() + cls._tz_offset()
if _should_use_real_time():
result = real_date.today()
else:
result = cls._date_to_freeze() + cls._tz_offset()

return date_to_fakedate(result)

@staticmethod
Expand Down Expand Up @@ -383,7 +387,11 @@ def timestamp(self):

@classmethod
def now(cls, tz=None):
now = cls._time_to_freeze() or real_datetime.now()
if _should_use_real_time():
now = real_datetime.now()
else:
now = cls._time_to_freeze() or real_datetime.now()

if tz:
result = tz.fromutc(now.replace(tzinfo=tz)) + cls._tz_offset()
else:
Expand All @@ -407,7 +415,10 @@ def today(cls):

@classmethod
def utcnow(cls):
result = cls._time_to_freeze() or real_datetime.utcnow()
if _should_use_real_time():
result = real_datetime.utcnow()
else:
result = cls._time_to_freeze() or real_datetime.now()
return datetime_to_fakedatetime(result)

@staticmethod
Expand Down
15 changes: 15 additions & 0 deletions tests/another_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,18 @@ def get_fake_gmtime():

def get_fake_strftime():
return fake_strftime


# Getters


def get_datetime_now():
return datetime.now()


def get_datetime_utcnow():
return datetime.utcnow()


def get_date_today():
return date.today()
18 changes: 18 additions & 0 deletions tests/test_class_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,24 @@ def test_import_after_start():
assert another_module.get_fake_strftime() is fake_strftime
del sys.modules['tests.another_module']


def test_ignore_import_after_start():
with freeze_time("2012-01-14", ignore=["tests.another_module"]):
assert "tests.another_module" not in sys.modules.keys()
from tests import another_module

utcnow = another_module.get_datetime_utcnow()
assert utcnow != FakeDatetime(2012, 1, 14)

now = another_module.get_datetime_now()
assert now != FakeDatetime(2012, 1, 14)

today = another_module.get_date_today()
assert today != FakeDate(2012, 1, 14)

del sys.modules["tests.another_module"]


def test_none_as_initial():
with freeze_time() as ft:
ft.move_to('2012-01-14')
Expand Down

0 comments on commit 1c63e4c

Please sign in to comment.