From 7e00a69d82d6f920e9f61181be019babb9d9f6b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Madet?= Date: Wed, 11 Dec 2024 14:51:40 +0100 Subject: [PATCH] Added tests for CoPurchaserHistoryService --- .../services/co_purchaser_history_service.py | 20 --- .../test_co_purchaser_history_service.py | 169 ++++++++++++++++++ 2 files changed, 169 insertions(+), 20 deletions(-) create mode 100644 tapir/accounts/tests/test_co_purchaser_history_service.py diff --git a/tapir/accounts/services/co_purchaser_history_service.py b/tapir/accounts/services/co_purchaser_history_service.py index a8f50873..a90442d4 100644 --- a/tapir/accounts/services/co_purchaser_history_service.py +++ b/tapir/accounts/services/co_purchaser_history_service.py @@ -22,26 +22,6 @@ class CoPurchaserHistoryService: ANNOTATION_HAS_CO_PURCHASER = "has_co_purchaser" ANNOTATION_HAS_CO_PURCHASER_DATE_CHECK = "has_co_purchaser_date_check" - @classmethod - def has_co_purchaser( - cls, tapir_user: TapirUser, at_datetime: datetime.datetime = None - ): - if at_datetime is None: - at_datetime = timezone.now() - - if not hasattr(tapir_user, cls.ANNOTATION_HAS_CO_PURCHASER): - tapir_user = cls.annotate_tapir_user_queryset_with_has_co_purchaser_at_date( - TapirUser.objects.filter(id=tapir_user.id), at_datetime - ).first() - - annotated_date = getattr(tapir_user, cls.ANNOTATION_HAS_CO_PURCHASER_DATE_CHECK) - if annotated_date != at_datetime: - raise ValueError( - f"Trying to get 'has co purchaser' at date {at_datetime}, but the queryset has been " - f"annotated relative to {annotated_date}" - ) - return getattr(tapir_user, cls.ANNOTATION_HAS_CO_PURCHASER) - @classmethod def annotate_tapir_user_queryset_with_has_co_purchaser_at_date( cls, diff --git a/tapir/accounts/tests/test_co_purchaser_history_service.py b/tapir/accounts/tests/test_co_purchaser_history_service.py new file mode 100644 index 00000000..0ce21bd1 --- /dev/null +++ b/tapir/accounts/tests/test_co_purchaser_history_service.py @@ -0,0 +1,169 @@ +import datetime + +from django.utils import timezone + +from tapir.accounts.models import TapirUser, UpdateTapirUserLogEntry +from tapir.accounts.services.co_purchaser_history_service import ( + CoPurchaserHistoryService, +) +from tapir.accounts.tests.factories.factories import TapirUserFactory +from tapir.utils.tests_utils import TapirFactoryTestBase, mock_timezone_now + + +class TestCoPurchaserHistoryService(TapirFactoryTestBase): + NOW = datetime.datetime(year=2022, month=7, day=13, hour=12) + REFERENCE_TIME = timezone.make_aware( + datetime.datetime(year=2022, month=5, day=21, hour=15) + ) + + def setUp(self) -> None: + super().setUp() + self.NOW = mock_timezone_now(self, self.NOW) + + @staticmethod + def create_irrelevant_log_entry(tapir_user, reference_time): + log_entry = UpdateTapirUserLogEntry.objects.create( + user=tapir_user, + old_values={"city": "Berlin"}, + new_values={"city": "Munich"}, + ) + log_entry.created_date = reference_time - datetime.timedelta(hours=5) + log_entry.save() + + def test_annotateTapirUserQuerysetWithHasCoPurchaserAtDate_noRelevantLogEntriesAndMemberHasCoPurchaser_annotatesTrue( + self, + ): + tapir_user = TapirUserFactory.create(co_purchaser="A test co-purchaser") + self.create_irrelevant_log_entry(tapir_user, self.REFERENCE_TIME) + + queryset = CoPurchaserHistoryService.annotate_tapir_user_queryset_with_has_co_purchaser_at_date( + TapirUser.objects.all(), self.REFERENCE_TIME + ) + + self.assertEqual( + "A test co-purchaser", getattr(queryset.get(), "co_purchaser_at_date") + ) + self.assertTrue( + getattr( + queryset.get(), CoPurchaserHistoryService.ANNOTATION_HAS_CO_PURCHASER + ) + ) + self.assertTrue( + self.REFERENCE_TIME, + getattr( + queryset.get(), + CoPurchaserHistoryService.ANNOTATION_HAS_CO_PURCHASER_DATE_CHECK, + ), + ) + + def test_annotateTapirUserQuerysetWithHasCoPurchaserAtDate_noRelevantLogEntriesAndMemberHasNoCoPurchaser_annotatesFalse( + self, + ): + tapir_user = TapirUserFactory.create(co_purchaser="") + self.create_irrelevant_log_entry(tapir_user, self.REFERENCE_TIME) + + queryset = CoPurchaserHistoryService.annotate_tapir_user_queryset_with_has_co_purchaser_at_date( + TapirUser.objects.all(), self.REFERENCE_TIME + ) + + self.assertEqual("", getattr(queryset.get(), "co_purchaser_at_date")) + self.assertFalse( + getattr( + queryset.get(), CoPurchaserHistoryService.ANNOTATION_HAS_CO_PURCHASER + ) + ) + self.assertTrue( + self.REFERENCE_TIME, + getattr( + queryset.get(), + CoPurchaserHistoryService.ANNOTATION_HAS_CO_PURCHASER_DATE_CHECK, + ), + ) + + def test_annotateTapirUserQuerysetWithHasCoPurchaserAtDate_hasRelevantLogEntriesWithCoPurchaser_annotatesTrue( + self, + ): + tapir_user = TapirUserFactory.create(co_purchaser="") + self.create_irrelevant_log_entry(tapir_user, self.REFERENCE_TIME) + + log_entry_in_the_past = UpdateTapirUserLogEntry.objects.create( + user=tapir_user, + old_values={"co_purchaser": ""}, + new_values={"co_purchaser": "Someone"}, + ) + log_entry_in_the_past.created_date = self.REFERENCE_TIME - datetime.timedelta( + hours=5 + ) + log_entry_in_the_past.save() + + log_entry_in_the_future = UpdateTapirUserLogEntry.objects.create( + user=tapir_user, + old_values={"co_purchaser": "Someone"}, + new_values={"co_purchaser": ""}, + ) + log_entry_in_the_future.created_date = self.REFERENCE_TIME + datetime.timedelta( + hours=5 + ) + log_entry_in_the_future.save() + + queryset = CoPurchaserHistoryService.annotate_tapir_user_queryset_with_has_co_purchaser_at_date( + TapirUser.objects.all(), self.REFERENCE_TIME + ) + + self.assertEqual("Someone", getattr(queryset.get(), "co_purchaser_at_date")) + self.assertTrue( + getattr( + queryset.get(), CoPurchaserHistoryService.ANNOTATION_HAS_CO_PURCHASER + ) + ) + self.assertTrue( + self.REFERENCE_TIME, + getattr( + queryset.get(), + CoPurchaserHistoryService.ANNOTATION_HAS_CO_PURCHASER_DATE_CHECK, + ), + ) + + def test_annotateTapirUserQuerysetWithHasCoPurchaserAtDate_hasRelevantLogEntriesWithNoCoPurchaser_annotatesFalse( + self, + ): + tapir_user = TapirUserFactory.create(co_purchaser="Someone") + self.create_irrelevant_log_entry(tapir_user, self.REFERENCE_TIME) + + log_entry_in_the_past = UpdateTapirUserLogEntry.objects.create( + user=tapir_user, + old_values={"co_purchaser": "Someone"}, + new_values={"co_purchaser": ""}, + ) + log_entry_in_the_past.created_date = self.REFERENCE_TIME - datetime.timedelta( + hours=5 + ) + log_entry_in_the_past.save() + + log_entry_in_the_future = UpdateTapirUserLogEntry.objects.create( + user=tapir_user, + old_values={"co_purchaser": ""}, + new_values={"co_purchaser": "Someone"}, + ) + log_entry_in_the_future.created_date = self.REFERENCE_TIME + datetime.timedelta( + hours=5 + ) + log_entry_in_the_future.save() + + queryset = CoPurchaserHistoryService.annotate_tapir_user_queryset_with_has_co_purchaser_at_date( + TapirUser.objects.all(), self.REFERENCE_TIME + ) + + self.assertEqual("", getattr(queryset.get(), "co_purchaser_at_date")) + self.assertFalse( + getattr( + queryset.get(), CoPurchaserHistoryService.ANNOTATION_HAS_CO_PURCHASER + ) + ) + self.assertTrue( + self.REFERENCE_TIME, + getattr( + queryset.get(), + CoPurchaserHistoryService.ANNOTATION_HAS_CO_PURCHASER_DATE_CHECK, + ), + )