]> git.parisson.com Git - django_quiz.git/commitdiff
working on tests for quiz/models
authorTom Walker <tomwalker0472@gmail.com>
Thu, 19 Jun 2014 08:58:36 +0000 (09:58 +0100)
committerTom Walker <tomwalker0472@gmail.com>
Thu, 19 Jun 2014 08:58:36 +0000 (09:58 +0100)
multichoice/tests.py
quiz/models.py
quiz/templatetags/quiz_tags.py
quiz/tests.py
templates/quiz/question.html

index 89bd22dca252925518b762cdb8b31264a26fef69..b89952f24f9a29c33046d27338777eb0f3e4e19e 100644 (file)
@@ -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)
index 811aca3b96b8d5a4b0bcb24b55c38d78ac012d1c..69e4975f575cc9c6d687352ba4ccfc35971edc7a 100644 (file)
@@ -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:
index 0492f959be410a1a06609e114f5b679d8a08f184..49ad25beee68a1eb0a7d02a6cafaee00234e3607 100644 (file)
@@ -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)
index cf4e4db0d27523af2608abdebbb213e5ec63d547..2bdda934879ce58487f03e5cea19700433b7c14d 100644 (file)
@@ -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)
index 59817930adb5d226fe3d4205beb15f1bea74cab7..c4913d809dd571c87a51a4cd67fecfe1342d575d 100644 (file)
@@ -57,7 +57,7 @@
                          {% endifequal %}
 
                          {% ifequal question_type 'MCQuestion' %}
-                         {% answers_for_mc_question question quiz %}
+                         {% answers_for_mc_question question %}
                          {% endifequal %}
 
                        </tbody>