Skip to content

Commit

Permalink
Added tests for MemberCanShopService
Browse files Browse the repository at this point in the history
  • Loading branch information
Theophile-Madet committed Dec 11, 2024
1 parent 32e3206 commit 125155a
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 15 deletions.
24 changes: 18 additions & 6 deletions tapir/coop/services/member_can_shop_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from django.db.models import QuerySet, Case, When, Value

from tapir.coop.models import ShareOwner, MemberStatus
from tapir.shifts.services.frozen_status_history_service import (
FrozenStatusHistoryService,
)
from tapir.shifts.services.shift_can_shop_service import ShiftCanShopService


Expand All @@ -15,18 +18,27 @@ def can_shop(
share_owner: ShareOwner,
at_datetime: datetime.datetime | datetime.date | None = None,
):
return (
share_owner.user is not None
and share_owner.is_active(at_datetime)
and ShiftCanShopService.can_shop(share_owner, at_datetime)
)
if share_owner.user is None:
return False
if not share_owner.is_active(at_datetime):
return False

member_object = share_owner
if not hasattr(
member_object, FrozenStatusHistoryService.ANNOTATION_IS_FROZEN_AT_DATE
):
member_object = share_owner.user.shift_user_data

return ShiftCanShopService.can_shop(member_object, at_datetime)

@classmethod
def annotate_share_owner_queryset_with_shopping_status_at_datetime(
cls, share_owners: QuerySet[ShareOwner], reference_datetime: datetime.datetime
):
members_who_can_shop = share_owners.filter(user__isnull=False)
members_who_can_shop = members_who_can_shop.with_status(MemberStatus.ACTIVE)
members_who_can_shop = members_who_can_shop.with_status(
MemberStatus.ACTIVE, reference_datetime
)
members_who_can_shop = (
ShiftCanShopService.annotate_share_owner_queryset_with_can_shop_at_datetime(
members_who_can_shop, reference_datetime
Expand Down
147 changes: 147 additions & 0 deletions tapir/coop/tests/test_member_can_shop_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import datetime

from django.utils import timezone

from tapir.accounts.tests.factories.factories import TapirUserFactory
from tapir.coop.models import ShareOwner, ShareOwnership
from tapir.coop.services.member_can_shop_service import MemberCanShopService
from tapir.coop.tests.factories import ShareOwnerFactory
from tapir.shifts.models import ShiftUserData
from tapir.utils.tests_utils import TapirFactoryTestBase


class TestMemberCanShopService(TapirFactoryTestBase):
REFERENCE_TIME = timezone.make_aware(datetime.datetime(year=2024, month=6, day=1))

def test_canShop_memberCanShop_annotatedWithTrue(
self,
):
tapir_user = TapirUserFactory.create(
share_owner__nb_shares=1, share_owner__is_investing=False
)
ShareOwnership.objects.update(
start_date=self.REFERENCE_TIME.date() - datetime.timedelta(days=1)
)

self.assertTrue(
MemberCanShopService.can_shop(tapir_user.share_owner, self.REFERENCE_TIME)
)

def test_canShop_memberHasNoTapirUser_annotatedWithFalse(
self,
):
share_owner = ShareOwnerFactory.create(nb_shares=1)
ShareOwnership.objects.update(
start_date=self.REFERENCE_TIME.date() - datetime.timedelta(days=1)
)

self.assertFalse(
MemberCanShopService.can_shop(share_owner, self.REFERENCE_TIME)
)

def test_canShop_memberIsNotActive_annotatedWithFalse(
self,
):
tapir_user = TapirUserFactory.create(
share_owner__nb_shares=1, share_owner__is_investing=False
)
ShareOwnership.objects.update(
start_date=self.REFERENCE_TIME.date() + datetime.timedelta(days=1)
)

self.assertFalse(
MemberCanShopService.can_shop(tapir_user.share_owner, self.REFERENCE_TIME)
)

def test_canShop_memberIsFrozen_annotatedWithFalse(
self,
):
tapir_user = TapirUserFactory.create(
share_owner__nb_shares=1, share_owner__is_investing=False
)
ShareOwnership.objects.update(
start_date=self.REFERENCE_TIME.date() - datetime.timedelta(days=1)
)
ShiftUserData.objects.update(is_frozen=True)

self.assertFalse(
MemberCanShopService.can_shop(tapir_user.share_owner, self.REFERENCE_TIME)
)

def test_annotateShareOwnerQuerysetWithShoppingStatusAtDatetime_memberCanShop_annotatedWithTrue(
self,
):
TapirUserFactory.create(
share_owner__nb_shares=1, share_owner__is_investing=False
)
ShareOwnership.objects.update(
start_date=self.REFERENCE_TIME.date() - datetime.timedelta(days=1)
)

queryset = MemberCanShopService.annotate_share_owner_queryset_with_shopping_status_at_datetime(
ShareOwner.objects.all(), self.REFERENCE_TIME
)

self.assertEqual(1, queryset.count())
self.assertTrue(
getattr(queryset.first(), MemberCanShopService.ANNOTATION_CAN_SHOP)
)
self.assertEqual(
self.REFERENCE_TIME,
getattr(
queryset.first(), MemberCanShopService.ANNOTATION_CAN_SHOP_DATE_CHECK
),
)

def test_annotateShareOwnerQuerysetWithShoppingStatusAtDatetime_memberHasNoTapirUser_annotatedWithFalse(
self,
):
ShareOwnerFactory.create(nb_shares=1)
ShareOwnership.objects.update(
start_date=self.REFERENCE_TIME.date() - datetime.timedelta(days=1)
)

queryset = MemberCanShopService.annotate_share_owner_queryset_with_shopping_status_at_datetime(
ShareOwner.objects.all(), self.REFERENCE_TIME
)

self.assertFalse(
getattr(queryset.first(), MemberCanShopService.ANNOTATION_CAN_SHOP)
)

def test_annotateShareOwnerQuerysetWithShoppingStatusAtDatetime_memberIsNotActive_annotatedWithFalse(
self,
):
TapirUserFactory.create(
share_owner__nb_shares=1, share_owner__is_investing=False
)
ShareOwnership.objects.update(
start_date=self.REFERENCE_TIME.date() + datetime.timedelta(days=1)
)

queryset = MemberCanShopService.annotate_share_owner_queryset_with_shopping_status_at_datetime(
ShareOwner.objects.all(), self.REFERENCE_TIME
)

self.assertFalse(
getattr(queryset.first(), MemberCanShopService.ANNOTATION_CAN_SHOP)
)

def test_annotateShareOwnerQuerysetWithShoppingStatusAtDatetime_memberIsFrozen_annotatedWithFalse(
self,
):
TapirUserFactory.create(
share_owner__nb_shares=1, share_owner__is_investing=False
)
ShareOwnership.objects.update(
start_date=self.REFERENCE_TIME.date() - datetime.timedelta(days=1)
)
ShiftUserData.objects.update(is_frozen=True)

queryset = MemberCanShopService.annotate_share_owner_queryset_with_shopping_status_at_datetime(
ShareOwner.objects.all(), self.REFERENCE_TIME
)

self.assertFalse(
getattr(queryset.first(), MemberCanShopService.ANNOTATION_CAN_SHOP)
)
23 changes: 14 additions & 9 deletions tapir/shifts/services/shift_can_shop_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import datetime
import typing

from django.db.models import QuerySet, Case, When
from django.db.models import QuerySet, Case, When, Value

from tapir.shifts.models import ShiftUserData
from tapir.shifts.services.frozen_status_history_service import (
Expand All @@ -28,20 +28,25 @@ def can_shop(
member_object, at_datetime
)

@staticmethod
@classmethod
def annotate_share_owner_queryset_with_can_shop_at_datetime(
share_owners: QuerySet[ShareOwner], reference_datetime: datetime.datetime
cls, share_owners: QuerySet[ShareOwner], reference_datetime: datetime.datetime
):
share_owners = FrozenStatusHistoryService.annotate_share_owner_queryset_with_is_frozen_at_datetime(
share_owners, reference_datetime
)

return share_owners.annotate(
Case(
When(
**{FrozenStatusHistoryService.ANNOTATION_IS_FROZEN_AT_DATE: True},
then=True,
**{
cls.ANNOTATION_SHIFT_CAN_SHOP: Case(
When(
**{
FrozenStatusHistoryService.ANNOTATION_IS_FROZEN_AT_DATE: True
},
then=False,
),
default=True,
),
default=False,
)
cls.ANNOTATION_SHIFT_CAN_SHOP_DATE_CHECK: Value(reference_datetime),
}
)

0 comments on commit 125155a

Please sign in to comment.