From 954f83e3d0b615092282aa751462ddafa008b7b4 Mon Sep 17 00:00:00 2001 From: tomwalker Date: Thu, 25 Sep 2014 16:31:11 +0100 Subject: [PATCH] cleaned up an 'any()' check, extending tests for quiz/models.progress.update_score() --- quiz/models.py | 10 +++++----- quiz/tests.py | 11 ++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/quiz/models.py b/quiz/models.py index 4ab5a56..13898db 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -243,11 +243,11 @@ class Progress(models.Model): category_test = Category.objects.filter(category=question.category)\ .exists() - if any([category_test is False, - score_to_add is False, - possible_to_add is False, - str(score_to_add).isdigit() is False, - str(possible_to_add).isdigit() is False]): + if any([item is False for item in [category_test, + score_to_add, + possible_to_add, + isinstance(score_to_add, int), + isinstance(possible_to_add, int)]]): return "error", "category does not exist or invalid score" to_find = re.escape(str(question.category)) +\ diff --git a/quiz/tests.py b/quiz/tests.py index cb495b2..09d68b5 100644 --- a/quiz/tests.py +++ b/quiz/tests.py @@ -165,12 +165,21 @@ class TestProgress(TestCase): self.assertIn('cheese', self.p1.list_all_cat_scores) self.assertEqual([3, 4, 75], self.p1.list_all_cat_scores['cheese']) + # pass in string instead of question instance with self.assertRaises(AttributeError): self.p1.update_score('hamster', 3, 4) - non_int = self.p1.update_score(question2, '1', 'a') + non_int = self.p1.update_score(question2, '1', 2) self.assertIn('error', str(non_int)) + # negative possible score + self.p1.update_score(question2, 0, -1) + self.assertEqual([3, 5, 60], self.p1.list_all_cat_scores['cheese']) + + # negative added score + self.p1.update_score(question2, -1, 1) + self.assertEqual([4, 6, 67], self.p1.list_all_cat_scores['cheese']) + class TestSitting(TestCase): def setUp(self): -- 2.39.5