From f92dd37226629e5bf23a7d1d4ac007ac48337a8a Mon Sep 17 00:00:00 2001 From: JulianMaurin Date: Tue, 19 Jul 2022 21:25:36 +0200 Subject: [PATCH 1/3] test(rest_framework.throttling.SimpleRateThrottle): reproduce the error --- tests/fake_module_lazy.py | 19 +++++++++++++++++++ tests/test_class_import.py | 12 ++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tests/fake_module_lazy.py diff --git a/tests/fake_module_lazy.py b/tests/fake_module_lazy.py new file mode 100644 index 00000000..3e27371b --- /dev/null +++ b/tests/fake_module_lazy.py @@ -0,0 +1,19 @@ +""" +Lazy module aims to reproduce the context of late loaded modules +Eg. testing a django view using the test client +""" + +def load(): + import time + + class TimeAsClassAttribute: + """ + Reproduce the behaviour of: rest_framework.throttling.SimpleRateThrottle + see: https://github.com/encode/django-rest-framework/blob/3.13.1/rest_framework/throttling.py#L63 + """ + _time = time.time + + def call_time(self): + return self._time() + + globals()["TimeAsClassAttribute"] = TimeAsClassAttribute diff --git a/tests/test_class_import.py b/tests/test_class_import.py index 709dc6e1..6e49b331 100644 --- a/tests/test_class_import.py +++ b/tests/test_class_import.py @@ -1,5 +1,8 @@ import time import sys + +import pytest + from .fake_module import ( fake_date_function, fake_datetime_function, @@ -9,6 +12,7 @@ fake_time_function, ) from . import fake_module +from . import fake_module_lazy from freezegun import freeze_time from freezegun.api import ( FakeDatetime, @@ -130,6 +134,14 @@ def test_fake_strftime_function(): assert fake_strftime_function() == '2012' +@freeze_time("2022-01-01 12:00:00") +def test_fake_time_function_as_class_attribute(): + fake_module_lazy.load() + + with pytest.raises(TypeError, match='fake_time\(\) takes 0 positional arguments but 1 was given'): + assert fake_module_lazy.TimeAsClassAttribute().call_time() + + def test_import_after_start(): with freeze_time('2012-01-14'): assert 'tests.another_module' not in sys.modules.keys() From 4313a88f14c948c0154c8f18bbcd3fba54040c1f Mon Sep 17 00:00:00 2001 From: JulianMaurin Date: Tue, 19 Jul 2022 21:28:27 +0200 Subject: [PATCH 2/3] fix(fake_time): accept any args/kwargs --- freezegun/api.py | 2 +- tests/test_class_import.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/freezegun/api.py b/freezegun/api.py index 32ce9848..22b10bd2 100644 --- a/freezegun/api.py +++ b/freezegun/api.py @@ -170,7 +170,7 @@ def get_current_time(): return freeze_factories[-1]() -def fake_time(): +def fake_time(*args, **kwargs): if _should_use_real_time(): return real_time() current_time = get_current_time() diff --git a/tests/test_class_import.py b/tests/test_class_import.py index 6e49b331..53bfbf45 100644 --- a/tests/test_class_import.py +++ b/tests/test_class_import.py @@ -134,12 +134,15 @@ def test_fake_strftime_function(): assert fake_strftime_function() == '2012' -@freeze_time("2022-01-01 12:00:00") +@freeze_time("2022-01-01") def test_fake_time_function_as_class_attribute(): + local_time = datetime.datetime(2022, 1, 1) + utc_time = local_time - datetime.timedelta(seconds=time.timezone) + expected_timestamp = time.mktime(utc_time.timetuple()) + fake_module_lazy.load() - with pytest.raises(TypeError, match='fake_time\(\) takes 0 positional arguments but 1 was given'): - assert fake_module_lazy.TimeAsClassAttribute().call_time() + assert fake_module_lazy.TimeAsClassAttribute().call_time() == expected_timestamp def test_import_after_start(): From 9d2b92f7faedec016ab17b088ebf330024ddf15f Mon Sep 17 00:00:00 2001 From: JulianMaurin Date: Wed, 20 Jul 2022 13:50:58 +0200 Subject: [PATCH 3/3] refacto(test_class_import): factorize tests introducting the _date_to_time function --- tests/test_class_import.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/test_class_import.py b/tests/test_class_import.py index 53bfbf45..b9b2a3db 100644 --- a/tests/test_class_import.py +++ b/tests/test_class_import.py @@ -37,10 +37,7 @@ def test_import_date_works(): @freeze_time("2012-01-14") def test_import_time(): - local_time = datetime.datetime(2012, 1, 14) - utc_time = local_time - datetime.timedelta(seconds=time.timezone) - expected_timestamp = time.mktime(utc_time.timetuple()) - assert fake_time_function() == expected_timestamp + assert fake_time_function() == _date_to_time(2012, 1, 14) def test_start_and_stop_works(): @@ -136,13 +133,10 @@ def test_fake_strftime_function(): @freeze_time("2022-01-01") def test_fake_time_function_as_class_attribute(): - local_time = datetime.datetime(2022, 1, 1) - utc_time = local_time - datetime.timedelta(seconds=time.timezone) - expected_timestamp = time.mktime(utc_time.timetuple()) fake_module_lazy.load() - assert fake_module_lazy.TimeAsClassAttribute().call_time() == expected_timestamp + assert fake_module_lazy.TimeAsClassAttribute().call_time() == _date_to_time(2022, 1, 1) def test_import_after_start(): @@ -199,3 +193,9 @@ def test_none_as_initial(): with freeze_time() as ft: ft.move_to('2012-01-14') assert fake_strftime_function() == '2012' + + +def _date_to_time(year, month, day): + local_time = datetime.datetime(year, month, day) + utc_time = local_time - datetime.timedelta(seconds=time.timezone) + return time.mktime(utc_time.timetuple())