From 0890390ebf82b6583c9be2556d894f92943eadaa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 29 Jul 2022 15:23:42 +0200
Subject: [PATCH 13/14] pricing: adapt get_agenda_pricing method for flat fee
 schedule mode (#67675)

---
 lingo/pricing/models.py      |  3 +-
 tests/pricing/test_models.py | 98 +++++++++++++++++++++++++++++++++---
 2 files changed, 92 insertions(+), 9 deletions(-)

diff --git a/lingo/pricing/models.py b/lingo/pricing/models.py
index 78c924f..26fc267 100644
--- a/lingo/pricing/models.py
+++ b/lingo/pricing/models.py
@@ -462,11 +462,12 @@ class AgendaPricing(models.Model):
         }
 
     @staticmethod
-    def get_agenda_pricing(agenda, start_date):
+    def get_agenda_pricing(agenda, start_date, flat_fee_schedule):
         try:
             return agenda.agendapricings.get(
                 date_start__lte=start_date,
                 date_end__gt=start_date,
+                flat_fee_schedule=flat_fee_schedule,
             )
         except (AgendaPricing.DoesNotExist, AgendaPricing.MultipleObjectsReturned):
             raise AgendaPricingNotFound
diff --git a/tests/pricing/test_models.py b/tests/pricing/test_models.py
index 2a0bc01..5c27d29 100644
--- a/tests/pricing/test_models.py
+++ b/tests/pricing/test_models.py
@@ -287,30 +287,80 @@ def test_get_agenda_pricing():
 
     # not found
     with pytest.raises(AgendaPricingNotFound):
-        AgendaPricing.get_agenda_pricing(agenda=agenda, start_date=start_date)
+        AgendaPricing.get_agenda_pricing(
+            agenda=agenda,
+            start_date=start_date,
+            flat_fee_schedule=False,
+        )
+    with pytest.raises(AgendaPricingNotFound):
+        AgendaPricing.get_agenda_pricing(
+            agenda=agenda,
+            start_date=start_date,
+            flat_fee_schedule=True,
+        )
 
     # ok
     agenda_pricing = AgendaPricing.objects.create(
         pricing=pricing,
         date_start=datetime.date(year=2021, month=9, day=1),
         date_end=datetime.date(year=2021, month=10, day=1),
+        flat_fee_schedule=False,
     )
     agenda_pricing.agendas.add(agenda)
-    assert AgendaPricing.get_agenda_pricing(agenda=agenda, start_date=start_date) == agenda_pricing
+    agenda_pricing2 = AgendaPricing.objects.create(
+        pricing=pricing,
+        date_start=datetime.date(year=2021, month=9, day=1),
+        date_end=datetime.date(year=2021, month=10, day=1),
+        flat_fee_schedule=True,
+    )
+    agenda_pricing2.agendas.add(agenda)
+    assert (
+        AgendaPricing.get_agenda_pricing(
+            agenda=agenda,
+            start_date=start_date,
+            flat_fee_schedule=False,
+        )
+        == agenda_pricing
+    )
+    assert (
+        AgendaPricing.get_agenda_pricing(
+            agenda=agenda,
+            start_date=start_date,
+            flat_fee_schedule=True,
+        )
+        == agenda_pricing2
+    )
 
     # more than one matching
     agenda_pricing = AgendaPricing.objects.create(
         pricing=pricing,
         date_start=datetime.date(year=2021, month=9, day=14),
         date_end=datetime.date(year=2021, month=9, day=16),
+        flat_fee_schedule=False,
     )
     agenda_pricing.agendas.add(agenda)
+    agenda_pricing2 = AgendaPricing.objects.create(
+        pricing=pricing,
+        date_start=datetime.date(year=2021, month=9, day=14),
+        date_end=datetime.date(year=2021, month=9, day=16),
+        flat_fee_schedule=True,
+    )
+    agenda_pricing2.agendas.add(agenda)
     with pytest.raises(AgendaPricingNotFound):
-        AgendaPricing.get_agenda_pricing(agenda=agenda, start_date=start_date)
+        AgendaPricing.get_agenda_pricing(
+            agenda=agenda,
+            start_date=start_date,
+            flat_fee_schedule=False,
+        )
+        AgendaPricing.get_agenda_pricing(
+            agenda=agenda,
+            start_date=start_date,
+            flat_fee_schedule=True,
+        )
 
 
 @pytest.mark.parametrize(
-    'event_date, found',
+    'start_date, found',
     [
         # just before first day
         ((2021, 8, 31), False),
@@ -322,21 +372,53 @@ def test_get_agenda_pricing():
         ((2021, 10, 1), False),
     ],
 )
-def test_get_agenda_pricing_event_date(event_date, found):
+def test_get_agenda_pricing_start_date(start_date, found):
     agenda = Agenda.objects.create(label='Foo bar')
     pricing = Pricing.objects.create(label='Foo bar')
     agenda_pricing = AgendaPricing.objects.create(
         pricing=pricing,
         date_start=datetime.date(year=2021, month=9, day=1),
         date_end=datetime.date(year=2021, month=10, day=1),
+        flat_fee_schedule=False,
     )
     agenda_pricing.agendas.add(agenda)
-    start_date = datetime.date(*event_date)
+    agenda_pricing2 = AgendaPricing.objects.create(
+        pricing=pricing,
+        date_start=datetime.date(year=2021, month=9, day=1),
+        date_end=datetime.date(year=2021, month=10, day=1),
+        flat_fee_schedule=True,
+    )
+    agenda_pricing2.agendas.add(agenda)
+    start_date = datetime.date(*start_date)
     if found:
-        assert AgendaPricing.get_agenda_pricing(agenda=agenda, start_date=start_date) == agenda_pricing
+        assert (
+            AgendaPricing.get_agenda_pricing(
+                agenda=agenda,
+                start_date=start_date,
+                flat_fee_schedule=False,
+            )
+            == agenda_pricing
+        )
+        assert (
+            AgendaPricing.get_agenda_pricing(
+                agenda=agenda,
+                start_date=start_date,
+                flat_fee_schedule=True,
+            )
+            == agenda_pricing2
+        )
     else:
         with pytest.raises(AgendaPricingNotFound):
-            AgendaPricing.get_agenda_pricing(agenda=agenda, start_date=start_date)
+            AgendaPricing.get_agenda_pricing(
+                agenda=agenda,
+                start_date=start_date,
+                flat_fee_schedule=False,
+            )
+            AgendaPricing.get_agenda_pricing(
+                agenda=agenda,
+                start_date=start_date,
+                flat_fee_schedule=True,
+            )
 
 
 @mock.patch('requests.Session.send', side_effect=mocked_requests_send)
-- 
2.35.1

