From 9f669f9d895a9830a6378bed020cc78121539e98 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 6 Mar 2020 10:50:45 +0100
Subject: [PATCH 2/2] make tests insensible to postgres/sqlite (#39583)

---
 chrono/manager/views.py       |  2 +-
 tests/test_api.py             | 36 +++++++-------
 tests/test_data_migrations.py | 93 -----------------------------------
 tests/test_import_export.py   |  1 +
 4 files changed, 20 insertions(+), 112 deletions(-)
 delete mode 100644 tests/test_data_migrations.py

diff --git a/chrono/manager/views.py b/chrono/manager/views.py
index c949c81..61cb63d 100644
--- a/chrono/manager/views.py
+++ b/chrono/manager/views.py
@@ -958,7 +958,7 @@ class TimePeriodExceptionDeleteView(ManagedDeskSubobjectMixin, DeleteView):
 
     def get_success_url(self):
         referer = self.request.META.get('HTTP_REFERER')
-        success_url = reverse('chrono-manager-time-period-exception-list', kwargs={'pk': self.desk.agenda_id})
+        success_url = reverse('chrono-manager-time-period-exception-list', kwargs={'pk': self.desk.pk})
         if success_url in referer:
             return success_url
 
diff --git a/tests/test_api.py b/tests/test_api.py
index 2fe92cd..d183ac0 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -65,7 +65,7 @@ def some_data(time_zone, mock_now):
         event = Event(start_datetime=first_date + datetime.timedelta(days=i), places=20, agenda=agenda)
         event.save()
 
-    agenda2 = Agenda(label=u'Foo bar2')
+    agenda2 = Agenda(label=u'Foo bar 2')
     agenda2.save()
     first_date = localtime(now()).replace(hour=20, minute=0, second=0, microsecond=0)
     first_date += datetime.timedelta(days=1)
@@ -111,7 +111,7 @@ def meetings_agenda(time_zone, mock_now):
 
 def test_agendas_api(app, some_data, meetings_agenda):
     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 2')[0]
     resp = app.get('/api/agenda/')
     assert resp.json == {
         'data': [
@@ -127,6 +127,18 @@ def test_agendas_api(app, some_data, meetings_agenda):
                     'fillslots_url': 'http://testserver/api/agenda/%s/fillslots/' % agenda1.slug,
                 },
             },
+            {
+                'text': 'Foo bar 2',
+                'id': u'foo-bar-2',
+                'kind': 'events',
+                'slug': 'foo-bar-2',
+                '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 +152,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,
-                },
-            },
         ]
     }
 
@@ -257,7 +257,7 @@ def test_datetime_api_fr(app, some_data):
 
 
 def test_datetime_api_label(app, some_data):
-    agenda_id = Agenda.objects.filter(label=u'Foo bar2')[0].id
+    agenda_id = Agenda.objects.filter(label=u'Foo bar 2')[0].id
     event = Event.objects.filter(agenda=agenda_id)[0]
     event.label = 'Hello world'
     event.save()
@@ -266,7 +266,7 @@ def test_datetime_api_label(app, some_data):
 
 
 def test_datetime_api_status_url(app, some_data):
-    agenda = Agenda.objects.get(label=u'Foo bar2')
+    agenda = Agenda.objects.get(label=u'Foo bar 2')
     resp = app.get('/api/agenda/%s/datetimes/' % agenda.slug)
     for datum in resp.json['data']:
         assert urlparse.urlparse(datum['api']['status_url']).path == '/api/agenda/%s/status/%s/' % (
@@ -614,7 +614,7 @@ def test_booking_api_fillslots(app, some_data, user):
     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')
+    bookings = Booking.objects.all().order_by('pk')
     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
@@ -1636,7 +1636,7 @@ def test_agenda_detail_api(app, some_data):
 
 def test_agenda_api_date_range(app, some_data):
     # test range limitation
-    agenda2 = Agenda.objects.get(slug='foo-bar2')
+    agenda2 = Agenda.objects.get(slug='foo-bar-2')
     base_date = agenda2.event_set.last().start_datetime.date()
     base_date = base_date + datetime.timedelta(days=1)
 
diff --git a/tests/test_data_migrations.py b/tests/test_data_migrations.py
deleted file mode 100644
index afb1698..0000000
--- a/tests/test_data_migrations.py
+++ /dev/null
@@ -1,93 +0,0 @@
-import datetime
-import pytest
-
-import django
-from django.db import connection
-from django.db.migrations.executor import MigrationExecutor
-from django.utils.timezone import make_aware
-
-from chrono.agendas.models import MeetingType
-
-pytestmark = pytest.mark.django_db
-
-
-def test_meeting_type_slug_migration():
-    if connection.vendor == 'sqlite' and django.VERSION > (1, 11, 0):
-        pytest.skip('SQLite schema editor cannot be used while foreign key constraint checks are enabled.')
-        return
-    executor = MigrationExecutor(connection)
-    migrate_from = [('agendas', '0011_meetingtype_slug')]
-    migrate_to = [('agendas', '0013_auto_20161028_1603')]
-    executor.migrate(migrate_from)
-    executor.loader.build_graph()
-
-    old_apps = executor.loader.project_state(migrate_from).apps
-    OldAgenda = old_apps.get_model('agendas', 'Agenda')
-    OldMeetingType = old_apps.get_model('agendas', 'MeetingType')
-
-    agenda = OldAgenda(label=u'Foo bar')
-    agenda.save()
-
-    meeting_type = OldMeetingType(agenda=agenda, label=u'Baz')
-    meeting_type.save()
-
-    executor.migrate(migrate_to)
-
-    assert MeetingType.objects.get(id=meeting_type.id).slug == 'baz'
-
-
-def test_timeperiod_data_migrations():
-    if connection.vendor == 'sqlite' and django.VERSION > (1, 11, 0):
-        pytest.skip('SQLite schema editor cannot be used while foreign key constraint checks are enabled.')
-        return
-    executor = MigrationExecutor(connection)
-    app = 'agendas'
-    migrate_from = [(app, '0016_desk')]
-    migrate_to = [(app, '0018_event_desk')]
-    executor.migrate(migrate_from)
-    old_apps = executor.loader.project_state(migrate_from).apps
-    Agenda = old_apps.get_model(app, 'Agenda')
-    MeetingType = old_apps.get_model(app, 'MeetingType')
-    TimePeriod = old_apps.get_model(app, 'TimePeriod')
-    Event = old_apps.get_model(app, 'Event')
-    agenda = Agenda.objects.create(label='foo', slug='foo', kind='meetings')
-    agenda2 = Agenda.objects.create(label='bar', slug='bar', kind='events')
-    TimePeriod.objects.create(
-        agenda=agenda, weekday=1, start_time=datetime.time(8, 0), end_time=datetime.time(12, 0)
-    )
-    TimePeriod.objects.create(
-        agenda=agenda, weekday=2, start_time=datetime.time(8, 0), end_time=datetime.time(10, 0)
-    )
-    TimePeriod.objects.create(
-        agenda=agenda, weekday=3, start_time=datetime.time(9, 0), end_time=datetime.time(12, 0)
-    )
-    meeting_type = MeetingType.objects.create(agenda=agenda, label='foo', slug='foo', duration=60)
-    Event.objects.create(
-        agenda=agenda,
-        places=1,
-        meeting_type=meeting_type,
-        start_datetime=make_aware(datetime.datetime(2017, 5, 22, 9, 30)),
-    )
-    Event.objects.create(
-        agenda=agenda,
-        places=1,
-        meeting_type=meeting_type,
-        start_datetime=make_aware(datetime.datetime(2017, 5, 22, 10, 0)),
-    )
-    Event.objects.create(
-        agenda=agenda2, places=5, start_datetime=make_aware(datetime.datetime(2017, 5, 22, 10, 0))
-    )
-    executor.loader.build_graph()
-    executor.migrate(migrate_to)
-    new_apps = executor.loader.project_state(migrate_to).apps
-    Agenda = new_apps.get_model(app, 'Agenda')
-    agenda = Agenda.objects.get(slug='foo')
-    TimePeriod = new_apps.get_model(app, 'TimePeriod')
-    for time_period in TimePeriod.objects.all():
-        assert time_period.desk.label == 'Guichet 1'
-        assert time_period.desk.slug == 'guichet-1'
-
-    Event = new_apps.get_model(app, 'Event')
-    for event in Event.objects.filter(agenda=agenda):
-        assert event.desk.label == 'Guichet 1'
-        assert event.desk.slug == 'guichet-1'
diff --git a/tests/test_import_export.py b/tests/test_import_export.py
index f2c95c7..8190577 100644
--- a/tests/test_import_export.py
+++ b/tests/test_import_export.py
@@ -91,6 +91,7 @@ def test_import_export(app, some_data, meetings_agenda):
 
     event = Event(agenda=agenda1, start_datetime=make_aware(datetime.datetime.now()), places=10)
     event.save()
+    desk, _ = Desk.objects.get_or_create(agenda=agenda2, label='Desk A', slug='desk-a')
     timeperiod = TimePeriod(
         weekday=2, desk=desk, start_time=datetime.time(10, 0), end_time=datetime.time(11, 0)
     )
-- 
2.20.1

