From 8a9a80831bdf8c2961a166e0db33a9e2bb3255af Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Thu, 6 Feb 2020 15:21:48 +0100
Subject: [PATCH 2/2] make tests insensible to postgres/sqlite (#39583)

---
 tests/test_api.py | 62 ++++++++++++++++++++++++++++++++---------------
 1 file changed, 43 insertions(+), 19 deletions(-)

diff --git a/tests/test_api.py b/tests/test_api.py
index 0538935..6e6d800 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -109,9 +109,29 @@ def meetings_agenda(time_zone, mock_now):
     return agenda
 
 
-def test_agendas_api(app, some_data, meetings_agenda):
+def test_agendas_api(app, meetings_agenda):
+    agenda = Agenda(label=u'Foo bar')
+    agenda.save()
+    first_date = localtime(now()).replace(hour=17, minute=0, second=0, microsecond=0)
+    first_date += datetime.timedelta(days=1)
+    for i in range(3):
+        event = Event(start_datetime=first_date + datetime.timedelta(days=i), places=20, agenda=agenda)
+        event.save()
+
+    agenda2 = Agenda(label=u'Foo bar Again')
+    agenda2.save()
+    first_date = localtime(now()).replace(hour=20, minute=0, second=0, microsecond=0)
+    first_date += datetime.timedelta(days=1)
+    for i in range(2):
+        event = Event(start_datetime=first_date + datetime.timedelta(days=i), places=20, agenda=agenda2)
+        event.save()
+
+    # a date in the past
+    event = Event(start_datetime=first_date - datetime.timedelta(days=10), places=10, agenda=agenda)
+    event.save()
+
     agenda1 = Agenda.objects.filter(label=u'Foo bar')[0]
-    agenda2 = Agenda.objects.filter(label=u'Foo bar2')[0]
+    agenda2 = Agenda.objects.filter(label=u'Foo bar Again')[0]
     resp = app.get('/api/agenda/')
     assert resp.json == {
         'data': [
@@ -127,6 +147,18 @@ def test_agendas_api(app, some_data, meetings_agenda):
                     'fillslots_url': 'http://testserver/api/agenda/%s/fillslots/' % agenda1.slug,
                 },
             },
+            {
+                'text': 'Foo bar Again',
+                'id': u'foo-bar-again',
+                'kind': 'events',
+                'slug': 'foo-bar-again',
+                'minimal_booking_delay': 1,
+                'maximal_booking_delay': 56,
+                'api': {
+                    'datetimes_url': 'http://testserver/api/agenda/%s/datetimes/' % agenda2.slug,
+                    'fillslots_url': 'http://testserver/api/agenda/%s/fillslots/' % agenda2.slug,
+                },
+            },
             {
                 'text': 'Foo bar Meeting',
                 'id': u'foo-bar-meeting',
@@ -140,18 +172,6 @@ def test_agendas_api(app, some_data, meetings_agenda):
                     'fillslots_url': 'http://testserver/api/agenda/%s/fillslots/' % meetings_agenda.slug,
                 },
             },
-            {
-                'text': 'Foo bar2',
-                'id': u'foo-bar2',
-                'kind': 'events',
-                'slug': 'foo-bar2',
-                'minimal_booking_delay': 1,
-                'maximal_booking_delay': 56,
-                'api': {
-                    'datetimes_url': 'http://testserver/api/agenda/%s/datetimes/' % agenda2.slug,
-                    'fillslots_url': 'http://testserver/api/agenda/%s/fillslots/' % agenda2.slug,
-                },
-            },
         ]
     }
 
@@ -609,11 +629,15 @@ def test_booking_api_fillslots(app, some_data, user):
     assert 'cancel_url' in resp.json['api']
     assert urlparse.urlparse(resp.json['api']['cancel_url']).netloc
     assert Booking.objects.count() == 3
-    # these 3 bookings are related, the first is the primary one
-    bookings = Booking.objects.all().order_by('primary_booking')
-    assert bookings[0].primary_booking is None
-    assert bookings[1].primary_booking.id == bookings[0].id == primary_booking_id
-    assert bookings[2].primary_booking.id == bookings[0].id == primary_booking_id
+
+    # there must be one primary booking
+    primary_booking = Booking.objects.filter(primary_booking__isnull=True).get()
+
+    # these 2 bookings are related to the primary booking
+    secondary_bookings = Booking.objects.exclude(primary_booking__isnull=True).all()
+    assert len(secondary_bookings) == 2
+    assert secondary_bookings[0].primary_booking.id == primary_booking.id
+    assert secondary_bookings[1].primary_booking.id == primary_booking.id
 
     # access by slug
     resp = app.post('/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': events_slugs})
-- 
2.20.1

