From 2f3343fa67f25440ee0cfb62a12f77af8fe4894e Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Thu, 12 Dec 2019 00:34:06 +0100
Subject: [PATCH] Serialise rollbacks in tests and make data migrations
 deterministic

---
 .../0007_unlink_school_schoolterm.py          | 31 +++++++++++++++++++
 biscuit/core/models.py                        | 11 ++++++-
 biscuit/core/tests/browser/test_selenium.py   |  2 ++
 3 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 biscuit/core/migrations/0007_unlink_school_schoolterm.py

diff --git a/biscuit/core/migrations/0007_unlink_school_schoolterm.py b/biscuit/core/migrations/0007_unlink_school_schoolterm.py
new file mode 100644
index 000000000..4e26febd8
--- /dev/null
+++ b/biscuit/core/migrations/0007_unlink_school_schoolterm.py
@@ -0,0 +1,31 @@
+# Generated by Django 2.2.8 on 2019-12-11 23:27
+
+from django.db import migrations, models
+
+
+def mark_current_term(apps, schema_editor):
+    db_alias = schema_editor.connection.alias
+
+    SchoolTerm = apps.get_model('core', 'SchoolTerm')  # noqa
+
+    if not SchoolTerm.objects.filter(current=True).exists():
+        SchoolTerm.objects.using(db_alias).latest('date_start').update(current=True)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('core', '0006_create_superuser'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='school',
+            name='current_term',
+        ),
+        migrations.AddField(
+            model_name='schoolterm',
+            name='current',
+            field=models.NullBooleanField(default=None, unique=True),
+        ),
+    ]
diff --git a/biscuit/core/models.py b/biscuit/core/models.py
index ffd0189e1..ae3f5950a 100644
--- a/biscuit/core/models.py
+++ b/biscuit/core/models.py
@@ -38,7 +38,9 @@ class School(models.Model):
     logo = ImageCropField(verbose_name=_('School logo'), blank=True, null=True)
     logo_cropping = ImageRatioField('logo', '600x600', size_warning=True)
 
-    current_term = models.ForeignKey('SchoolTerm', models.CASCADE, related_name='+')
+    @property
+    def current_term(self):
+        return SchoolTerm.objects.get(current=True)
 
     class Meta:
         ordering = ['name', 'name_official']
@@ -57,6 +59,13 @@ class SchoolTerm(models.Model):
     date_end = models.DateField(verbose_name=_(
         'Effective end date of term'), null=True)
 
+    current = models.NullBooleanField(default=None, unique=True)
+
+    def save(self, *args, **kwargs):
+        if self.current is False:
+            self.current = None
+        super().save(*args, **kwargs)
+
 
 class Person(models.Model, ExtensibleModel):
     """ A model describing any person related to a school, including, but not
diff --git a/biscuit/core/tests/browser/test_selenium.py b/biscuit/core/tests/browser/test_selenium.py
index 918ec79c2..fcdde2165 100644
--- a/biscuit/core/tests/browser/test_selenium.py
+++ b/biscuit/core/tests/browser/test_selenium.py
@@ -11,6 +11,8 @@ SeleniumTestCaseBase.browsers = list(filter(bool, os.environ.get('TEST_SELENIUM_
 SeleniumTestCaseBase.selenium_hub = os.environ.get('TEST_SELENIUM_HUB', '') or None
 
 class SeleniumTests(SeleniumTestCase):
+    serialized_rollback = True
+
     @classmethod
     def _screenshot(cls, filename):
         screenshot_path = os.environ.get('TEST_SCREENSHOT_PATH', None)
-- 
GitLab