Skip to content
test_pages.py 5.65 KiB
Newer Older
Andreas Klöckner's avatar
Andreas Klöckner committed
from __future__ import division

Dong Zhuang's avatar
Dong Zhuang committed
__copyright__ = "Copyright (C) 2014 Andreas Kloeckner, Zesheng Wang, Dong Zhuang"
Andreas Klöckner's avatar
Andreas Klöckner committed

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

Dong Zhuang's avatar
Dong Zhuang committed
from django.test import TestCase
Dong Zhuang's avatar
Dong Zhuang committed
from django.urls import reverse
Dong Zhuang's avatar
Dong Zhuang committed
from django.contrib.auth import get_user_model
Dong Zhuang's avatar
Dong Zhuang committed
from course.models import FlowPageVisit, Course
from .base_test_mixins import SingleCoursePageTestMixin
Dong Zhuang's avatar
Dong Zhuang committed
QUIZ_FLOW_ID = "quiz-test"
Dong Zhuang's avatar
Dong Zhuang committed

class SingleCourseQuizPageTest(SingleCoursePageTestMixin, TestCase):
    flow_id = QUIZ_FLOW_ID
zwang180's avatar
zwang180 committed

Dong Zhuang's avatar
Dong Zhuang committed
    # TODO: This should be moved to tests for auth module
zwang180's avatar
zwang180 committed
    def test_user_creation(self):
Dong Zhuang's avatar
Dong Zhuang committed
        # Should have 4 users
        self.assertEqual(get_user_model().objects.all().count(), 4)
        self.c.logout()

        self.assertTrue(
            self.c.login(
                username=self.instructor_participation.user.username,
                password=(
                    self.courses_setup_list[0]
                    ["participations"][0]
                    ["user"]["password"])))

    # TODO: This should move to tests for course.view module
zwang180's avatar
zwang180 committed
    def test_course_creation(self):
        # Should only have one course
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(Course.objects.all().count(), 1)
        resp = self.c.get(reverse("relate-course_page",
                                  args=[self.course.identifier]))
zwang180's avatar
zwang180 committed
        # 200 != 302 is better than False is not True
        self.assertEqual(resp.status_code, 200)

    def test_quiz_no_answer(self):
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(0)
Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.client_post_answer_by_ordinal(1, {"answer": ['0.5']})
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(5)
    def test_quiz_choice(self):
Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.client_post_answer_by_ordinal(2, {"choice": ['0']})
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(2)
zwang180's avatar
zwang180 committed
    def test_quiz_multi_choice_exact_correct(self):
Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.client_post_answer_by_ordinal(3, {"choice": ['0', '1', '4']})
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(1)
zwang180's avatar
zwang180 committed
    def test_quiz_multi_choice_exact_wrong(self):
Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.client_post_answer_by_ordinal(3, {"choice": ['0', '1']})
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(0)
Dong Zhuang's avatar
Dong Zhuang committed
    def test_quiz_multi_choice_proportion_partial(self):
        resp = self.client_post_answer_by_ordinal(4, {"choice": ['0']})
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(0.8)
Dong Zhuang's avatar
Dong Zhuang committed
    def test_quiz_multi_choice_proportion_correct(self):
        resp = self.client_post_answer_by_ordinal(4, {"choice": ['0', '3']})
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(1)
zwang180's avatar
zwang180 committed
    def test_quiz_inline(self):
Dong Zhuang's avatar
Dong Zhuang committed
        answer_data = {
            'blank1': ['Bar'], 'blank_2': ['0.2'], 'blank3': ['1'],
            'blank4': ['5'], 'blank5': ['Bar'], 'choice2': ['0'],
            'choice_a': ['0']}
        resp = self.client_post_answer_by_ordinal(5, answer_data)
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(10)
    # All I can do for now since db do not store ordinal value
    def test_quiz_survey_choice(self):
Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.client_post_answer_by_ordinal(6, {"answer": ["NOTHING!!!"]})
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(0)

        query = FlowPageVisit.objects.filter(
Dong Zhuang's avatar
Dong Zhuang committed
            flow_session__exact=self.page_params["flow_session_id"],
            answer__isnull=False)
        self.assertEqual(query.count(), 1)
        record = query[0]
        self.assertEqual(record.answer["answer"], "NOTHING!!!")

    def test_quiz_survey_text(self):
Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.client_post_answer_by_ordinal(7, {"choice": ['8']})
        self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(self.end_quiz().status_code, 200)
        self.assertSessionScoreEqual(0)

        query = FlowPageVisit.objects.filter(
Dong Zhuang's avatar
Dong Zhuang committed
                            flow_session__exact=self.page_params["flow_session_id"],
zwang180's avatar
zwang180 committed
                            answer__isnull=False)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(query.count(), 1)
        record = query[0]
        self.assertEqual(record.answer["choice"], 8)