Skip to content
test_generic.py 41.1 KiB
Newer Older
# -*- coding: utf-8 -*-

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.
"""

import six
Dong Zhuang's avatar
Dong Zhuang committed
import os
from base64 import b64encode
from collections import namedtuple

import unittest
Dong Zhuang's avatar
Dong Zhuang committed
from django.test import TestCase
from django.urls import resolve
from course.models import FlowSession
from course.constants import MAX_EXTRA_CREDIT_FACTOR
from course.page.base import (
    AnswerFeedback, get_auto_feedback,
    validate_point_count, InvalidFeedbackPointsError)
Dong Zhuang's avatar
Dong Zhuang committed
from tests.base_test_mixins import (
Dong Zhuang's avatar
Dong Zhuang committed
    SingleCoursePageTestMixin, FallBackStorageMessageTestMixin,
from tests.utils import mock
from tests import factories
Dong Zhuang's avatar
Dong Zhuang committed
QUIZ_FLOW_ID = "quiz-test"
Dong Zhuang's avatar
Dong Zhuang committed
MESSAGE_ANSWER_SAVED_TEXT = "Answer saved."
MESSAGE_ANSWER_FAILED_SAVE_TEXT = "Failed to submit answer."
FIXTURE_PATH = os.path.join(os.path.dirname(__file__), "..", 'fixtures')
def get_upload_file_path(file_name, fixture_path=FIXTURE_PATH):
    return os.path.join(fixture_path, file_name)


TEST_TEXT_FILE_PATH = get_upload_file_path("test_file.txt")
TEST_PDF_FILE_PATH = get_upload_file_path("test_file.pdf")

TEST_HGTEXT_MARKDOWN_ANSWER = u"""
type: ChoiceQuestion
id: myquestion
shuffle: True
prompt: |
    # What is a quarter?
  - "1"
  - "2"
  - ~CORRECT~ 1/4
  - ~CORRECT~ $\\frac{1}{4}$
  - 四分之三
"""
TEST_HGTEXT_MARKDOWN_ANSWER_WRONG = u"""
type: ChoiceQuestion
id: myquestion
shuffle: True
prompt: |
    # What is a quarter?
  - "1"
  - "2"
  - 1/4
  - $\\frac{1}{4}$
  - 四分之三
"""

PageTuple = namedtuple(
    'PageTuple', [
        'page_id',
        'group_id',
        'need_human_grade',
        'expecting_grade',
        'need_runpy',
        'correct_answer',
        'grade_data',
        'full_points',
    ]
)

TEST_AUDIO_OUTPUT_ANSWER = """
import numpy as np
t = np.linspace(0, 1, sample_rate, endpoint=False)
signal = np.sin(2*np.pi*t * 440)

output_audio(signal)
"""

TEST_PAGE_TUPLE = (
    PageTuple("welcome", "intro", False, False, False, None, {}, None),
    PageTuple("half", "quiz_start", False, True, False, {"answer": '0.5'}, {}, 5),
    PageTuple("krylov", "quiz_start", False, True, False, {"choice": ['0']}, {}, 2),
    PageTuple("ice_cream_toppings", "quiz_start", False, True, False,
              {"choice": ['0', '1', '4']}, {}, 1),
    PageTuple("matrix_props", "quiz_start", False, True, False,
              {"choice": ['0', '3']}, {}, 1),
    PageTuple("inlinemulti", "quiz_start", False, True, False,
              {'blank1': 'Bar', 'blank_2': '0.2', 'blank3': '1',
               'blank4': '5', 'blank5': 'Bar', 'choice2': '0',
               'choice_a': '0'}, {}, 10),
    PageTuple("fear", "quiz_start", True, False, False, {"answer": "NOTHING!!!"},
              {}, 0),
    PageTuple("age_group", "quiz_start", True, False, False, {"choice": 3}, {}, 0),
    PageTuple("hgtext", "quiz_tail", True, True, False,
              {"answer": TEST_HGTEXT_MARKDOWN_ANSWER},
              {"grade_percent": "100", "released": "on"}, 5),
    PageTuple("addition", "quiz_tail", False, True, True, {"answer": 'c = b + a\r'},
              {"grade_percent": "100", "released": "on"}, 1),
    PageTuple("pymult", "quiz_tail", True, True, True, {"answer": 'c = a * b\r'},
              {"grade_percent": "100", "released": "on"}, None),
    PageTuple("neumann", "quiz_tail", False, True, False, {"answer": "1/(1-A)"}, {},
              5),
    PageTuple("py_simple_list", "quiz_tail", True, True, True,
              {"answer": 'b = [a] * 50\r'},
              {"grade_percent": "100", "released": "on"}, 4),

    # Skipped
    # PageTuple("test_audio_output", "quiz_tail", True, True, True,
    #           {"answer": TEST_AUDIO_OUTPUT_ANSWER}, {}, 1),

    PageTuple("quarter", "quiz_tail", False, True, False, {"answer": ['0.25']},
              {}, 0),
    PageTuple("anyup", "quiz_tail", True, False, False, TEST_TEXT_FILE_PATH,
              {"grade_percent": "100", "released": "on"}, 5),
    PageTuple("proof", "quiz_tail", True, False, False, TEST_PDF_FILE_PATH,
              {"grade_percent": "100", "released": "on"}, 5),
    PageTuple("eigvec", "quiz_tail", False, True, False, {"answer": 'matrix'}, {},
              2),
    PageTuple("lsq", "quiz_tail", False, True, False, {"choice": ['2']}, {}, 1),
)


class SingleCourseQuizPageTestMixin(SingleCoursePageTestMixin,
                                    FallBackStorageMessageTestMixin):
    flow_id = QUIZ_FLOW_ID

    skip_code_question = True

    @classmethod
    def ensure_grading_ui_get(cls, page_id):
        with cls.temporarily_switch_to_user(cls.instructor_participation.user):
            url = cls.get_page_grading_url_by_page_id(page_id)
            resp = cls.c.get(url)
            assert resp.status_code == 200
    @classmethod
    def ensure_analytic_page_get(cls, group_id, page_id):
        with cls.temporarily_switch_to_user(cls.instructor_participation.user):
            resp = cls.get_flow_page_analytics(
                flow_id=cls.flow_id, group_id=group_id,
            assert resp.status_code == 200
    @classmethod
    def ensure_download_submission(cls, group_id, page_id):
        with cls.temporarily_switch_to_user(cls.instructor_participation.user):
            group_page_id = "%s/%s" % (group_id, page_id)
            resp = cls.post_download_all_submissions_by_group_page_id(
                group_page_id=group_page_id, flow_id=cls.flow_id)
            assert resp.status_code == 200
            prefix, zip_file = resp["Content-Disposition"].split('=')
            assert prefix == "attachment; filename"
            assert resp.get('Content-Type') == "application/zip"
    def submit_page_answer_by_ordinal_and_test(
            cls, page_ordinal, use_correct_answer=True, answer_data=None,
            skip_code_question=True,
            expected_grade=None, expected_post_answer_status_code=200,
            do_grading=False, do_human_grade=False, grade_data=None,
            ensure_grading_ui_get_before_grading=False,
            ensure_grading_ui_get_after_grading=False,
Loading
Loading full blame...