Skip to content
Snippets Groups Projects
Commit 11f8faed authored by fu2662cw's avatar fu2662cw :speech_balloon:
Browse files

Connect Question to Criterion

parent 24099829
No related branches found
No related tags found
No related merge requests found
...@@ -4,4 +4,3 @@ from django.contrib import admin ...@@ -4,4 +4,3 @@ from django.contrib import admin
from .models import Sport from .models import Sport
admin.site.register(Sport) admin.site.register(Sport)
...@@ -2,5 +2,5 @@ from django.apps import AppConfig ...@@ -2,5 +2,5 @@ from django.apps import AppConfig
class QuizConfig(AppConfig): class QuizConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField' default_auto_field = "django.db.models.BigAutoField"
name = 'quiz' name = "quiz"
# 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'),
),
]
# 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,
),
]
...@@ -60,3 +60,6 @@ class Question(models.Model): ...@@ -60,3 +60,6 @@ class Question(models.Model):
"""Defines a Question that is assigned to exactly one Criterion""" """Defines a Question that is assigned to exactly one Criterion"""
text = models.TextField() text = models.TextField()
criterion = models.OneToOneField(
Criterion, on_delete=models.CASCADE, primary_key=True
)
""" This module tests all our quiz models""" """ This module tests all our quiz models"""
from django.test import TestCase from django.test import TestCase
from .models import Sport, Criterion, Question from .models import Sport, Criterion, Question
...@@ -65,23 +64,43 @@ class CriterionRatingTest(TestCase): ...@@ -65,23 +64,43 @@ class CriterionRatingTest(TestCase):
self.assertEqual(self.test_sport.get_rating(criterion=self.criterion), 8) self.assertEqual(self.test_sport.get_rating(criterion=self.criterion), 8)
class CriterionModelTest(TestCase): class CriterionAndQuestionModelTest(TestCase):
"""Tests the Criterion model""" """Tests the Criterion and the Question model which have a One to One Relation"""
def test_criterion_can_be_created(self): def setUp(self):
"""New criterion is saved to the db""" self.name = "Einzelsport"
name = "Einzelsport" self.criterion = Criterion(name=self.name)
Criterion(name=name).save() 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() 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): with self.assertRaises(Criterion.question.RelatedObjectDoesNotExist):
"""Tests the Question model""" self.criterion.question
def test_question_can_be_saved_and_loaded(self): def test_question_can_be_saved_and_loaded(self):
"""New Question is saved to the db and can be loaded""" """New Question is saved to the db and can be loaded"""
text = "Ich trainiere gerne mit anderen im Team" text = "Ich trainiere gerne mit anderen im Team"
Question(text=text).save() Question(text=text, criterion=self.criterion).save()
test_question = Question.objects.first() test_question = Question.objects.first()
self.assertEqual(test_question.text, text) self.assertEqual(test_question.text, text)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment