from __future__ import division __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. """ from django.test import TestCase import unittest from course.page.choice import markup_to_html_plain from tests.base_test_mixins import SingleCoursePageTestMixin from tests.test_sandbox import ( SingleCoursePageSandboxTestBaseMixin ) from tests.contants import PAGE_ERRORS from tests.utils import mock # The last item is within a pair of backticks # https://github.com/inducer/relate/issues/121 # Notice the text must wrapped by quotes CHOICE_MARKDOWN = """ type: ChoiceQuestion id: simple value: 1 shuffle: False prompt: | # Good What is good? choices: - Bad - Worse - ~CORRECT~ Well - "`So so`" """ CHOICE_MARKDOWN_WITHOUT_CORRECT_ANSWER = """ type: ChoiceQuestion id: simple value: 1 shuffle: False prompt: | # Good What is good? choices: - Bad - Worse - Well - So so """ CHOICE_MARKDOWN_WITH_DISREGARD = """ type: ChoiceQuestion id: simple value: 1 shuffle: False prompt: | # Good What is good? choices: - Bad - Worse - ~CORRECT~ Well - ~DISREGARD~ So so """ CHOICE_MARKDOWN_WITH_ALWAYS_CORRECT = """ type: ChoiceQuestion id: simple value: 1 shuffle: False prompt: | # Good What is good? choices: - Bad - Worse - ~CORRECT~ Well - ~ALWAYS_CORRECT~ So so """ CHOICE_MARKDOWN_WITH_ANSWER_EXPLANATION = """ type: ChoiceQuestion id: simple value: 1 shuffle: False prompt: | # Good What is good? choices: - Bad - Worse - ~CORRECT~ Well - So so answer_explanation: This is the explanation. """ MULTIPLE_CHOICES_MARKDWON_NORMAL_PATTERN = """ type: MultipleChoiceQuestion id: ice_cream_toppings %(credit_mode_str)s value: 1 shuffle: %(shuffle)s prompt: | # Ice Cream Toppings Which of the following are ice cream toppings? choices: - ~CORRECT~ Sprinkles - ~CORRECT~ Chocolate chunks - Vacuum cleaner dust - Spider webs - ~CORRECT~ Almond bits %(extra_attr)s """ # noqa MULTIPLE_CHOICES_MARKDWON_WITH_MULTIPLE_MODE1 = """ type: MultipleChoiceQuestion id: ice_cream_toppings credit_mode: exact value: 1 shuffle: False prompt: | # Ice Cream Toppings Which of the following are ice cream toppings? choices: - ~CORRECT~~CORRECT~ Sprinkles - ~CORRECT~ Chocolate chunks - Vacuum cleaner dust - Spider webs - ~CORRECT~ Almond bits """ MULTIPLE_CHOICES_MARKDWON_WITH_MULTIPLE_MODE2 = """ type: MultipleChoiceQuestion id: ice_cream_toppings credit_mode: exact value: 1 shuffle: False prompt: | # Ice Cream Toppings Which of the following are ice cream toppings? choices: - ~DISREGARD~~CORRECT~ Sprinkles - ~CORRECT~ Chocolate chunks - Vacuum cleaner dust - Spider webs - ~CORRECT~ Almond bits """ MULTIPLE_CHOICES_MARKDWON_WITH_DISREGARD_PATTERN = """ type: MultipleChoiceQuestion id: ice_cream_toppings credit_mode: %(credit_mode)s value: 1 shuffle: False prompt: | # Ice Cream Toppings Which of the following are ice cream toppings? choices: - ~CORRECT~ Sprinkles - ~CORRECT~ Chocolate chunks - Vacuum cleaner dust - Spider webs - ~CORRECT~ Almond bits - ~DISREGARD~ A flawed option """ # noqa MULTIPLE_CHOICES_MARKDWON_WITH_ALWAYS_CORRECT_PATTERN = """ type: MultipleChoiceQuestion id: ice_cream_toppings credit_mode: %(credit_mode)s value: 1 shuffle: False prompt: | # Ice Cream Toppings Which of the following are ice cream toppings? choices: - ~CORRECT~ Sprinkles - ~CORRECT~ Chocolate chunks - Vacuum cleaner dust - Spider webs - ~CORRECT~ Almond bits - ~ALWAYS_CORRECT~ A flawed option """ # noqa class ChoicesQuestionTest(SingleCoursePageSandboxTestBaseMixin, TestCase): def test_choice(self): markdown = CHOICE_MARKDOWN resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxHasValidPage(resp) self.assertEqual(len(self.get_sandbox_page_data()), 3) page_data = self.get_sandbox_page_data()[2] self.assertTrue("permutation" in page_data) resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={"choice": [1]}) self.assertEqual(resp.status_code, 200) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 0) resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={"choice": [2]}) self.assertEqual(resp.status_code, 200) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 1) def test_choice_without_correct(self): markdown = CHOICE_MARKDOWN_WITHOUT_CORRECT_ANSWER resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxNotHasValidPage(resp) self.assertResponseContextContains( resp, PAGE_ERRORS, "one or more correct answer(s) " "expected, 0 found") def test_choice_with_disregard(self): markdown = CHOICE_MARKDOWN_WITH_DISREGARD resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxNotHasValidPage(resp) self.assertResponseContextContains( resp, PAGE_ERRORS, "ChoiceQuestion does not allow any choices " "marked 'disregard'") def test_choice_with_always_correct(self): markdown = CHOICE_MARKDOWN_WITH_ALWAYS_CORRECT resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxNotHasValidPage(resp) self.assertResponseContextContains( resp, PAGE_ERRORS, "ChoiceQuestion does not allow any choices " "marked 'always_correct'") def test_choice_with_explanation(self): markdown = CHOICE_MARKDOWN_WITH_ANSWER_EXPLANATION resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxHasValidPage(resp) self.assertResponseContextContains(resp, "correct_answer", "This is the explanation.") class MultiChoicesQuestionTest(SingleCoursePageSandboxTestBaseMixin, TestCase): # {{{ choice with multiple modes def test_choice_item_with_multiple_modes1(self): resp = self.get_page_sandbox_preview_response( MULTIPLE_CHOICES_MARKDWON_WITH_MULTIPLE_MODE1) self.assertEqual(resp.status_code, 200) self.assertSandboxNotHasValidPage(resp) expected_page_error = ("ValidationError: sandbox, choice 1: " "more than one choice modes set: " "'~CORRECT~~CORRECT~'") self.assertResponseContextContains(resp, PAGE_ERRORS, expected_page_error) def test_choice_item_with_multiple_modes2(self): resp = self.get_page_sandbox_preview_response( MULTIPLE_CHOICES_MARKDWON_WITH_MULTIPLE_MODE2) self.assertEqual(resp.status_code, 200) self.assertSandboxNotHasValidPage(resp) expected_page_error = ("ValidationError: sandbox, choice 1: " "more than one choice modes set: " "'~DISREGARD~~CORRECT~'") self.assertResponseContextContains(resp, PAGE_ERRORS, expected_page_error) # }}} def test_shuffle(self): markdown = (MULTIPLE_CHOICES_MARKDWON_NORMAL_PATTERN % {"shuffle": "True", "credit_mode_str": "credit_mode: exact", "extra_attr": ""}) resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxHasValidPage(resp) self.assertEqual(len(self.get_sandbox_page_data()), 3) page_data = self.get_sandbox_page_data()[2] self.assertTrue("permutation" in page_data) permutation = page_data["permutation"] unpermed_correct_answer_idx = [0, 1, 4] correct_idx = [] for idx in unpermed_correct_answer_idx: correct_idx.append(str(permutation.index(idx))) resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={"choice": correct_idx}) self.assertEqual(resp.status_code, 200) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 1) def test_exact_mode(self): markdown = (MULTIPLE_CHOICES_MARKDWON_NORMAL_PATTERN % {"shuffle": "False", "credit_mode_str": "credit_mode: exact", "extra_attr": ""}) resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxHasValidPage(resp) self.assertEqual(len(self.get_sandbox_page_data()), 3) # This is to make sure page_data exists and is ordered page_data = self.get_sandbox_page_data()[2] self.assertTrue("permutation" in page_data) permutation = page_data["permutation"] self.assertEqual(permutation, sorted(permutation)) resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={"choice": ['0', '1', '4']}) self.assertEqual(resp.status_code, 200) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 1) resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={"choice": ['0']}) self.assertEqual(resp.status_code, 200) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 0) def test_proportional_mode(self): markdown = (MULTIPLE_CHOICES_MARKDWON_NORMAL_PATTERN % {"shuffle": "False", "credit_mode_str": "credit_mode: proportional", "extra_attr": ""}) resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxHasValidPage(resp) self.assertEqual(len(self.get_sandbox_page_data()), 3) resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={"choice": ['0', '1', '4']}) self.assertEqual(resp.status_code, 200) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 1) resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={"choice": ['0', '1', '2', '3', '4']}) self.assertEqual(resp.status_code, 200) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 0.6) # {{{ choices with disregard or always_correct tag def test_choice_item_with_disregard(self): markdown = (MULTIPLE_CHOICES_MARKDWON_WITH_DISREGARD_PATTERN % {"credit_mode": "proportional_correct"}) resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxHasValidPage(resp) resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={"choice": ['2', '5']}) self.assertEqual(resp.status_code, 200) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 0) resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={"choice": ['0', '1']}) self.assertEqual(resp.status_code, 200) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 2/3) resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={"choice": ['0', '1', '5']}) self.assertEqual(resp.status_code, 200) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 2/3) def test_choice_item_with_always_correct(self): markdown = (MULTIPLE_CHOICES_MARKDWON_WITH_ALWAYS_CORRECT_PATTERN % {"credit_mode": "proportional_correct"}) resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxHasValidPage(resp) resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={"choice": ['2', '5']}) self.assertEqual(resp.status_code, 200) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 0) # Note that the feedback correctness is different from when the "flawed" # option is tagged "~DISREGARD~" resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={"choice": ['0', '1']}) self.assertEqual(resp.status_code, 200) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 0.75) resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={"choice": ['0', '1', '5']}) self.assertEqual(resp.status_code, 200) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 0.75) self.assertResponseContextEqual( resp, "correct_answer", "The correct answer is: " "
%s
" % y fake_page_context = object self.assertEqual( markup_to_html_plain(fake_page_context, "abcd"), "abcd") self.assertEqual(markup_to_html_plain(fake_page_context, ""), "") def test_markup_to_html_plain_wrapp_by_p_other_tag(self): with mock.patch("course.page.choice.markup_to_html") as mock_mth: mock_mth.side_effect = lambda x, y: "