From: Tom Walker Date: Mon, 23 Jun 2014 21:42:28 +0000 (+0100) Subject: slight improvement of readability using any() in quiz/models, and working on views... X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=335bdaefaf577901266257583c0703e89ef4a7a6;p=django_quiz.git slight improvement of readability using any() in quiz/models, and working on views relating to questions for anon users --- diff --git a/quiz/models.py b/quiz/models.py index 32ba608..d1ce6ac 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -244,9 +244,9 @@ class Progress(models.Model): category_test = Category.objects.filter(category = category_queried) \ .exists() - 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: + if any([category_test == False, score_to_add == False, + possible_to_add == False, str(score_to_add).isdigit() == False, + 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 3df4e08..afebe93 100644 --- a/quiz/tests.py +++ b/quiz/tests.py @@ -1,9 +1,12 @@ # -*- coding: iso-8859-15 -*- from django.contrib.auth.models import User, AnonymousUser +from django.core.urlresolvers import resolve +from django.http import HttpRequest from django.test import TestCase from django.test.client import Client, RequestFactory + from quiz.models import Category, Quiz, Progress, Sitting, Question from quiz.views import quiz_take from multichoice.models import MCQuestion @@ -312,15 +315,39 @@ class TestQuestionViewsAnon(TestCase): self.factory = RequestFactory() - def test_quiz_take_anon(self): - request = self.client.get('/q/tq1/') - request.user = AnonymousUser() - request.session = self.client.session - request.session['set_expiry'] = 0 + def test_quiz_take_anon_view_only(self): + found = resolve('/q/tq1/') - print request + self.assertEqual(found.func, quiz_take) + self.assertEqual(found.kwargs, {'quiz_name': 'tq1'}) + self.assertEqual(found.url_name, 'quiz_start_page') - self.assertContains(response, 'Sign up') + response = self.client.get('/q/tq1/') + + self.assertContains(response, 'squawk', status_code = 200) + self.assertEqual(self.client.session.get_expiry_age(), 259200) + self.assertEqual(self.client.session['1_q_list'], [1, 2]) + self.assertEqual(self.client.session['1_score'], 0) + self.assertEqual(self.client.session['page_count'], 0) + self.assertEqual(response.context['quiz'].id, 1) + self.assertEqual(response.context['question'].content, "squawk") + self.assertEqual(response.context['question_type'], "MCQuestion") + self.assertEqual(response.context['previous'], {}) + self.assertEqual(response.context['show_advert'], False) + self.assertTemplateUsed('question.html') + + session = self.client.session + session.set_expiry(1) # session is set when user first + session.save() # accesses a quiz + + response2 = self.client.get('/q/tq1/') + self.assertEqual(self.client.session.get_expiry_age(), 1) + self.assertEqual(self.client.session['1_q_list'], [1, 2]) + self.assertEqual(self.client.session['1_score'], 0) + self.assertEqual(self.client.session['page_count'], 0) + + def test_quiz_take_anon_submit(self): + response = self.client.get('/q/tq1/') class TestQuestionViewsUser(TestCase):