From a9d66a90d4641efeda665d0050de28ba73fadd43 Mon Sep 17 00:00:00 2001 From: Andrew Yoo Date: Fri, 11 Nov 2022 10:21:26 -0800 Subject: [PATCH 01/10] Add is_dst parameter based on time's tm_isdst field instead of passing in None all the time --- django_celery_beat/utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/django_celery_beat/utils.py b/django_celery_beat/utils.py index efd309ac..2c003305 100644 --- a/django_celery_beat/utils.py +++ b/django_celery_beat/utils.py @@ -3,6 +3,7 @@ # -- a recursive loader import! from django.conf import settings from django.utils import timezone +import time is_aware = timezone.is_aware # celery schedstate return None will make it not work @@ -24,7 +25,15 @@ def make_aware(value): else: # naive datetimes are assumed to be in local timezone. if timezone.is_naive(value): - value = timezone.make_aware(value, timezone.get_default_timezone()) + tm_isdst = time.localtime().tm_isdst + + # tm_isdst may return -1 if it cannot be determined + if tm_isdst == -1: + is_dst = None + else: + is_dst = bool(tm_isdst) + + value = timezone.make_aware(value, timezone.get_default_timezone(), is_dst=is_dst) return value From b0749b2df3616b4704d47ccbca3c41a8109ff985 Mon Sep 17 00:00:00 2001 From: Andrew Yoo Date: Mon, 14 Nov 2022 11:54:05 -0800 Subject: [PATCH 02/10] add unit tests --- t/unit/test_utils.py | 114 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 t/unit/test_utils.py diff --git a/t/unit/test_utils.py b/t/unit/test_utils.py new file mode 100644 index 00000000..af092a50 --- /dev/null +++ b/t/unit/test_utils.py @@ -0,0 +1,114 @@ +from unittest import mock +from django.test import TestCase +from datetime import datetime +import time + +from django_celery_beat import utils +from django.utils import timezone + +class UtilsTest(TestCase): + + @mock.patch('django_celery_beat.utils.timezone.localtime') + @mock.patch('django_celery_beat.utils.timezone.get_default_timezone') + @mock.patch('django_celery_beat.utils.timezone.make_aware') + @mock.patch('django_celery_beat.utils.time.localtime') + @mock.patch('django_celery_beat.utils.timezone.is_naive') + @mock.patch('django_celery_beat.utils.getattr') + def test_make_aware_use_tz_naive(self, mock_getattr, mock_is_naive, mock_localtime_1, mock_make_aware, mock_get_default_timezone, mock_localtime_2): + dt = datetime(2022, 11, 6, 1, 15, 0) + mock_getattr.return_value = True + mock_is_naive.return_value = True + mock_get_default_timezone.return_value = "America/Los_Angeles" + mock_localtime_2.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 0]) + mock_make_aware.return_value = dt + + self.assertEquals(utils.make_aware(dt), mock_localtime_2.return_value) + + mock_localtime_1.assert_not_called() + mock_make_aware.assert_called_with(dt, timezone.utc) + mock_get_default_timezone.assert_called() + mock_localtime_2.assert_called_with(dt, "America/Los_Angeles") + + @mock.patch('django_celery_beat.utils.timezone.localtime') + @mock.patch('django_celery_beat.utils.timezone.get_default_timezone') + @mock.patch('django_celery_beat.utils.timezone.make_aware') + @mock.patch('django_celery_beat.utils.time.localtime') + @mock.patch('django_celery_beat.utils.timezone.is_naive') + @mock.patch('django_celery_beat.utils.getattr') + def test_make_aware_use_tz_not_naive(self, mock_getattr, mock_is_naive, mock_localtime_1, mock_make_aware, mock_get_default_timezone, mock_localtime_2): + dt = datetime(2022, 11, 6, 1, 15, 0) + mock_getattr.return_value = True + mock_is_naive.return_value = False + mock_get_default_timezone.return_value = "America/Los_Angeles" + mock_localtime_2.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 0]) + mock_make_aware.return_value = dt + + self.assertEquals(utils.make_aware(dt), mock_localtime_2.return_value) + + mock_localtime_1.assert_not_called() + mock_make_aware.assert_not_called() + mock_get_default_timezone.assert_called() + mock_localtime_2.assert_called_with(dt, "America/Los_Angeles") + + @mock.patch('django_celery_beat.utils.timezone.localtime') + @mock.patch('django_celery_beat.utils.timezone.get_default_timezone') + @mock.patch('django_celery_beat.utils.timezone.make_aware') + @mock.patch('django_celery_beat.utils.time.localtime') + @mock.patch('django_celery_beat.utils.timezone.is_naive') + @mock.patch('django_celery_beat.utils.getattr') + def test_make_aware_not_use_tz_naive_dst(self, mock_getattr, mock_is_naive, mock_localtime_1, mock_make_aware, mock_get_default_timezone, mock_localtime_2): + dt = datetime(2022, 11, 6, 1, 15, 0) + mock_getattr.return_value = False + mock_is_naive.return_value = True + mock_get_default_timezone.return_value = "America/Los_Angeles" + mock_localtime_1.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 1]) + mock_make_aware.return_value = dt + + self.assertEquals(utils.make_aware(dt), dt) + + mock_localtime_1.assert_called_with() + mock_make_aware.assert_called_with(dt, "America/Los_Angeles", is_dst=True) + mock_get_default_timezone.assert_called() + mock_localtime_2.assert_not_called() + + @mock.patch('django_celery_beat.utils.timezone.localtime') + @mock.patch('django_celery_beat.utils.timezone.get_default_timezone') + @mock.patch('django_celery_beat.utils.timezone.make_aware') + @mock.patch('django_celery_beat.utils.time.localtime') + @mock.patch('django_celery_beat.utils.timezone.is_naive') + @mock.patch('django_celery_beat.utils.getattr') + def test_make_aware_not_use_tz_naive_not_dst(self, mock_getattr, mock_is_naive, mock_localtime_1, mock_make_aware, mock_get_default_timezone, mock_localtime_2): + dt = datetime(2022, 11, 6, 1, 15, 0) + mock_getattr.return_value = False + mock_is_naive.return_value = True + mock_get_default_timezone.return_value = "America/Los_Angeles" + mock_localtime_1.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 0]) + mock_make_aware.return_value = dt + + self.assertEquals(utils.make_aware(dt), dt) + + mock_localtime_1.assert_called_with() + mock_make_aware.assert_called_with(dt, "America/Los_Angeles", is_dst=False) + mock_get_default_timezone.assert_called() + mock_localtime_2.assert_not_called() + + @mock.patch('django_celery_beat.utils.timezone.localtime') + @mock.patch('django_celery_beat.utils.timezone.get_default_timezone') + @mock.patch('django_celery_beat.utils.timezone.make_aware') + @mock.patch('django_celery_beat.utils.time.localtime') + @mock.patch('django_celery_beat.utils.timezone.is_naive') + @mock.patch('django_celery_beat.utils.getattr') + def test_make_aware_not_use_tz_not_naive_dst(self, mock_getattr, mock_is_naive, mock_localtime_1, mock_make_aware, mock_get_default_timezone, mock_localtime_2): + dt = datetime(2022, 11, 6, 1, 15, 0) + mock_getattr.return_value = False + mock_is_naive.return_value = False + mock_get_default_timezone.return_value = "America/Los_Angeles" + mock_localtime_1.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 0]) + mock_make_aware.return_value = dt + + self.assertEquals(utils.make_aware(dt), dt) + + mock_localtime_1.assert_not_called() + mock_make_aware.assert_not_called() + mock_get_default_timezone.assert_not_called() + mock_localtime_2.assert_not_called() From e73f397a460e7c925fc8f09fcf963f3a466a4b83 Mon Sep 17 00:00:00 2001 From: Andrew Yoo Date: Mon, 14 Nov 2022 11:56:18 -0800 Subject: [PATCH 03/10] Add -1 case --- t/unit/test_utils.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/t/unit/test_utils.py b/t/unit/test_utils.py index af092a50..566e9050 100644 --- a/t/unit/test_utils.py +++ b/t/unit/test_utils.py @@ -98,6 +98,25 @@ def test_make_aware_not_use_tz_naive_not_dst(self, mock_getattr, mock_is_naive, @mock.patch('django_celery_beat.utils.time.localtime') @mock.patch('django_celery_beat.utils.timezone.is_naive') @mock.patch('django_celery_beat.utils.getattr') + def test_make_aware_not_use_tz_naive_negative_dst(self, mock_getattr, mock_is_naive, mock_localtime_1, mock_make_aware, mock_get_default_timezone, mock_localtime_2): + dt = datetime(2022, 11, 6, 1, 15, 0) + mock_getattr.return_value = False + mock_is_naive.return_value = True + mock_get_default_timezone.return_value = "America/Los_Angeles" + mock_localtime_1.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, -1]) + mock_make_aware.return_value = dt + + self.assertEquals(utils.make_aware(dt), dt) + + mock_localtime_1.assert_called_with() + mock_make_aware.assert_called_with(dt, "America/Los_Angeles", is_dst=None) + mock_get_default_timezone.assert_called() + @mock.patch('django_celery_beat.utils.timezone.localtime') + @mock.patch('django_celery_beat.utils.timezone.get_default_timezone') + @mock.patch('django_celery_beat.utils.timezone.make_aware') + @mock.patch('django_celery_beat.utils.time.localtime') + @mock.patch('django_celery_beat.utils.timezone.is_naive') + @mock.patch('django_celery_beat.utils.getattr') def test_make_aware_not_use_tz_not_naive_dst(self, mock_getattr, mock_is_naive, mock_localtime_1, mock_make_aware, mock_get_default_timezone, mock_localtime_2): dt = datetime(2022, 11, 6, 1, 15, 0) mock_getattr.return_value = False From d142f883e2701ad00c4734e7d54ce8f34bee29c2 Mon Sep 17 00:00:00 2001 From: Andrew Yoo Date: Mon, 14 Nov 2022 15:39:07 -0800 Subject: [PATCH 04/10] add line --- t/unit/test_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/t/unit/test_utils.py b/t/unit/test_utils.py index 566e9050..8150d5f5 100644 --- a/t/unit/test_utils.py +++ b/t/unit/test_utils.py @@ -111,6 +111,7 @@ def test_make_aware_not_use_tz_naive_negative_dst(self, mock_getattr, mock_is_na mock_localtime_1.assert_called_with() mock_make_aware.assert_called_with(dt, "America/Los_Angeles", is_dst=None) mock_get_default_timezone.assert_called() + @mock.patch('django_celery_beat.utils.timezone.localtime') @mock.patch('django_celery_beat.utils.timezone.get_default_timezone') @mock.patch('django_celery_beat.utils.timezone.make_aware') From 6d51ddc9d518c34d1071bf053408a3fcc9400b08 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Nov 2022 23:39:20 +0000 Subject: [PATCH 05/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- django_celery_beat/utils.py | 3 ++- t/unit/test_utils.py | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/django_celery_beat/utils.py b/django_celery_beat/utils.py index 2c003305..5ce3a809 100644 --- a/django_celery_beat/utils.py +++ b/django_celery_beat/utils.py @@ -1,9 +1,10 @@ """Utilities.""" # -- XXX This module must not use translation as that causes # -- a recursive loader import! +import time + from django.conf import settings from django.utils import timezone -import time is_aware = timezone.is_aware # celery schedstate return None will make it not work diff --git a/t/unit/test_utils.py b/t/unit/test_utils.py index 8150d5f5..450500f5 100644 --- a/t/unit/test_utils.py +++ b/t/unit/test_utils.py @@ -1,10 +1,12 @@ +import time +from datetime import datetime from unittest import mock + from django.test import TestCase -from datetime import datetime -import time +from django.utils import timezone from django_celery_beat import utils -from django.utils import timezone + class UtilsTest(TestCase): @@ -22,7 +24,7 @@ def test_make_aware_use_tz_naive(self, mock_getattr, mock_is_naive, mock_localti mock_localtime_2.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 0]) mock_make_aware.return_value = dt - self.assertEquals(utils.make_aware(dt), mock_localtime_2.return_value) + self.assertEqual(utils.make_aware(dt), mock_localtime_2.return_value) mock_localtime_1.assert_not_called() mock_make_aware.assert_called_with(dt, timezone.utc) @@ -43,7 +45,7 @@ def test_make_aware_use_tz_not_naive(self, mock_getattr, mock_is_naive, mock_loc mock_localtime_2.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 0]) mock_make_aware.return_value = dt - self.assertEquals(utils.make_aware(dt), mock_localtime_2.return_value) + self.assertEqual(utils.make_aware(dt), mock_localtime_2.return_value) mock_localtime_1.assert_not_called() mock_make_aware.assert_not_called() @@ -64,7 +66,7 @@ def test_make_aware_not_use_tz_naive_dst(self, mock_getattr, mock_is_naive, mock mock_localtime_1.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 1]) mock_make_aware.return_value = dt - self.assertEquals(utils.make_aware(dt), dt) + self.assertEqual(utils.make_aware(dt), dt) mock_localtime_1.assert_called_with() mock_make_aware.assert_called_with(dt, "America/Los_Angeles", is_dst=True) @@ -85,7 +87,7 @@ def test_make_aware_not_use_tz_naive_not_dst(self, mock_getattr, mock_is_naive, mock_localtime_1.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 0]) mock_make_aware.return_value = dt - self.assertEquals(utils.make_aware(dt), dt) + self.assertEqual(utils.make_aware(dt), dt) mock_localtime_1.assert_called_with() mock_make_aware.assert_called_with(dt, "America/Los_Angeles", is_dst=False) @@ -106,7 +108,7 @@ def test_make_aware_not_use_tz_naive_negative_dst(self, mock_getattr, mock_is_na mock_localtime_1.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, -1]) mock_make_aware.return_value = dt - self.assertEquals(utils.make_aware(dt), dt) + self.assertEqual(utils.make_aware(dt), dt) mock_localtime_1.assert_called_with() mock_make_aware.assert_called_with(dt, "America/Los_Angeles", is_dst=None) @@ -126,7 +128,7 @@ def test_make_aware_not_use_tz_not_naive_dst(self, mock_getattr, mock_is_naive, mock_localtime_1.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 0]) mock_make_aware.return_value = dt - self.assertEquals(utils.make_aware(dt), dt) + self.assertEqual(utils.make_aware(dt), dt) mock_localtime_1.assert_not_called() mock_make_aware.assert_not_called() From 789d16bbadb0ced128cef21a969e01281cc4d7fe Mon Sep 17 00:00:00 2001 From: Andrew Yoo Date: Thu, 6 Jul 2023 10:14:16 -0700 Subject: [PATCH 06/10] format --- django_celery_beat/utils.py | 4 +++- t/unit/test_utils.py | 48 ++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/django_celery_beat/utils.py b/django_celery_beat/utils.py index 5ce3a809..012d18e9 100644 --- a/django_celery_beat/utils.py +++ b/django_celery_beat/utils.py @@ -34,7 +34,9 @@ def make_aware(value): else: is_dst = bool(tm_isdst) - value = timezone.make_aware(value, timezone.get_default_timezone(), is_dst=is_dst) + value = timezone.make_aware(value, + timezone.get_default_timezone(), + is_dst=is_dst) return value diff --git a/t/unit/test_utils.py b/t/unit/test_utils.py index 450500f5..92ed2398 100644 --- a/t/unit/test_utils.py +++ b/t/unit/test_utils.py @@ -16,7 +16,13 @@ class UtilsTest(TestCase): @mock.patch('django_celery_beat.utils.time.localtime') @mock.patch('django_celery_beat.utils.timezone.is_naive') @mock.patch('django_celery_beat.utils.getattr') - def test_make_aware_use_tz_naive(self, mock_getattr, mock_is_naive, mock_localtime_1, mock_make_aware, mock_get_default_timezone, mock_localtime_2): + def test_make_aware_use_tz_naive(self, + mock_getattr, + mock_is_naive, + mock_localtime_1, + mock_make_aware, + mock_get_default_timezone, + mock_localtime_2): dt = datetime(2022, 11, 6, 1, 15, 0) mock_getattr.return_value = True mock_is_naive.return_value = True @@ -37,7 +43,13 @@ def test_make_aware_use_tz_naive(self, mock_getattr, mock_is_naive, mock_localti @mock.patch('django_celery_beat.utils.time.localtime') @mock.patch('django_celery_beat.utils.timezone.is_naive') @mock.patch('django_celery_beat.utils.getattr') - def test_make_aware_use_tz_not_naive(self, mock_getattr, mock_is_naive, mock_localtime_1, mock_make_aware, mock_get_default_timezone, mock_localtime_2): + def test_make_aware_use_tz_not_naive(self, + mock_getattr, + mock_is_naive, + mock_localtime_1, + mock_make_aware, + mock_get_default_timezone, + mock_localtime_2): dt = datetime(2022, 11, 6, 1, 15, 0) mock_getattr.return_value = True mock_is_naive.return_value = False @@ -58,7 +70,13 @@ def test_make_aware_use_tz_not_naive(self, mock_getattr, mock_is_naive, mock_loc @mock.patch('django_celery_beat.utils.time.localtime') @mock.patch('django_celery_beat.utils.timezone.is_naive') @mock.patch('django_celery_beat.utils.getattr') - def test_make_aware_not_use_tz_naive_dst(self, mock_getattr, mock_is_naive, mock_localtime_1, mock_make_aware, mock_get_default_timezone, mock_localtime_2): + def test_make_aware_not_use_tz_naive_dst(self, + mock_getattr, + mock_is_naive, + mock_localtime_1, + mock_make_aware, + mock_get_default_timezone, + mock_localtime_2): dt = datetime(2022, 11, 6, 1, 15, 0) mock_getattr.return_value = False mock_is_naive.return_value = True @@ -79,7 +97,13 @@ def test_make_aware_not_use_tz_naive_dst(self, mock_getattr, mock_is_naive, mock @mock.patch('django_celery_beat.utils.time.localtime') @mock.patch('django_celery_beat.utils.timezone.is_naive') @mock.patch('django_celery_beat.utils.getattr') - def test_make_aware_not_use_tz_naive_not_dst(self, mock_getattr, mock_is_naive, mock_localtime_1, mock_make_aware, mock_get_default_timezone, mock_localtime_2): + def test_make_aware_not_use_tz_naive_not_dst(self, + mock_getattr, + mock_is_naive, + mock_localtime_1, + mock_make_aware, + mock_get_default_timezone, + mock_localtime_2): dt = datetime(2022, 11, 6, 1, 15, 0) mock_getattr.return_value = False mock_is_naive.return_value = True @@ -100,7 +124,13 @@ def test_make_aware_not_use_tz_naive_not_dst(self, mock_getattr, mock_is_naive, @mock.patch('django_celery_beat.utils.time.localtime') @mock.patch('django_celery_beat.utils.timezone.is_naive') @mock.patch('django_celery_beat.utils.getattr') - def test_make_aware_not_use_tz_naive_negative_dst(self, mock_getattr, mock_is_naive, mock_localtime_1, mock_make_aware, mock_get_default_timezone, mock_localtime_2): + def test_make_aware_not_use_tz_naive_negative_dst(self, + mock_getattr, + mock_is_naive, + mock_localtime_1, + mock_make_aware, + mock_get_default_timezone, + mock_localtime_2): dt = datetime(2022, 11, 6, 1, 15, 0) mock_getattr.return_value = False mock_is_naive.return_value = True @@ -120,7 +150,13 @@ def test_make_aware_not_use_tz_naive_negative_dst(self, mock_getattr, mock_is_na @mock.patch('django_celery_beat.utils.time.localtime') @mock.patch('django_celery_beat.utils.timezone.is_naive') @mock.patch('django_celery_beat.utils.getattr') - def test_make_aware_not_use_tz_not_naive_dst(self, mock_getattr, mock_is_naive, mock_localtime_1, mock_make_aware, mock_get_default_timezone, mock_localtime_2): + def test_make_aware_not_use_tz_not_naive_dst(self, + mock_getattr, + mock_is_naive, + mock_localtime_1, + mock_make_aware, + mock_get_default_timezone, + mock_localtime_2): dt = datetime(2022, 11, 6, 1, 15, 0) mock_getattr.return_value = False mock_is_naive.return_value = False From 6df332f7784dc9fe73f87981d9cab25cea177ac3 Mon Sep 17 00:00:00 2001 From: Andrew Yoo Date: Thu, 6 Jul 2023 10:22:43 -0700 Subject: [PATCH 07/10] more formatting changes --- t/unit/test_utils.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/t/unit/test_utils.py b/t/unit/test_utils.py index 92ed2398..d43bd0d8 100644 --- a/t/unit/test_utils.py +++ b/t/unit/test_utils.py @@ -54,7 +54,8 @@ def test_make_aware_use_tz_not_naive(self, mock_getattr.return_value = True mock_is_naive.return_value = False mock_get_default_timezone.return_value = "America/Los_Angeles" - mock_localtime_2.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 0]) + test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 0] + mock_localtime_2.return_value = time.struct_time(test_time) mock_make_aware.return_value = dt self.assertEqual(utils.make_aware(dt), mock_localtime_2.return_value) @@ -81,7 +82,8 @@ def test_make_aware_not_use_tz_naive_dst(self, mock_getattr.return_value = False mock_is_naive.return_value = True mock_get_default_timezone.return_value = "America/Los_Angeles" - mock_localtime_1.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 1]) + test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 1] + mock_localtime_1.return_value = time.struct_time(test_time) mock_make_aware.return_value = dt self.assertEqual(utils.make_aware(dt), dt) @@ -108,7 +110,8 @@ def test_make_aware_not_use_tz_naive_not_dst(self, mock_getattr.return_value = False mock_is_naive.return_value = True mock_get_default_timezone.return_value = "America/Los_Angeles" - mock_localtime_1.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 0]) + test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 0] + mock_localtime_1.return_value = time.struct_time(test_time) mock_make_aware.return_value = dt self.assertEqual(utils.make_aware(dt), dt) @@ -135,7 +138,8 @@ def test_make_aware_not_use_tz_naive_negative_dst(self, mock_getattr.return_value = False mock_is_naive.return_value = True mock_get_default_timezone.return_value = "America/Los_Angeles" - mock_localtime_1.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, -1]) + test_time = [2022, 11, 6, 1, 15, 0, 0, 310, -1] + mock_localtime_1.return_value = time.struct_time(test_time) mock_make_aware.return_value = dt self.assertEqual(utils.make_aware(dt), dt) @@ -161,7 +165,8 @@ def test_make_aware_not_use_tz_not_naive_dst(self, mock_getattr.return_value = False mock_is_naive.return_value = False mock_get_default_timezone.return_value = "America/Los_Angeles" - mock_localtime_1.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 0]) + test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 0] + mock_localtime_1.return_value = time.struct_time(test_time) mock_make_aware.return_value = dt self.assertEqual(utils.make_aware(dt), dt) From 0a1fe8bf721427cd4db44b4a382b0c7a1c3860bc Mon Sep 17 00:00:00 2001 From: Andrew Yoo Date: Thu, 6 Jul 2023 10:26:36 -0700 Subject: [PATCH 08/10] some more format optimizations --- t/unit/test_utils.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/t/unit/test_utils.py b/t/unit/test_utils.py index d43bd0d8..be7b2091 100644 --- a/t/unit/test_utils.py +++ b/t/unit/test_utils.py @@ -27,7 +27,8 @@ def test_make_aware_use_tz_naive(self, mock_getattr.return_value = True mock_is_naive.return_value = True mock_get_default_timezone.return_value = "America/Los_Angeles" - mock_localtime_2.return_value = time.struct_time([2022, 11, 6, 1, 15, 0, 0, 310, 0]) + test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 0] + mock_localtime_2.return_value = time.struct_time(test_time) mock_make_aware.return_value = dt self.assertEqual(utils.make_aware(dt), mock_localtime_2.return_value) @@ -63,7 +64,10 @@ def test_make_aware_use_tz_not_naive(self, mock_localtime_1.assert_not_called() mock_make_aware.assert_not_called() mock_get_default_timezone.assert_called() - mock_localtime_2.assert_called_with(dt, "America/Los_Angeles") + mock_localtime_2.assert_called_with( + dt, + "America/Los_Angeles" + ) @mock.patch('django_celery_beat.utils.timezone.localtime') @mock.patch('django_celery_beat.utils.timezone.get_default_timezone') @@ -89,7 +93,11 @@ def test_make_aware_not_use_tz_naive_dst(self, self.assertEqual(utils.make_aware(dt), dt) mock_localtime_1.assert_called_with() - mock_make_aware.assert_called_with(dt, "America/Los_Angeles", is_dst=True) + mock_make_aware.assert_called_with( + dt, + "America/Los_Angeles", + is_dst=True + ) mock_get_default_timezone.assert_called() mock_localtime_2.assert_not_called() @@ -117,7 +125,11 @@ def test_make_aware_not_use_tz_naive_not_dst(self, self.assertEqual(utils.make_aware(dt), dt) mock_localtime_1.assert_called_with() - mock_make_aware.assert_called_with(dt, "America/Los_Angeles", is_dst=False) + mock_make_aware.assert_called_with( + dt, + "America/Los_Angeles", + is_dst=False + ) mock_get_default_timezone.assert_called() mock_localtime_2.assert_not_called() @@ -145,7 +157,11 @@ def test_make_aware_not_use_tz_naive_negative_dst(self, self.assertEqual(utils.make_aware(dt), dt) mock_localtime_1.assert_called_with() - mock_make_aware.assert_called_with(dt, "America/Los_Angeles", is_dst=None) + mock_make_aware.assert_called_with( + dt, + "America/Los_Angeles", + is_dst=None + ) mock_get_default_timezone.assert_called() @mock.patch('django_celery_beat.utils.timezone.localtime') From c54d3c1cbb35914d42e80381a4a18ca6d564a1b4 Mon Sep 17 00:00:00 2001 From: Andrew Yoo Date: Thu, 6 Jul 2023 10:35:50 -0700 Subject: [PATCH 09/10] try this --- t/unit/test_utils.py | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/t/unit/test_utils.py b/t/unit/test_utils.py index be7b2091..b96466d6 100644 --- a/t/unit/test_utils.py +++ b/t/unit/test_utils.py @@ -36,7 +36,8 @@ def test_make_aware_use_tz_naive(self, mock_localtime_1.assert_not_called() mock_make_aware.assert_called_with(dt, timezone.utc) mock_get_default_timezone.assert_called() - mock_localtime_2.assert_called_with(dt, "America/Los_Angeles") + mock_localtime_2.assert_called_with(dt, + "America/Los_Angeles") @mock.patch('django_celery_beat.utils.timezone.localtime') @mock.patch('django_celery_beat.utils.timezone.get_default_timezone') @@ -64,10 +65,8 @@ def test_make_aware_use_tz_not_naive(self, mock_localtime_1.assert_not_called() mock_make_aware.assert_not_called() mock_get_default_timezone.assert_called() - mock_localtime_2.assert_called_with( - dt, - "America/Los_Angeles" - ) + mock_localtime_2.assert_called_with(dt, + "America/Los_Angeles") @mock.patch('django_celery_beat.utils.timezone.localtime') @mock.patch('django_celery_beat.utils.timezone.get_default_timezone') @@ -93,11 +92,9 @@ def test_make_aware_not_use_tz_naive_dst(self, self.assertEqual(utils.make_aware(dt), dt) mock_localtime_1.assert_called_with() - mock_make_aware.assert_called_with( - dt, - "America/Los_Angeles", - is_dst=True - ) + mock_make_aware.assert_called_with(dt, + "America/Los_Angeles", + is_dst=True) mock_get_default_timezone.assert_called() mock_localtime_2.assert_not_called() @@ -125,11 +122,9 @@ def test_make_aware_not_use_tz_naive_not_dst(self, self.assertEqual(utils.make_aware(dt), dt) mock_localtime_1.assert_called_with() - mock_make_aware.assert_called_with( - dt, - "America/Los_Angeles", - is_dst=False - ) + mock_make_aware.assert_called_with(dt, + "America/Los_Angeles", + is_dst=False) mock_get_default_timezone.assert_called() mock_localtime_2.assert_not_called() @@ -157,11 +152,9 @@ def test_make_aware_not_use_tz_naive_negative_dst(self, self.assertEqual(utils.make_aware(dt), dt) mock_localtime_1.assert_called_with() - mock_make_aware.assert_called_with( - dt, - "America/Los_Angeles", - is_dst=None - ) + mock_make_aware.assert_called_with(dt, + "America/Los_Angeles", + is_dst=None) mock_get_default_timezone.assert_called() @mock.patch('django_celery_beat.utils.timezone.localtime') From a4a39c0920a947e239e726d33c0b8bf1412ddf85 Mon Sep 17 00:00:00 2001 From: Andrew Yoo Date: Thu, 6 Jul 2023 10:43:16 -0700 Subject: [PATCH 10/10] shorter line --- t/unit/test_utils.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/t/unit/test_utils.py b/t/unit/test_utils.py index b96466d6..b711a961 100644 --- a/t/unit/test_utils.py +++ b/t/unit/test_utils.py @@ -134,13 +134,13 @@ def test_make_aware_not_use_tz_naive_not_dst(self, @mock.patch('django_celery_beat.utils.time.localtime') @mock.patch('django_celery_beat.utils.timezone.is_naive') @mock.patch('django_celery_beat.utils.getattr') - def test_make_aware_not_use_tz_naive_negative_dst(self, - mock_getattr, - mock_is_naive, - mock_localtime_1, - mock_make_aware, - mock_get_default_timezone, - mock_localtime_2): + def test_make_aware_not_use_tz_naive_neg_dst(self, + mock_getattr, + mock_is_naive, + mock_localtime_1, + mock_make_aware, + mock_get_default_timezone, + mock_localtime_2): dt = datetime(2022, 11, 6, 1, 15, 0) mock_getattr.return_value = False mock_is_naive.return_value = True