From ee3c028ad590788878dbe2a7719a4c2375fdf594 Mon Sep 17 00:00:00 2001 From: tvedeane Date: Sun, 7 Jan 2024 21:30:55 +0100 Subject: [PATCH 1/4] Fix tests depending on year 2024 --- tapir/coop/tests/test_MemberStatus.py | 10 ++-------- tapir/welcomedesk/tests/test_welcome_desk_messages.py | 6 ++---- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/tapir/coop/tests/test_MemberStatus.py b/tapir/coop/tests/test_MemberStatus.py index 18d8045c4..9babc386f 100644 --- a/tapir/coop/tests/test_MemberStatus.py +++ b/tapir/coop/tests/test_MemberStatus.py @@ -11,7 +11,6 @@ ) from tapir.coop.tests.factories import ShareOwnerFactory, MembershipPauseFactory from tapir.utils.tests_utils import TapirFactoryTestBase -from tapir.utils.tests_utils import mock_timezone_now class ShareOwnerStatusBaseTestClass(ABC): @@ -36,7 +35,6 @@ def test_hasPastShares_returnsSold(self): share.start_date = datetime.date(year=2020, month=1, day=1) share.end_date = datetime.date(year=2022, month=1, day=1) share.save() - mock_timezone_now(self, datetime.datetime(year=2023, month=1, day=1)) self.assertMemberStatus(share_owner, MemberStatus.SOLD) def test_hasFutureShares_returnsSold(self): @@ -44,9 +42,8 @@ def test_hasFutureShares_returnsSold(self): nb_shares=1, is_investing=False ) share: ShareOwnership = share_owner.share_ownerships.first() - share.start_date = datetime.date(year=2024, month=1, day=1) + share.start_date = datetime.date(year=3024, month=1, day=1) share.save() - mock_timezone_now(self, datetime.datetime(year=2023, month=1, day=1)) self.assertMemberStatus(share_owner, MemberStatus.SOLD) def test_hasNoSharesAndIsInvesting_returnsSold(self): @@ -70,7 +67,6 @@ def test_isInvestingAndPaused_returnsInvesting(self): start_date=datetime.date(year=2022, month=1, day=1), end_date=datetime.date(year=2024, month=1, day=1), ) - mock_timezone_now(self, datetime.datetime(year=2023, month=1, day=1)) self.assertMemberStatus(share_owner, MemberStatus.INVESTING) def test_isPaused_returnsPaused(self): @@ -80,9 +76,8 @@ def test_isPaused_returnsPaused(self): MembershipPauseFactory.create( share_owner=share_owner, start_date=datetime.date(year=2022, month=1, day=1), - end_date=datetime.date(year=2024, month=1, day=1), + end_date=datetime.date(year=2124, month=1, day=1), ) - mock_timezone_now(self, datetime.datetime(year=2023, month=1, day=1)) self.assertMemberStatus(share_owner, MemberStatus.PAUSED) def test_hasInvactivePause_returnsActive(self): @@ -94,7 +89,6 @@ def test_hasInvactivePause_returnsActive(self): start_date=datetime.date(year=2020, month=1, day=1), end_date=datetime.date(year=2022, month=1, day=1), ) - mock_timezone_now(self, datetime.datetime(year=2023, month=1, day=1)) self.assertMemberStatus(share_owner, MemberStatus.ACTIVE) def test_default_returnsActive(self): diff --git a/tapir/welcomedesk/tests/test_welcome_desk_messages.py b/tapir/welcomedesk/tests/test_welcome_desk_messages.py index fda1e1c8c..63cd01a55 100644 --- a/tapir/welcomedesk/tests/test_welcome_desk_messages.py +++ b/tapir/welcomedesk/tests/test_welcome_desk_messages.py @@ -8,11 +8,10 @@ from tapir.coop.tests.factories import ShareOwnerFactory, MembershipPauseFactory from tapir.shifts.models import ( ShiftAttendanceMode, - ShiftAccountEntry, ShiftAttendanceTemplate, ShiftExemption, ) -from tapir.utils.tests_utils import TapirFactoryTestBase, mock_timezone_now +from tapir.utils.tests_utils import TapirFactoryTestBase from tapir.utils.user_utils import UserUtils @@ -58,9 +57,8 @@ def test_is_paused(self): MembershipPauseFactory.create( share_owner=user.share_owner, start_date=datetime.date(year=2022, month=1, day=1), - end_date=datetime.date(year=2024, month=1, day=1), + end_date=datetime.date(year=3024, month=1, day=1), ) - mock_timezone_now(self, datetime.datetime(year=2024, month=1, day=1)) self.check_alerts(user.share_owner, [self.Messages.IS_PAUSED]) def test_no_abcd_shift(self): From 0c565cb98d297bced708258fcb7c252f9872f213 Mon Sep 17 00:00:00 2001 From: tvedeane Date: Sun, 7 Jan 2024 22:47:17 +0100 Subject: [PATCH 2/4] Fix wrong id in the test --- tapir/shifts/tests/test_slot_template_edit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tapir/shifts/tests/test_slot_template_edit.py b/tapir/shifts/tests/test_slot_template_edit.py index 34d267887..0da372739 100644 --- a/tapir/shifts/tests/test_slot_template_edit.py +++ b/tapir/shifts/tests/test_slot_template_edit.py @@ -87,7 +87,7 @@ def test_editing_a_slot_template_does_not_affect_past_slots(self): self.assertEqual("Name before", slot.name) response = self.client.post( - reverse(self.SLOT_TEMPLATE_EDIT_VIEW, args=[shift_template.id]), + reverse(self.SLOT_TEMPLATE_EDIT_VIEW, args=[slot_template.id]), { "name": "Name after", "check_update_future_shifts": True, From 1d823e6a182816799bdad485b6eb446150e3284b Mon Sep 17 00:00:00 2001 From: tvedeane Date: Sun, 28 Jan 2024 13:55:39 +0100 Subject: [PATCH 3/4] Use timezone.now() --- tapir/coop/models.py | 2 +- tapir/utils/models.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tapir/coop/models.py b/tapir/coop/models.py index d74d54ffe..2a1cec607 100644 --- a/tapir/coop/models.py +++ b/tapir/coop/models.py @@ -108,7 +108,7 @@ def with_name(self, search_string: str): def with_status(self, status: str, date=None): if date is None: - date = datetime.date.today() + date = timezone.now() active_ownerships = ShareOwnership.objects.active_temporal(date) if status == MemberStatus.SOLD: diff --git a/tapir/utils/models.py b/tapir/utils/models.py index 79607c375..238567639 100644 --- a/tapir/utils/models.py +++ b/tapir/utils/models.py @@ -1,7 +1,6 @@ from __future__ import annotations from collections import namedtuple -from datetime import date from django.core.exceptions import ValidationError from django.db import models @@ -299,7 +298,7 @@ def overlapping_with(self, obj) -> DurationModelMixinQuerySet: def active_temporal(self, effective_date=None) -> DurationModelMixinQuerySet: if not effective_date: # if no effective date was given, use today as the default - effective_date = date.today() + effective_date = timezone.now() return self.overlapping_with( # It's impossible to instantiate an abstract model, therefore create a namedtuple here namedtuple("FakeDurationModelMixin", ["start_date", "end_date"])( From 07665133f1574eaeece1a3fc90b13a7af4203d70 Mon Sep 17 00:00:00 2001 From: tvedeane Date: Sun, 28 Jan 2024 13:59:15 +0100 Subject: [PATCH 4/4] Revert back unnecessary changes --- tapir/coop/tests/test_MemberStatus.py | 10 ++++++++-- tapir/welcomedesk/tests/test_welcome_desk_messages.py | 5 +++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/tapir/coop/tests/test_MemberStatus.py b/tapir/coop/tests/test_MemberStatus.py index 9babc386f..18d8045c4 100644 --- a/tapir/coop/tests/test_MemberStatus.py +++ b/tapir/coop/tests/test_MemberStatus.py @@ -11,6 +11,7 @@ ) from tapir.coop.tests.factories import ShareOwnerFactory, MembershipPauseFactory from tapir.utils.tests_utils import TapirFactoryTestBase +from tapir.utils.tests_utils import mock_timezone_now class ShareOwnerStatusBaseTestClass(ABC): @@ -35,6 +36,7 @@ def test_hasPastShares_returnsSold(self): share.start_date = datetime.date(year=2020, month=1, day=1) share.end_date = datetime.date(year=2022, month=1, day=1) share.save() + mock_timezone_now(self, datetime.datetime(year=2023, month=1, day=1)) self.assertMemberStatus(share_owner, MemberStatus.SOLD) def test_hasFutureShares_returnsSold(self): @@ -42,8 +44,9 @@ def test_hasFutureShares_returnsSold(self): nb_shares=1, is_investing=False ) share: ShareOwnership = share_owner.share_ownerships.first() - share.start_date = datetime.date(year=3024, month=1, day=1) + share.start_date = datetime.date(year=2024, month=1, day=1) share.save() + mock_timezone_now(self, datetime.datetime(year=2023, month=1, day=1)) self.assertMemberStatus(share_owner, MemberStatus.SOLD) def test_hasNoSharesAndIsInvesting_returnsSold(self): @@ -67,6 +70,7 @@ def test_isInvestingAndPaused_returnsInvesting(self): start_date=datetime.date(year=2022, month=1, day=1), end_date=datetime.date(year=2024, month=1, day=1), ) + mock_timezone_now(self, datetime.datetime(year=2023, month=1, day=1)) self.assertMemberStatus(share_owner, MemberStatus.INVESTING) def test_isPaused_returnsPaused(self): @@ -76,8 +80,9 @@ def test_isPaused_returnsPaused(self): MembershipPauseFactory.create( share_owner=share_owner, start_date=datetime.date(year=2022, month=1, day=1), - end_date=datetime.date(year=2124, month=1, day=1), + end_date=datetime.date(year=2024, month=1, day=1), ) + mock_timezone_now(self, datetime.datetime(year=2023, month=1, day=1)) self.assertMemberStatus(share_owner, MemberStatus.PAUSED) def test_hasInvactivePause_returnsActive(self): @@ -89,6 +94,7 @@ def test_hasInvactivePause_returnsActive(self): start_date=datetime.date(year=2020, month=1, day=1), end_date=datetime.date(year=2022, month=1, day=1), ) + mock_timezone_now(self, datetime.datetime(year=2023, month=1, day=1)) self.assertMemberStatus(share_owner, MemberStatus.ACTIVE) def test_default_returnsActive(self): diff --git a/tapir/welcomedesk/tests/test_welcome_desk_messages.py b/tapir/welcomedesk/tests/test_welcome_desk_messages.py index 63cd01a55..92c8b6e27 100644 --- a/tapir/welcomedesk/tests/test_welcome_desk_messages.py +++ b/tapir/welcomedesk/tests/test_welcome_desk_messages.py @@ -11,7 +11,7 @@ ShiftAttendanceTemplate, ShiftExemption, ) -from tapir.utils.tests_utils import TapirFactoryTestBase +from tapir.utils.tests_utils import TapirFactoryTestBase, mock_timezone_now from tapir.utils.user_utils import UserUtils @@ -57,8 +57,9 @@ def test_is_paused(self): MembershipPauseFactory.create( share_owner=user.share_owner, start_date=datetime.date(year=2022, month=1, day=1), - end_date=datetime.date(year=3024, month=1, day=1), + end_date=datetime.date(year=2024, month=1, day=1), ) + mock_timezone_now(self, datetime.datetime(year=2024, month=1, day=1)) self.check_alerts(user.share_owner, [self.Messages.IS_PAUSED]) def test_no_abcd_shift(self):