From 500c139af9feb4f92cb579cb725ef4688a10087f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 2 Jun 2020 09:44:53 +0200
Subject: [PATCH 1/2] agendas: add tests on queryset for agenda settings
 (#43516)

---
 tests/test_agendas.py | 64 +++++++++++++++++++++++++++++++++++++++++++
 tests/test_manager.py | 36 ++++++++++++++++++++++++
 2 files changed, 100 insertions(+)

diff --git a/tests/test_agendas.py b/tests/test_agendas.py
index d3b0fd9..3779f4f 100644
--- a/tests/test_agendas.py
+++ b/tests/test_agendas.py
@@ -708,6 +708,70 @@ def test_agenda_get_effective_time_periods(db):
     assert common_timeperiod.end_time == datetime.time(18, 0)
 
 
+def test_desk_exceptions_within_two_weeks():
+    agenda = Agenda.objects.create(label='Agenda', kind='meetings')
+    desk = Desk.objects.create(agenda=agenda, label='Desk')
+
+    # no exception
+    assert list(desk.get_exceptions_within_two_weeks()) == []
+
+    # exception ends in the past
+    exception = TimePeriodException.objects.create(
+        desk=desk,
+        start_datetime=now() - datetime.timedelta(days=2),
+        end_datetime=now() - datetime.timedelta(days=1),
+    )
+    assert list(desk.get_exceptions_within_two_weeks()) == []
+
+    # exception ends in the future - 14 days
+    exception.end_datetime = now() + datetime.timedelta(days=10)
+    # but starts in the past
+    exception.start_datetime = now() - datetime.timedelta(days=2)
+    exception.save()
+    assert list(desk.get_exceptions_within_two_weeks()) == [exception]
+
+    # exception ends in the future - 14 days
+    exception.end_datetime = now() + datetime.timedelta(days=10)
+    # but starts in the future
+    exception.start_datetime = now() + datetime.timedelta(days=2)
+    exception.save()
+    assert list(desk.get_exceptions_within_two_weeks()) == [exception]
+
+    # exception ends in the future + 14 days
+    exception.end_datetime = now() + datetime.timedelta(days=20)
+    # but starts in the past
+    exception.start_datetime = now() - datetime.timedelta(days=2)
+    exception.save()
+    assert list(desk.get_exceptions_within_two_weeks()) == [exception]
+
+    # create another one, very far way from now
+    exception2 = TimePeriodException.objects.create(
+        desk=desk,
+        start_datetime=now() + datetime.timedelta(days=200),
+        end_datetime=now() + datetime.timedelta(days=201),
+    )
+
+    # exception ends in the future + 14 days
+    exception.end_datetime = now() + datetime.timedelta(days=20)
+    # but starts in the future - 14 days
+    exception.start_datetime = now() + datetime.timedelta(days=2)
+    exception.save()
+    assert list(desk.get_exceptions_within_two_weeks()) == [exception]
+
+    # exception ends in the future + 14 days
+    exception.end_datetime = now() + datetime.timedelta(days=20)
+    # but starts in the future + 14 days
+    exception.start_datetime = now() + datetime.timedelta(days=21)
+    exception.save()
+    assert list(desk.get_exceptions_within_two_weeks()) == [exception]
+
+    # exception in the past
+    exception.end_datetime = now() - datetime.timedelta(days=20)
+    exception.start_datetime = now() - datetime.timedelta(days=21)
+    exception.save()
+    assert list(desk.get_exceptions_within_two_weeks()) == [exception2]
+
+
 def test_desk_duplicate():
     agenda = Agenda.objects.create(label='Agenda')
     desk = Desk.objects.create(label='Desk', agenda=agenda)
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 1fb5faf..b2c5af7 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -283,6 +283,42 @@ def test_options_agenda_as_manager(app, manager_user):
     assert '<h2>Settings' in resp.text
 
 
+def test_options_meetings_agenda_num_queries(app, admin_user):
+    agenda = Agenda.objects.create(label='Agenda', kind='meetings')
+    for i in range(0, 10):
+        MeetingType.objects.create(agenda=agenda, label='MT %s' % i)
+        desk = Desk.objects.create(agenda=agenda, label='Desk %s' % i)
+        for weekday in (0, 6):
+            TimePeriod.objects.create(
+                weekday=weekday, desk=desk, start_time=datetime.time(10, 0), end_time=datetime.time(12, 0)
+            )
+        # exception starts and ends in the past
+        TimePeriodException.objects.create(
+            desk=desk,
+            start_datetime=now() - datetime.timedelta(days=2),
+            end_datetime=now() - datetime.timedelta(days=1),
+        )
+        if i % 2:
+            # exception starts in the past but ends in the futur
+            TimePeriodException.objects.create(
+                desk=desk,
+                start_datetime=now() - datetime.timedelta(days=1),
+                end_datetime=now() + datetime.timedelta(days=1),
+            )
+        else:
+            # exception in more than 2 weeks
+            TimePeriodException.objects.create(
+                desk=desk,
+                start_datetime=now() + datetime.timedelta(days=20),
+                end_datetime=now() + datetime.timedelta(days=21),
+            )
+
+    app = login(app)
+    with CaptureQueriesContext(connection) as ctx:
+        app.get('/manage/agendas/%s/settings' % agenda.pk)
+        assert len(ctx.captured_queries) == 77
+
+
 def test_delete_agenda(app, admin_user):
     agenda = Agenda(label=u'Foo bar')
     agenda.save()
-- 
2.20.1

