From: Tom Walker Date: Thu, 19 Jun 2014 18:57:01 +0000 (+0100) Subject: continuing to work on the quiz/models.py X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=a00bbe26b3e97606776f445a74b7d0f36a43d1f7;p=django_quiz.git continuing to work on the quiz/models.py --- diff --git a/quiz/models.py b/quiz/models.py index 69e4975..b4cd73a 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -161,7 +161,6 @@ class Progress(models.Model): the third is the percentage correct. The dict will have one key for every category that you have defined - BUT """ categories = Category.objects.all() @@ -238,13 +237,17 @@ class Progress(models.Model): Increase if all were correct. Does not return anything. + + TO DO: Raise error when necessary """ category_test = Category.objects.filter(category = category_queried) \ .exists() - if category_test == False: - return "error", "category does not exist" + if category_test == False or score_to_add == False or \ + possible_to_add == False or str(score_to_add).isdigit() == False or \ + str(possible_to_add).isdigit() == False: + return "error", "category does not exist or invalid score" to_find = re.escape(str(category_queried)) + r",(\d+),(\d+)," diff --git a/quiz/tests.py b/quiz/tests.py index 2bdda93..46c828c 100644 --- a/quiz/tests.py +++ b/quiz/tests.py @@ -1,6 +1,6 @@ # -*- coding: iso-8859-15 -*- - +from django.contrib.auth.models import User from django.test import TestCase from quiz.models import Category, Quiz, Progress, Sitting, Question @@ -59,9 +59,108 @@ class TestQuiz(TestCase): c1 = Category.objects.get(id = 1) q5 = Quiz.objects.create(id = 5, - title = "test quiz 5", - description = "d5", - url = "tq5", - category = c1,) + title = "test quiz 5", + description = "d5", + url = "tq5", + category = c1, + exam_paper = True,) self.assertEqual(q5.category.category, c1.category) + self.assertEqual(q5.random_order, False) + self.assertEqual(q5.answers_at_end, False) + self.assertEqual(q5.exam_paper, True) + + +class TestProgress(TestCase): + def setUp(self): + Category.objects.new_category(category = "elderberries") + + Quiz.objects.create(id = 1, + title = "test quiz 1", + description = "d1", + url = "tq1",) + + self.user = User.objects.create_user(username = "jacob", + email = "jacob@jacob.com", + password = "top_secret") + + + def test_list_all_empty(self): + p1 = Progress.objects.new_progress(self.user) + self.assertEqual(p1.score, "") + + category_dict = p1.list_all_cat_scores() + + self.assertIn(str(category_dict.keys()[0]), p1.score) + + category_dict = p1.list_all_cat_scores() + + self.assertIn("elderberries", p1.score) + + Category.objects.new_category(category = "cheese") + + category_dict = p1.list_all_cat_scores() + + self.assertIn("cheese", p1.score) + + def test_check_cat(self): + p1 = Progress.objects.new_progress(self.user) + elderberry_score = p1.check_cat_score("elderberries") + + self.assertEqual(elderberry_score, (0, 0)) + + fake_score = p1.check_cat_score("monkey") + + self.assertEqual(fake_score, ("error", "category does not exist")) + + Category.objects.new_category(category = "cheese") + cheese_score = p1.check_cat_score("cheese") + + self.assertEqual(cheese_score, (0, 0)) + self.assertIn("cheese", p1.score) + + def test_update_score(self): + p1 = Progress.objects.new_progress(self.user) + p1.list_all_cat_scores() + p1.update_score("elderberries", 1, 2) + elderberry_score = p1.check_cat_score("elderberries") + + self.assertEqual(elderberry_score, (1, 2)) + + Category.objects.new_category(category = "cheese") + p1.update_score("cheese", 3, 4) + cheese_score = p1.check_cat_score("cheese") + + self.assertEqual(cheese_score, (3, 4)) + + fake_cat = p1.update_score("hamster", 3, 4) + self.assertIn('error', str(fake_cat)) + + non_int = p1.update_score("hamster", "1", "a") + self.assertIn('error', str(non_int)) + + +class TestSitting(TestCase): + def setUp(self): + q1 = Quiz.objects.create(id = 1, + title = "test quiz 1", + description = "d1", + url = "tq1",) + + Question.objects.create(id = 1, + quiz = q1, + content = "squawk",) + + Question.objects.create(id = 2, + quiz = q1, + content = "squeek",) + + self.user = User.objects.create_user(username = "jacob", + email = "jacob@jacob.com", + password = "top_secret") + + Sitting.objects.new_sitting(self.user, q1) + + def test_get_next(self): + s1 = Sitting.objects.get(id = 1) + print s1.question_list