Skip to content

Commit

Permalink
Added tests for NumberOfCoPurchasersAtDateView
Browse files Browse the repository at this point in the history
  • Loading branch information
Theophile-Madet committed Dec 11, 2024
1 parent 3b227a4 commit b9a69dd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 8 deletions.
31 changes: 24 additions & 7 deletions tapir/accounts/services/co_purchaser_history_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

import datetime

from django.db.models import Value, OuterRef, Case, When, QuerySet, Q
from django.db.models import (
Value,
OuterRef,
Case,
When,
QuerySet,
Q,
CharField,
Subquery,
)
from django.db.models.functions import Coalesce
from django.utils import timezone

from tapir.accounts.models import TapirUser, UpdateTapirUserLogEntry
Expand Down Expand Up @@ -42,13 +52,20 @@ def annotate_tapir_user_queryset_with_has_co_purchaser_at_date(
at_datetime = timezone.now()

queryset = queryset.annotate(
co_purchaser_at_date=UpdateTapirUserLogEntry.objects.filter(
user_id=OuterRef("id"),
created_date__lte=at_datetime,
new_values__co_purchaser__isnull=False,
co_purchaser_from_log_entry=Subquery(
UpdateTapirUserLogEntry.objects.filter(
user_id=OuterRef("id"),
created_date__lte=at_datetime,
new_values__co_purchaser__isnull=False,
)
.order_by("-created_date")
.values("new_values__co_purchaser")[:1],
output_field=CharField(),
)
.order_by("-created_date")
.values("new_values__co_purchaser")[:1]
)

queryset = queryset.annotate(
co_purchaser_at_date=Coalesce("co_purchaser_from_log_entry", "co_purchaser")
)

return queryset.annotate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_calculateDatapoint_memberIsWorkingButIsNotAbcd_notCounted(self):

self.assertEqual(0, result)

def test_calculateDatapoint_memberIsWorkingAnAbcd_counted(self):
def test_calculateDatapoint_memberIsWorkingAndAbcd_counted(self):
tapir_user = TapirUserFactory.create(
date_joined=self.REFERENCE_TIME - datetime.timedelta(days=1)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import datetime

from django.utils import timezone

from tapir.accounts.tests.factories.factories import TapirUserFactory
from tapir.statistics.views.fancy_graph.number_of_co_purchasers_view import (
NumberOfCoPurchasersAtDateView,
)
from tapir.utils.tests_utils import (
TapirFactoryTestBase,
mock_timezone_now,
)


class TestNumberOfCoPurchasersView(TapirFactoryTestBase):
NOW = datetime.datetime(year=2023, month=4, day=1, hour=12)
REFERENCE_TIME = timezone.make_aware(
datetime.datetime(year=2022, month=6, day=15, hour=12)
)

def setUp(self) -> None:
super().setUp()
self.NOW = mock_timezone_now(self, self.NOW)

def test_calculateDatapoint_memberHasCoPurchaserButIsNotWorking_notCounted(self):
TapirUserFactory.create(
date_joined=self.REFERENCE_TIME + datetime.timedelta(days=1),
co_purchaser="A test co-purchaser",
)

result = NumberOfCoPurchasersAtDateView().calculate_datapoint(
self.REFERENCE_TIME
)

self.assertEqual(0, result)

def test_calculateDatapoint_memberIsWorkingButDoesntHaveACoPurchaser_notCounted(
self,
):
TapirUserFactory.create(
date_joined=self.REFERENCE_TIME - datetime.timedelta(days=1),
share_owner__is_investing=False,
co_purchaser="",
)

result = NumberOfCoPurchasersAtDateView().calculate_datapoint(
self.REFERENCE_TIME
)

self.assertEqual(0, result)

def test_calculateDatapoint_memberIsWorkingAndHasCoPurchaser_counted(self):
TapirUserFactory.create(
date_joined=self.REFERENCE_TIME - datetime.timedelta(days=1),
co_purchaser="A test co-purchaser",
share_owner__is_investing=False,
)

result = NumberOfCoPurchasersAtDateView().calculate_datapoint(
self.REFERENCE_TIME
)

self.assertEqual(1, result)

0 comments on commit b9a69dd

Please sign in to comment.