]> git.parisson.com Git - django_quiz.git/commitdiff
working on tests for quiz.views
authorTom Walker <tomwalker0472@gmail.com>
Fri, 20 Jun 2014 23:19:34 +0000 (00:19 +0100)
committerTom Walker <tomwalker0472@gmail.com>
Fri, 20 Jun 2014 23:19:34 +0000 (00:19 +0100)
quiz/models.py
quiz/tests.py
quiz/views.py
templates/quiz/list_categories.html [new file with mode: 0644]
templates/quiz/view_quiz_category.html

index b4cd73a380aaff10e257ca6f93f38667b35853a5..32ba608efd2897a56d8ff188d15398ce78a98a02 100644 (file)
@@ -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
 
 
index 46c828c1db1fea5fcf06fa04e801a7e29e50e083..25838e92cbf4f6014190122ea98327b651a940f2 100644 (file)
@@ -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')
index cf7dff7b2e6ee4cd3b261e1d54186b24a3017807..b853cc981419695e6b151c099da1d1d24dcc4db6 100644 (file)
@@ -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 (file)
index 0000000..1ae019b
--- /dev/null
@@ -0,0 +1,21 @@
+{% extends 'base.html' %}
+{% block title %}All Quizzes{% endblock %}
+
+{% block content %}
+<h2>Category list</h2>
+
+<ul>
+  {% for cat in categories %}
+  <li>
+       <a href="{% url 'quiz.views.view_category' slug=cat.category %}">
+         {{ cat.category }}
+       </a>
+  </li>
+  {% endfor %}
+</ul>
+
+{% endblock %}
+
+{% block right-sidebar %}
+<h2>Sponsored Advertisements</h2>
+{% endblock %}
index e8e1ad17c3d92f75c90c3e894b17c20631c126f6..b60a0c9a94738762c3f1a292140879da90e21a5e 100644 (file)
@@ -2,14 +2,19 @@
 {% block title %}Quizzes related to {{ category.category }}{% endblock %}
 
 {% block content %}
+<h1>Quizzes in {{ category.category }}</h1>
+
     {% if quizzes %}
-    <h1>Quizzes in {{ category.category }}</h1>
         <ul>
         {% for quiz in quizzes %}
-            <li><a href="{{ BASE_URL }}/quiz/{{ quiz.url }}">{{ quiz.title }}</a></li>
+            <li>
+                         <a href="{% url 'quiz_question' quiz_name=quiz.url %}">
+                               {{ quiz.title }}
+                         </a>
+                       </li>
         {% endfor %}
         </ul>
     {% else %}
-        <p>There are no quizzes.</p>
+        <p>There are no quizzes</p>
     {% endif %}
 {% endblock %}