From 1ccaa688a37b32fe8deb3cd01a59eff3fc461855 Mon Sep 17 00:00:00 2001 From: Tom Walker Date: Sat, 21 Jun 2014 00:19:34 +0100 Subject: [PATCH] working on tests for quiz.views --- quiz/models.py | 7 +- quiz/tests.py | 113 ++++++++++++++++++++++--- quiz/views.py | 10 ++- templates/quiz/list_categories.html | 21 +++++ templates/quiz/view_quiz_category.html | 11 ++- 5 files changed, 143 insertions(+), 19 deletions(-) create mode 100644 templates/quiz/list_categories.html diff --git a/quiz/models.py b/quiz/models.py index b4cd73a..32ba608 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -378,6 +378,9 @@ class Sitting(models.Model): if divisor < 1: return 0 # prevent divide by zero error + if dividend > divisor: + return 100 + correct = int(round((dividend / divisor) * 100)) if correct >= 1: @@ -394,6 +397,8 @@ class Sitting(models.Model): Adds uid of incorrect question to the list. The question object must be passed in. """ + if isinstance(question, Question) == False: + return False current_incorrect = self.incorrect_questions question_id = question.id @@ -402,7 +407,7 @@ class Sitting(models.Model): def get_incorrect_questions(self): question_list = self.incorrect_questions - split_questions = question_list.split(',') + split_questions = filter(None, question_list.split(',')) return split_questions diff --git a/quiz/tests.py b/quiz/tests.py index 46c828c..25838e9 100644 --- a/quiz/tests.py +++ b/quiz/tests.py @@ -4,6 +4,8 @@ from django.contrib.auth.models import User from django.test import TestCase from quiz.models import Category, Quiz, Progress, Sitting, Question +from multichoice.models import MCQuestion +from true_false.models import TF_Question class TestCategory(TestCase): def setUp(self): @@ -142,25 +144,112 @@ class TestProgress(TestCase): class TestSitting(TestCase): def setUp(self): - q1 = Quiz.objects.create(id = 1, + quiz1 = Quiz.objects.create(id = 1, title = "test quiz 1", description = "d1", url = "tq1",) - Question.objects.create(id = 1, - quiz = q1, - content = "squawk",) + question1 = MCQuestion.objects.create(id = 1, + content = "squawk",) + question1.quiz.add(quiz1) - Question.objects.create(id = 2, - quiz = q1, - content = "squeek",) + question2 = MCQuestion.objects.create(id = 2, + content = "squeek",) + question2.quiz.add(quiz1) self.user = User.objects.create_user(username = "jacob", - email = "jacob@jacob.com", - password = "top_secret") + email = "jacob@jacob.com", + password = "top_secret") + + Sitting.objects.new_sitting(self.user, quiz1) + + def test_get_next_remove_first(self): + s1 = Sitting.objects.get(id = 1) + self.assertEqual(s1.get_next_question(), 1) - Sitting.objects.new_sitting(self.user, q1) + s1.remove_first_question() + self.assertEqual(s1.get_next_question(), 2) - def test_get_next(self): + s1.remove_first_question() + self.assertEqual(s1.get_next_question(), False) + + s1.remove_first_question() + self.assertEqual(s1.get_next_question(), False) + + def test_scoring(self): s1 = Sitting.objects.get(id = 1) - print s1.question_list + self.assertEqual(s1.get_current_score(), 0) + + s1.add_to_score(1) + self.assertEqual(s1.get_current_score(), 1) + self.assertEqual(s1.get_percent_correct(), 50) + + s1.add_to_score(1) + self.assertEqual(s1.get_current_score(), 2) + self.assertEqual(s1.get_percent_correct(), 100) + + s1.add_to_score(1) + self.assertEqual(s1.get_current_score(), 3) + self.assertEqual(s1.get_percent_correct(), 100) + + def test_incorrect_and_complete(self): + s1 = Sitting.objects.get(id = 1) + self.assertEqual(s1.get_incorrect_questions(), []) + + question1 = MCQuestion.objects.get(id = 1) + s1.add_incorrect_question(question1) + self.assertIn("1", s1.get_incorrect_questions()) + + question3 = TF_Question.objects.create(id = 3, + content = "oink",) + s1.add_incorrect_question(question3) + self.assertIn("3", s1.get_incorrect_questions()) + + quiz = Quiz.objects.get(id = 1) + f_test = s1.add_incorrect_question(quiz) + self.assertEqual(f_test, False) + self.assertNotIn("test", s1.get_incorrect_questions()) + + self.assertEqual(s1.complete, False) + s1.mark_quiz_complete() + self.assertEqual(s1.complete, True) + +""" +Tests relating to views +""" + +class TestNonQuestionViews(TestCase): + def setUp(self): + Category.objects.new_category(category = "elderberries") + c1 = Category.objects.get(id = 1) + Category.objects.new_category(category = "straw.berries") + Category.objects.new_category(category = "black berries") + + Quiz.objects.create(id = 1, + title = "test quiz 1", + description = "d1", + url = "tq1", + category = c1) + Quiz.objects.create(id = 2, + title = "test quiz 2", + description = "d2", + url = "t q2",) + + + def test_index(self): + response = self.client.get('/q/') + + self.assertContains(response, 'test quiz 1') + + def test_list_categories(self): + response = self.client.get('/q/category/') + + self.assertContains(response, 'elderberries') + self.assertContains(response, 'straw.berries') + self.assertContains(response, 'black-berries') + + def test_view_cat(self): + response = self.client.get('/q/category/elderberries/') + + self.assertContains(response, 'test quiz 1') + self.assertNotContains(response, 'test quiz 2') diff --git a/quiz/views.py b/quiz/views.py index cf7dff7..b853cc9 100644 --- a/quiz/views.py +++ b/quiz/views.py @@ -16,7 +16,7 @@ def index(request): def list_categories(request): - return render(request, 'quiz_index.html', + return render(request, 'list_categories.html', {'categories': Category.objects.all(),}) @@ -24,6 +24,7 @@ def view_category(request, slug): category = get_object_or_404(Category, category = slug.replace(' ', '-').lower()) quizzes = Quiz.objects.filter(category = category) + return render(request, 'view_quiz_category.html', {'category': category, 'quizzes': quizzes,}) @@ -309,11 +310,14 @@ def user_progress_score_update(request, category, score, possible): def anon_session_score(request, add = 0, possible = 0): """ Returns the session score for non-signed in users. - If number passed in then add this to the running total and then return session score + If number passed in then add this to the running total and + return session score + examples: x, y = anon_session_score(1, 1) will add 1 out of a possible 1 x, y = anon_session_score(0, 2) will add 0 out of a possible 2 - x, y = anon_session_score() will return the session score without modification + x, y = anon_session_score() will return the session score + without modification """ if "session_score" not in request.session: request.session["session_score"] = 0 diff --git a/templates/quiz/list_categories.html b/templates/quiz/list_categories.html new file mode 100644 index 0000000..1ae019b --- /dev/null +++ b/templates/quiz/list_categories.html @@ -0,0 +1,21 @@ +{% extends 'base.html' %} +{% block title %}All Quizzes{% endblock %} + +{% block content %} +

Category list

+ + + +{% endblock %} + +{% block right-sidebar %} +

Sponsored Advertisements

+{% endblock %} diff --git a/templates/quiz/view_quiz_category.html b/templates/quiz/view_quiz_category.html index e8e1ad1..b60a0c9 100644 --- a/templates/quiz/view_quiz_category.html +++ b/templates/quiz/view_quiz_category.html @@ -2,14 +2,19 @@ {% block title %}Quizzes related to {{ category.category }}{% endblock %} {% block content %} +

Quizzes in {{ category.category }}

+ {% if quizzes %} -

Quizzes in {{ category.category }}

{% else %} -

There are no quizzes.

+

There are no quizzes

{% endif %} {% endblock %} -- 2.39.5