diff --git a/unisportomat/quiz/apps.py b/unisportomat/quiz/apps.py index 81351a5b0822edf832ecdcdc118988ae6446e86f..1b9381ba14ab1072e8d8b014251eea4f61b263e0 100644 --- a/unisportomat/quiz/apps.py +++ b/unisportomat/quiz/apps.py @@ -4,6 +4,7 @@ from django.apps import AppConfig class QuizConfig(AppConfig): - """Application Config for the quiz app """ - default_auto_field = 'django.db.models.BigAutoField' - name = 'quiz' + """Application Config for the quiz app""" + + default_auto_field = "django.db.models.BigAutoField" + name = "quiz" diff --git a/unisportomat/quiz/migrations/0002_auto_20210525_1514.py b/unisportomat/quiz/migrations/0002_auto_20210525_1514.py new file mode 100644 index 0000000000000000000000000000000000000000..e23bd6866ebabb0d43e81a0cdff00355d5b9bda7 --- /dev/null +++ b/unisportomat/quiz/migrations/0002_auto_20210525_1514.py @@ -0,0 +1,39 @@ +# Generated by Django 3.2 on 2021-05-25 15:14 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("quiz", "0001_refactor_sport_and_add_criteria"), + ] + + operations = [ + migrations.CreateModel( + name="Question", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("text", models.TextField()), + ], + ), + migrations.AlterField( + model_name="criterionrating", + name="rating", + field=models.IntegerField( + validators=[ + django.core.validators.MaxValueValidator(10), + django.core.validators.MinValueValidator(1), + ] + ), + ), + ] diff --git a/unisportomat/quiz/migrations/0003_criterion_question.py b/unisportomat/quiz/migrations/0003_criterion_question.py new file mode 100644 index 0000000000000000000000000000000000000000..9b3c850d5f3e5d6c7c03fab3b93b58143e75dea1 --- /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 0000000000000000000000000000000000000000..a0fcc71132c920e236a3b17b9f51ed3df1bd9012 --- /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 a3b25fde270a944af9ecd5d8a89ad8a129c2c66e..a9e9563fab1932bd9f786c904afed72a8c2d1a94 100644 --- a/unisportomat/quiz/models.py +++ b/unisportomat/quiz/models.py @@ -54,3 +54,12 @@ class Criterion(models.Model): """ name = models.TextField() + + +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 3c8a3f087dfcd217ae3297e625c2355ec692c3b7..8455a4ea7ae0d47caa5fcdbf6ac6d2dfadd382f1 100644 --- a/unisportomat/quiz/tests.py +++ b/unisportomat/quiz/tests.py @@ -1,7 +1,6 @@ """ This module tests all our quiz models""" - from django.test import TestCase -from .models import Sport, Criterion +from .models import Sport, Criterion, Question class SportModelTest(TestCase): @@ -65,12 +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 setUp(self): + self.name = "Einzelsport" + self.criterion = Criterion(name=self.name) + self.criterion.save() - def test_criterion_can_be_created(self): - """New criterion is saved to the db""" - name = "Einzelsport" - Criterion(name=name).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() + + 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, criterion=self.criterion).save() + test_question = Question.objects.first() + self.assertEqual(test_question.text, text)