Skip to content
test_code.py 48.4 KiB
Newer Older
from __future__ import annotations


Dong Zhuang's avatar
Dong Zhuang committed
__copyright__ = "Copyright (C) 2018 Dong Zhuang"

__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
import unittest
from socket import error as socket_error
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.test import Client, RequestFactory, TestCase, override_settings
Andreas Klöckner's avatar
Andreas Klöckner committed
from course.constants import MAX_EXTRA_CREDIT_FACTOR
from course.models import FlowSession
Dong Zhuang's avatar
Dong Zhuang committed
from course.page.code import (
    PythonCodeQuestionWithHumanTextFeedback,
    is_nuisance_failure,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from course.utils import CoursePageContext, FlowPageContext
Dong Zhuang's avatar
Dong Zhuang committed
from tests.base_test_mixins import (
    MockAddMessageMixing,
    SingleCoursePageTestMixin,
    SingleCourseQuizPageTestMixin,
Andreas Klöckner's avatar
Andreas Klöckner committed
    SubprocessRunpyContainerMixin,
Andreas Klöckner's avatar
Andreas Klöckner committed
from tests.constants import MESSAGE_ANSWER_SAVED_TEXT, PAGE_ERRORS
from tests.test_sandbox import SingleCoursePageSandboxTestBaseMixin
from tests.utils import LocmemBackendTestsMixin, mail, mock
Dong Zhuang's avatar
Dong Zhuang committed

from . import markdowns

Andreas Klöckner's avatar
Andreas Klöckner committed

Dong Zhuang's avatar
Dong Zhuang committed
NO_CORRECTNESS_INFO_MSG = "No information on correctness of answer."

NOT_ALLOW_MULTIPLE_SUBMISSION_WARNING = (
    "code question does not explicitly "
    "allow multiple submission. Either add "
    "access_rules/add_permissions/change_answer "
Dong Zhuang's avatar
Dong Zhuang committed
    "or add 'single_submission: True' to confirm that you intend "
    "for only a single submission to be allowed. "
    "While you're at it, consider adding "
    "access_rules/add_permissions/see_correctness."
MAX_AUTO_FEEDBACK_POINTS_VALICATION_ERROR_MSG_PATTERN = (
Dong Zhuang's avatar
Dong Zhuang committed
    "'max_auto_feedback_points' is invalid: expecting "
    "a value within [0, %(max_extra_credit_factor)s], "
    "got %(invalid_value)s."
)

GRADE_CODE_FAILING_MSG = (
    "The grading code failed. Sorry about that."
)

Neal Davis's avatar
Neal Davis committed
RUNCODE_WITH_RETRIES_PATH = "course.page.code.request_run_with_retries"
AUTO_FEEDBACK_POINTS_OUT_OF_RANGE_ERROR_MSG_PATTERN = (
    "'correctness' is invalid: expecting "
    "a value within [0, %s] or None, "
    "got %s."
)
class SingleCourseQuizPageCodeQuestionTest(
Dong Zhuang's avatar
Dong Zhuang committed
            SingleCourseQuizPageTestMixin, MockAddMessageMixing,
            SubprocessRunpyContainerMixin, TestCase):
Dong Zhuang's avatar
Dong Zhuang committed
    skip_code_question = False
    def setUpTestData(cls):
        super().setUpTestData()

        client = Client()
        client.force_login(cls.student_participation.user)

        cls.start_flow(client, cls.flow_id)
    def test_code_page_correct(self):
        page_id = "addition"
Dong Zhuang's avatar
Dong Zhuang committed
        submit_answer_response, post_grade_response = (
            self.default_submit_page_answer_by_page_id_and_test(page_id))
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertAddMessageCalledWith(MESSAGE_ANSWER_SAVED_TEXT)
    def test_code_page_wrong(self):
        page_id = "addition"
Dong Zhuang's avatar
Dong Zhuang committed
        submit_answer_response, post_grade_response = (
            self.default_submit_page_answer_by_page_id_and_test(
                page_id, answer_data={"answer": "c = a - b\r"},
Dong Zhuang's avatar
Dong Zhuang committed
                expected_grade=0))
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertAddMessageCalledWith(MESSAGE_ANSWER_SAVED_TEXT)
    def test_code_page_identical_to_reference(self):
        page_id = "addition"
Dong Zhuang's avatar
Dong Zhuang committed
        submit_answer_response, post_grade_response = (
            self.default_submit_page_answer_by_page_id_and_test(
                page_id, answer_data={"answer": "c = a + b\r"},
Dong Zhuang's avatar
Dong Zhuang committed
                expected_grade=1))
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertAddMessageCalledWith(MESSAGE_ANSWER_SAVED_TEXT)

        self.assertResponseContextAnswerFeedbackContainsFeedback(
Dong Zhuang's avatar
Dong Zhuang committed
            submit_answer_response,
            ("It looks like you submitted code "
             "that is identical to the reference "
             "solution. This is not allowed."))

    def test_code_human_feedback_page_submit(self):
        page_id = "pymult"
Dong Zhuang's avatar
Dong Zhuang committed
        submit_answer_response, post_grade_response = (
            self.default_submit_page_answer_by_page_id_and_test(page_id))
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertAddMessageCalledWith(MESSAGE_ANSWER_SAVED_TEXT)

    def test_code_human_feedback_page_grade1(self):
        page_id = "pymult"

Dong Zhuang's avatar
Dong Zhuang committed
        submit_answer_response, post_grade_response = (
            self.default_submit_page_answer_by_page_id_and_test(
                page_id, answer_data={"answer": "c = b * a\r"},
                expected_grade=4))

        self.assertResponseContextAnswerFeedbackContainsFeedback(
Dong Zhuang's avatar
Dong Zhuang committed
            post_grade_response, "The human grader assigned 2/2 points.")
        self.assertSessionScoreEqual(4)

    def test_code_human_feedback_page_grade2(self):
        page_id = "pymult"

        feedback_text = "This is the feedback from instructor."
Dong Zhuang's avatar
Dong Zhuang committed
        submit_answer_response, post_grade_response = (
            self.default_submit_page_answer_by_page_id_and_test(
                page_id, answer_data={"answer": "c = a / b\r"},
Dong Zhuang's avatar
Dong Zhuang committed
                grade_data_extra_kwargs={"feedback_text": feedback_text},
                expected_grade=2))

        self.assertResponseContextAnswerFeedbackContainsFeedback(
Dong Zhuang's avatar
Dong Zhuang committed
            submit_answer_response, "'c' is inaccurate")

        self.assertResponseContextAnswerFeedbackContainsFeedback(
Dong Zhuang's avatar
Dong Zhuang committed
            submit_answer_response, "The autograder assigned 0/2 points.")

        self.assertResponseContextAnswerFeedbackContainsFeedback(
            post_grade_response, "The human grader assigned 2/2 points.")

        self.assertResponseContextAnswerFeedbackContainsFeedback(
            post_grade_response, "The human grader assigned 2/2 points.")

    def test_code_human_feedback_page_grade3(self):
        page_id = "py_simple_list"
Dong Zhuang's avatar
Dong Zhuang committed

        submit_answer_response, post_grade_response = (
            self.default_submit_page_answer_by_page_id_and_test(
                page_id, answer_data={"answer": "b = [a + 1] * 50\r"},
Dong Zhuang's avatar
Dong Zhuang committed
                do_grading=False))

        # this is testing feedback.finish(0.3, feedback_msg)
        # 2 * 0.3 = 0.6
        self.assertResponseContextAnswerFeedbackContainsFeedback(
Dong Zhuang's avatar
Dong Zhuang committed
            submit_answer_response, "The autograder assigned 0.90/3 points.")
        self.assertResponseContextAnswerFeedbackContainsFeedback(
Dong Zhuang's avatar
Dong Zhuang committed
            submit_answer_response, "The elements in b have wrong values")

    def test_code_human_feedback_page_grade4(self):
        page_id = "py_simple_list"
Dong Zhuang's avatar
Dong Zhuang committed
        submit_answer_response, post_grade_response = (
            self.default_submit_page_answer_by_page_id_and_test(page_id))
        self.assertResponseContextAnswerFeedbackContainsFeedback(
Dong Zhuang's avatar
Dong Zhuang committed
            submit_answer_response, "b looks good")
        self.assertResponseContextAnswerFeedbackContainsFeedback(
Dong Zhuang's avatar
Dong Zhuang committed
            post_grade_response, "The human grader assigned 1/1 points.")
Dong Zhuang's avatar
Dong Zhuang committed
            "grade_percent": "",
            "released": "on"
Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.submit_page_human_grading_by_page_id_and_test(
            page_id, grade_data=grade_data, expected_grades=None)

        self.assertFormErrorLoose(resp, None)

        # not released
        feedback_text = "This is the feedback from instructor."
        grade_data = {
Loading
Loading full blame...