From 11f8faed95e030bb64fd62bb810eefad3baa0cee Mon Sep 17 00:00:00 2001
From: Noah Jonah Gente <fu2662cw@mi.fu-berlin.de>
Date: Wed, 26 May 2021 22:44:32 +0200
Subject: [PATCH] Connect Question to Criterion

---
 unisportomat/quiz/admin.py                    |  1 -
 unisportomat/quiz/apps.py                     |  4 +-
 .../migrations/0003_criterion_question.py     | 19 +++++++++
 .../migrations/0004_auto_20210526_2014.py     | 28 +++++++++++++
 unisportomat/quiz/models.py                   |  3 ++
 unisportomat/quiz/tests.py                    | 41 ++++++++++++++-----
 6 files changed, 82 insertions(+), 14 deletions(-)
 create mode 100644 unisportomat/quiz/migrations/0003_criterion_question.py
 create mode 100644 unisportomat/quiz/migrations/0004_auto_20210526_2014.py

diff --git a/unisportomat/quiz/admin.py b/unisportomat/quiz/admin.py
index 83f1f9a..f56f313 100644
--- a/unisportomat/quiz/admin.py
+++ b/unisportomat/quiz/admin.py
@@ -4,4 +4,3 @@ from django.contrib import admin
 from .models import Sport
 
 admin.site.register(Sport)
-
diff --git a/unisportomat/quiz/apps.py b/unisportomat/quiz/apps.py
index 3dc8afe..d27c63a 100644
--- a/unisportomat/quiz/apps.py
+++ b/unisportomat/quiz/apps.py
@@ -2,5 +2,5 @@ from django.apps import AppConfig
 
 
 class QuizConfig(AppConfig):
-    default_auto_field = 'django.db.models.BigAutoField'
-    name = 'quiz'
+    default_auto_field = "django.db.models.BigAutoField"
+    name = "quiz"
diff --git a/unisportomat/quiz/migrations/0003_criterion_question.py b/unisportomat/quiz/migrations/0003_criterion_question.py
new file mode 100644
index 0000000..9b3c850
--- /dev/null
+++ b/unisportomat/quiz/migrations/0003_criterion_question.py
@@ -0,0 +1,19 @@
+# Generated by Django 3.2 on 2021-05-26 19:50
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('quiz', '0002_auto_20210525_1514'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='criterion',
+            name='question',
+            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='quiz.question'),
+        ),
+    ]
diff --git a/unisportomat/quiz/migrations/0004_auto_20210526_2014.py b/unisportomat/quiz/migrations/0004_auto_20210526_2014.py
new file mode 100644
index 0000000..a0fcc71
--- /dev/null
+++ b/unisportomat/quiz/migrations/0004_auto_20210526_2014.py
@@ -0,0 +1,28 @@
+# Generated by Django 3.2 on 2021-05-26 20:14
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('quiz', '0003_criterion_question'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='criterion',
+            name='question',
+        ),
+        migrations.RemoveField(
+            model_name='question',
+            name='id',
+        ),
+        migrations.AddField(
+            model_name='question',
+            name='criterion',
+            field=models.OneToOneField(default=None, on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to='quiz.criterion'),
+            preserve_default=False,
+        ),
+    ]
diff --git a/unisportomat/quiz/models.py b/unisportomat/quiz/models.py
index 6df7d94..a9e9563 100644
--- a/unisportomat/quiz/models.py
+++ b/unisportomat/quiz/models.py
@@ -60,3 +60,6 @@ class Question(models.Model):
     """Defines a Question that is assigned to exactly one Criterion"""
 
     text = models.TextField()
+    criterion = models.OneToOneField(
+        Criterion, on_delete=models.CASCADE, primary_key=True
+    )
diff --git a/unisportomat/quiz/tests.py b/unisportomat/quiz/tests.py
index a0c4bde..8455a4e 100644
--- a/unisportomat/quiz/tests.py
+++ b/unisportomat/quiz/tests.py
@@ -1,5 +1,4 @@
 """ This module tests all our quiz models"""
-
 from django.test import TestCase
 from .models import Sport, Criterion, Question
 
@@ -65,23 +64,43 @@ class CriterionRatingTest(TestCase):
         self.assertEqual(self.test_sport.get_rating(criterion=self.criterion), 8)
 
 
-class CriterionModelTest(TestCase):
-    """Tests the Criterion model"""
+class CriterionAndQuestionModelTest(TestCase):
+    """Tests the Criterion and the Question model which have a One to One Relation"""
 
-    def test_criterion_can_be_created(self):
-        """New criterion is saved to the db"""
-        name = "Einzelsport"
-        Criterion(name=name).save()
+    def setUp(self):
+        self.name = "Einzelsport"
+        self.criterion = Criterion(name=self.name)
+        self.criterion.save()
+
+    def test_criterion_can_be_saved_and_loaded(self):
+        """New criterion can be loaded from the db"""
         test_criterion = Criterion.objects.first()
-        self.assertEqual(test_criterion.name, name)
+        self.assertEqual(test_criterion.name, self.name)
 
+    def test_question_can_be_added(self):
+        """
+        If a question is added to a criterion, then it is accessible through the criterion.
+        """
+        text = "Ich trainiere gerne mit anderen im Team"
+        question = Question(text=text, criterion=self.criterion)
+        question.save()
+        self.criterion.question = question
+        self.assertEqual(question, self.criterion.question)
+
+    def test_criterion_stays_if_question_deleted(self):
+        """If assigned question is deleted the foreign key is set None"""
+        text = "Ich trainiere gerne mit anderen im Team"
+        question = Question(text=text, criterion=self.criterion)
+        question.save()
+        question.delete()
+        self.criterion = Criterion.objects.first()
 
-class QuestionModelTest(TestCase):
-    """Tests the Question model"""
+        with self.assertRaises(Criterion.question.RelatedObjectDoesNotExist):
+            self.criterion.question
 
     def test_question_can_be_saved_and_loaded(self):
         """New Question is saved to the db and can be loaded"""
         text = "Ich trainiere gerne mit anderen im Team"
-        Question(text=text).save()
+        Question(text=text, criterion=self.criterion).save()
         test_question = Question.objects.first()
         self.assertEqual(test_question.text, text)
-- 
GitLab