From b6a445de24e4a0a49f2f75c53d24c3b91eeacaaf Mon Sep 17 00:00:00 2001 From: Tom Walker Date: Thu, 19 Jun 2014 09:58:36 +0100 Subject: [PATCH] working on tests for quiz/models --- multichoice/tests.py | 21 ++++++++---- quiz/models.py | 28 +++++----------- quiz/templatetags/quiz_tags.py | 3 +- quiz/tests.py | 60 ++++++++++++++++++++++++++++++++++ templates/quiz/question.html | 2 +- 5 files changed, 85 insertions(+), 29 deletions(-) diff --git a/multichoice/tests.py b/multichoice/tests.py index 89bd22d..b89952f 100644 --- a/multichoice/tests.py +++ b/multichoice/tests.py @@ -11,17 +11,24 @@ class TestMCQuestionModel(TestCase): "swallow?"), explanation = "I, I don't know that!",) - Answer.objects.create(question = q, + Answer.objects.create(id = 123, + question = q, content = "African", correct = False,) - Answer.objects.create(question = q, + Answer.objects.create(id = 456, + question = q, content = "European", correct = True) - def test_correct_answer(self): - china = Country.objects.get(name="China") - self.assertEqual(china.population, 1400000000) - self.assertEqual(china.climate, 'TEMPERATE') - self.assertEqual(china.healthcare, 4) + def test_answers(self): + q = MCQuestion.objects.get(id = 1) + answers = Answer.objects.filter(question__id = q.id) + correct_a = Answer.objects.get(question__id = q.id, + correct = True,) + + self.assertEqual(answers.count(), 2) + self.assertEqual(correct_a.content, "European") + self.assertEqual(q.check_if_correct(123), False) + self.assertEqual(q.check_if_correct(456), True) diff --git a/quiz/models.py b/quiz/models.py index 811aca3..69e4975 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -39,20 +39,12 @@ CATEGORY_CHOICES = ( ('Endocrinology', 'Endocrinology'), ) -""" -Category used to define a category for either a quiz or question -""" - class CategoryManager(models.Manager): - """ - custom manager for Progress class - """ + def new_category(self, category): - """ - add a new category, replacing spaces and making lowercase - """ new_category = self.create(category = - category.replace(' ', '-').lower()) + re.sub('\s+', '-', category).lower()) + new_category.save() @@ -74,10 +66,6 @@ class Category(models.Model): return self.category -""" -Quiz is a container that can be filled with various different question types. -""" - class Quiz(models.Model): title = models.CharField(max_length = 60, @@ -112,13 +100,13 @@ class Quiz(models.Model): attempt by a user will be stored",) - def save(self, force_insert = False, force_update = False): - self.url = self.url.replace(' ', '-').lower() + def save(self, force_insert = False, force_update = False, *args, **kwargs): + self.url = re.sub('\s+', '-', self.url).lower() - self.url = ''.join(letter for letter in self.url if letter.isalnum() - or letter == '-') # removes non-alphanumerics + self.url = ''.join(letter for letter in self.url if + letter.isalnum() or letter == '-') - super(Quiz, self).save(force_insert, force_update) + super(Quiz, self).save(force_insert, force_update, *args, **kwargs) class Meta: diff --git a/quiz/templatetags/quiz_tags.py b/quiz/templatetags/quiz_tags.py index 0492f95..49ad25b 100644 --- a/quiz/templatetags/quiz_tags.py +++ b/quiz/templatetags/quiz_tags.py @@ -4,8 +4,9 @@ from multichoice.models import Answer register = template.Library() @register.inclusion_tag('answers_for_mc_question.html', takes_context=True) -def answers_for_mc_question(context, question, quiz): +def answers_for_mc_question(context, question): answers = Answer.objects.filter(question__id=question.id).order_by('?') + print answers return {'answers': answers,} @register.inclusion_tag('correct_answer.html', takes_context=True) diff --git a/quiz/tests.py b/quiz/tests.py index cf4e4db..2bdda93 100644 --- a/quiz/tests.py +++ b/quiz/tests.py @@ -1,3 +1,6 @@ +# -*- coding: iso-8859-15 -*- + + from django.test import TestCase from quiz.models import Category, Quiz, Progress, Sitting, Question @@ -5,3 +8,60 @@ from quiz.models import Category, Quiz, Progress, Sitting, Question class TestCategory(TestCase): def setUp(self): Category.objects.new_category(category = "elderberries") + Category.objects.new_category(category = "straw.berries") + Category.objects.new_category(category = "black berries") + Category.objects.new_category(category = "squishy berries") + + def test_categories(self): + c1 = Category.objects.get(id = 1) + c2 = Category.objects.get(id = 2) + c3 = Category.objects.get(id = 3) + c4 = Category.objects.get(id = 4) + + self.assertEqual(c1.category, "elderberries") + self.assertEqual(c2.category, "straw.berries") + self.assertEqual(c3.category, "black-berries") + self.assertEqual(c4.category, "squishy-berries") + +class TestQuiz(TestCase): + def setUp(self): + Category.objects.new_category(category = "elderberries") + Quiz.objects.create(id = 1, + title = "test quiz 1", + description = "d1", + url = "tq1",) + Quiz.objects.create(id = 2, + title = "test quiz 2", + description = "d2", + url = "t q2",) + Quiz.objects.create(id = 3, + title = "test quiz 3", + description = "d3", + url = "t q3",) + Quiz.objects.create(id = 4, + title = "test quiz 4", + description = "d4", + url = "t-!£$%^&*q4",) + + + def test_quiz_url(self): + q1 = Quiz.objects.get(id = 1) + q2 = Quiz.objects.get(id = 2) + q3 = Quiz.objects.get(id = 3) + q4 = Quiz.objects.get(id = 4) + + self.assertEqual(q1.url, "tq1") + self.assertEqual(q2.url, "t-q2") + self.assertEqual(q3.url, "t-q3") + self.assertEqual(q4.url, "t-q4") + + def test_quiz_options(self): + c1 = Category.objects.get(id = 1) + + q5 = Quiz.objects.create(id = 5, + title = "test quiz 5", + description = "d5", + url = "tq5", + category = c1,) + + self.assertEqual(q5.category.category, c1.category) diff --git a/templates/quiz/question.html b/templates/quiz/question.html index 5981793..c4913d8 100644 --- a/templates/quiz/question.html +++ b/templates/quiz/question.html @@ -57,7 +57,7 @@ {% endifequal %} {% ifequal question_type 'MCQuestion' %} - {% answers_for_mc_question question quiz %} + {% answers_for_mc_question question %} {% endifequal %} -- 2.39.5