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 from tests.test_sandbox import ( SingleCoursePageSandboxTestBaseMixin, PAGE_ERRORS ) from tests.utils import mock INLINE_MULTI_MARKDOWN_SINGLE = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[blank1]] are often used in code examples. answers: blank1: type: ShortAnswer width: 4em required: False hint: Tex can be rendered in hint, e.g. $x_1$. hint_title: Hint correct_answer: - BAR - bar """ INLINE_MULTI_MARKDOWN_TWO_NOT_REQUIRED = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[blank1]] are often used in code examples. A quarter equals [[choice1]]. answers: blank1: type: ShortAnswer width: 4em required: False hint: Tex can be rendered in hint, e.g. $x_1$. hint_title: Hint correct_answer: - (?:bar)?\s+ - BAR - bar choice1: type: ChoicesAnswer choices: - 0.2 - 1/6 - ~CORRECT~ 0.25 -

This_should_be_wrapped_by_p_tag

- [0.25] """ INLINE_MULTI_MARKDOWN_EMBEDDED_ATTR_PATTERN = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[blank1]] are often used in code examples. One dollar is [[blank2]]. answers: blank1: type: ShortAnswer %(attr1)s correct_answer: - BAR - bar blank2: type: ShortAnswer %(attr2)s correct_answer: - type: float rtol: 0.00001 value: 1 - one """ INLINE_MULTI_MARKDOWN_FLOAT_WITHOUT_TOL = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[blank1]] are often used in code examples. One dollar is [[blank2]]. answers: blank1: type: ShortAnswer width: 4em required: False hint: Tex can be rendered in hint, e.g. $x_1$. hint_title: Hint correct_answer: - BAR - bar blank2: type: ShortAnswer width: 3em prepended_text: "$" hint: Blank with prepended text correct_answer: - type: float value: 1 """ INLINE_MULTI_MARKDOWN_NOT_ALLOWED_EMBEDDED_QTYPE = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[blank1]] are often used in code examples. answers: blank1: type: SomeQuestionType width: 4em required: False hint: Tex can be rendered in hint, e.g. $x_1$. hint_title: Hint correct_answer: - BAR - bar """ INLINE_MULTI_MARKDOWN_EMBEDDED_QUESTION_NOT_STRUCT = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[blank1]] are often used in code examples. answers: blank1: Something """ INLINE_MULTI_MARKDOWN_EMBEDDED_HAS_NO_EXTRA_HTML = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | [[blank1]][[blank2]] answers: blank1: type: ShortAnswer correct_answer: - BAR - bar blank2: type: ShortAnswer correct_answer: - BAR - bar """ INLINE_MULTI_MARKDOWN_EMBEDDED_NO_CORRECT_ANSWER = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[blank1]] are often used in code examples. answers: blank1: type: ShortAnswer correct_answer: [] """ INLINE_MULTI_MARKDOWN_EMBEDDED_TEXT_Q_NO_STRINGIFIABLE_CORRECT_ANSWER = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[blank1]] are often used in code examples. answers: blank1: type: ShortAnswer correct_answer: - (?:foo\s+)?\s """ INLINE_MULTI_MARKDOWN_EMBEDDED_CHOICE_Q_NO_CORRECT_ANSWER = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[choice]] are often used in code examples. answers: choice: type: ChoicesAnswer choices: - 0.2 - 1/6 - 0.25 """ INLINE_MULTI_MARKDOWN_EMBEDDED_CHOICE_QUESTION = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[choice]] are often used in code examples. answers: choice: type: ChoicesAnswer choices: - 0.2 - 1/6 - ~CORRECT~ 0.25 """ INLINE_MULTI_MARKDOWN_EMBEDDED_NAMING_ERROR = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[blank1]] are often used in code examples. A quarter equals [[1choice]]. answers: blank1: type: ShortAnswer width: 4em required: False hint: Tex can be rendered in hint, e.g. $x_1$. hint_title: Hint correct_answer: - (?:bar)?\s+ - BAR - bar choice: type: ChoicesAnswer choices: - 0.2 - 1/6 - ~CORRECT~ 0.25 -

This_should_be_wrapped_by_p_tag

- [0.25] """ INLINE_MULTI_MARKDOWN_ANSWERS_NAMING_ERROR = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[blank1]] are often used in code examples. A quarter equals [[choice1]]. answers: blank1: type: ShortAnswer width: 4em required: False hint: Tex can be rendered in hint, e.g. $x_1$. hint_title: Hint correct_answer: - (?:bar)?\s+ - BAR - bar choice1: type: ChoicesAnswer choices: - 0.2 - 1/6 - ~CORRECT~ 0.25 -

This_should_be_wrapped_by_p_tag

- [0.25] 2choice: type: ChoicesAnswer choices: - 0.2 - 1/6 - ~CORRECT~ 0.25 -

This_should_be_wrapped_by_p_tag

- [0.25] """ INLINE_MULTI_MARKDOWN_EMBEDDED_NAMING_DUPLICATED = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[blank1]][[blank1]] are often used in code examples. A quarter equals [[choice1]][[choice1]]. answers: blank1: type: ShortAnswer width: 4em required: False hint: Tex can be rendered in hint, e.g. $x_1$. hint_title: Hint correct_answer: - (?:bar)?\s+ - BAR - bar choice1: type: ChoicesAnswer choices: - 0.2 - 1/6 - ~CORRECT~ 0.25 """ INLINE_MULTI_MARKDOWN_REDUNDANT = """ type: InlineMultiQuestion id: inlinemulti value: 10 answer_explanation: This is an explanation. prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[blank1]] are often used in code examples. answers: blank1: type: ShortAnswer width: 4em required: True hint: Tex can be rendered in hint, e.g. $x_1$. hint_title: Hint correct_answer: - BAR - bar blank_2: type: ShortAnswer width: 10em hint:
  1. with no hint title
  2. HTML is OK
    1. correct_answer: - "1/5" - type: float value: 1/5 rtol: 0.00001 - 0.2 """ INLINE_MULTI_EMBEDDED_WITH_MARKDOWN = """ type: InlineMultiQuestion id: inlinemulti value: 10 answer_explanation: This is an explanation. prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | Foo and [[blank1]] are often used in code examples. answers: blank1: type: ShortAnswer width: 4em required: True hint: Tex can be rendered in hint, e.g. $x_1$. hint_title: Hint correct_answer: - BAR - bar """ INLINE_MULTI_MARKDOWN_NO_ANSWER_FIELD = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | abcd answers: blank1: type: ShortAnswer width: 4em required: True hint: Tex can be rendered in hint, e.g. $x_1$. hint_title: Hint correct_answer: - BAR - bar """ INLINE_MULTI_MARKDOWN_HAS_UNPAIRED_WRAPPER = """ type: InlineMultiQuestion id: inlinemulti value: 10 prompt: | # An InlineMultiQuestion example Complete the following paragraph. question: | [[[[blank1]]]] answers: blank1: type: ShortAnswer width: 4em required: True hint: Tex can be rendered in hint, e.g. $x_1$. hint_title: Hint correct_answer: - BAR - bar """ class InlineMultiQuestionTest(SingleCoursePageSandboxTestBaseMixin, TestCase): def test_single(self): markdown = INLINE_MULTI_MARKDOWN_SINGLE resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxHasValidPage(resp) self.assertSandboxWarningTextContain(resp, None) # When there's more than one field, that field is force_required. resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={}) self.assertEqual(resp.status_code, 200) self.assertFormErrorLoose(resp, "This field is required.") def test_two_not_required(self): markdown = INLINE_MULTI_MARKDOWN_TWO_NOT_REQUIRED resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxHasValidPage(resp) self.assertSandboxWarningTextContain(resp, None) # because this choice was wrapped by p tag before markdown handling self.assertContains( resp, "

      This_should_be_wrapped_by_p_tag

      ", html=True) self.assertContains(resp, "[0.25]") # When there's more than one fields, can submit with no answer resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={}) self.assertEqual(resp.status_code, 200) self.assertFormErrorLoose(resp, None) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 0) # partial answer resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={'blank1': ['Bar']}) self.assertEqual(resp.status_code, 200) self.assertFormErrorLoose(resp, None) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 0.5) # full answer, choice wrong answer resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={'blank1': 'Bar', 'choice1': 4}) self.assertEqual(resp.status_code, 200) self.assertFormErrorLoose(resp, None) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 0.5) # full answer, all correct resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={'blank1': 'Bar', 'choice1': 2}) self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 1) def test_submit_validation_error(self): markdown = INLINE_MULTI_MARKDOWN_FLOAT_WITHOUT_TOL resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxHasValidPage(resp) self.assertSandboxWarningTextContain( resp, "Float match should have either rtol or " "atol--otherwise it will match any number") resp = self.get_page_sandbox_submit_answer_response( markdown, answer_data={'blank1': 'Bar', 'blank2': 'abc'}) self.assertEqual(resp.status_code, 200) self.assertFormErrorLoose( resp, "TypeError: can't convert expression to float") def test_not_allowed_embedded_question_type(self): markdown = INLINE_MULTI_MARKDOWN_NOT_ALLOWED_EMBEDDED_QTYPE resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxNotHasValidPage(resp) self.assertResponseContextContains( resp, PAGE_ERRORS, "unknown embedded question type 'SomeQuestionType'") def test_embedded_question_not_struct(self): markdown = INLINE_MULTI_MARKDOWN_EMBEDDED_QUESTION_NOT_STRUCT resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxNotHasValidPage(resp) self.assertResponseContextContains( resp, PAGE_ERRORS, "Embedded question 'blank1' must be a struct") def test_embedded_question_no_extra_html(self): markdown = INLINE_MULTI_MARKDOWN_EMBEDDED_HAS_NO_EXTRA_HTML resp = self.get_page_sandbox_preview_response(markdown) self.assertEqual(resp.status_code, 200) self.assertSandboxHasValidPage(resp) # There's no html string between rendered blank1 field and blank2 field self.assertIn('
      ', html=True) # vim: fdm=marker