Skip to content
test_inline.py 32 KiB
Newer Older
Dong Zhuang's avatar
Dong Zhuang committed
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
Dong Zhuang's avatar
Dong Zhuang committed
import unittest
Dong Zhuang's avatar
Dong Zhuang committed
from course.content import get_repo_blob
from course.flow import get_page_behavior

Dong Zhuang's avatar
Dong Zhuang committed
from tests.base_test_mixins import SingleCourseQuizPageTestMixin
Dong Zhuang's avatar
Dong Zhuang committed
from tests.test_sandbox import (
Dong Zhuang's avatar
Dong Zhuang committed
    SingleCoursePageSandboxTestBaseMixin
Dong Zhuang's avatar
Dong Zhuang committed
from tests.constants import PAGE_ERRORS
Dong Zhuang's avatar
Dong Zhuang committed
from tests.utils import mock, may_run_expensive_tests, SKIP_EXPENSIVE_TESTS_REASON
Dong Zhuang's avatar
Dong Zhuang committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

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:
        - <plain> BAR
        - <plain>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:
        - <regex>(?:bar)?\s+
        - <plain> BAR
        - <plain>bar

    choice1:
        type: ChoicesAnswer
        choices:
        - 0.2
        - 1/6
        - ~CORRECT~ 0.25
        - <div><p>This_should_be_wrapped_by_p_tag</p></div>
        - [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:
        - <plain> BAR
        - <plain>bar

    blank2:
        type: ShortAnswer
        %(attr2)s
        correct_answer:
        - type: float
          rtol: 0.00001
          value: 1
        - <plain> 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:
        - <plain> BAR
        - <plain>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:
        - <plain> BAR
        - <plain>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:
        - <plain> BAR
        - <plain>bar

    blank2:
        type: ShortAnswer
        correct_answer:
        - <plain> BAR
        - <plain>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:
        - <regex>(?: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:
        - <regex>(?:bar)?\s+
        - <plain> BAR
        - <plain>bar

    choice:
        type: ChoicesAnswer
        choices:
        - 0.2
        - 1/6
        - ~CORRECT~ 0.25
        - <div><p>This_should_be_wrapped_by_p_tag</p></div>
        - [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:
        - <regex>(?:bar)?\s+
        - <plain> BAR
        - <plain>bar

    choice1:
        type: ChoicesAnswer
        choices:
        - 0.2
        - 1/6
        - ~CORRECT~ 0.25
        - <div><p>This_should_be_wrapped_by_p_tag</p></div>
        - [0.25]

    2choice:
        type: ChoicesAnswer
        choices:
        - 0.2
        - 1/6
        - ~CORRECT~ 0.25
        - <div><p>This_should_be_wrapped_by_p_tag</p></div>
        - [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:
        - <regex>(?:bar)?\s+
        - <plain> BAR
        - <plain>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:
        - <plain> BAR
        - <plain>bar

    blank_2:
        type: ShortAnswer
        width: 10em
        hint: <ol><li>with no hint title</li><li>HTML is OK</li><ol>
        correct_answer:
        - <plain> "1/5"
        - type: float
          value: 1/5
          rtol: 0.00001
        - <plain> 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.
    <img src="media:images/classroom.jpeg">

answers:

    blank1:
        type: ShortAnswer
        width: 4em
        required: True
        hint: Tex can be rendered in hint, e.g. $x_1$.
        hint_title: Hint
        correct_answer:
        - <plain> BAR
        - <plain>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:
        - <plain> BAR
        - <plain>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:
        - <plain> BAR
        - <plain>bar

"""

Dong Zhuang's avatar
Dong Zhuang committed
INLINE_MULTI_MARKDOWN_FEWER = """
type: InlineMultiQuestion
id: inlinemulti
value: 10
prompt: |

    # An InlineMultiQuestion example

    Complete the following paragraph.(old version)

question: |

    Foo and [[blank1]] are often used in code examples, or
    tutorials. $\\frac{1}{5}$ is equivalent to [[blank_2]].

    The correct answer for this choice question is [[choice_a]].
    The Upper case of "foo" is [[choice2]].

    One dollar is [[blank3]], and five percent is [[blank4]].

answers:

    blank1:
        type: ShortAnswer
        width: 4em
        required: True
        hint: Tex can be rendered in hint, e.g. $x_1$.
        hint_title: Hint
        correct_answer:
        - <plain> BAR
        - <plain>bar

    blank_2:
        type: ShortAnswer
        width: 10em
        hint: <ol><li>with no hint title</li><li>HTML is OK</li><ol>
        correct_answer:
        - <plain> "1/5"
        - type: float
          value: 1/5
          rtol: 0.00001
        - <plain> 0.2

    choice_a:
        type: ChoicesAnswer
        required: True
        choices:
        - ~CORRECT~ Correct
        - Wrong

    choice2:
        type: ChoicesAnswer
        choices:
        - ~CORRECT~ FOO
        - BAR
        - fOO

    blank3:
        type: ShortAnswer
        width: 3em
        prepended_text: "$"
        hint: Blank with prepended text
        correct_answer:
        - type: float
          value: 1
          rtol: 0.00001
        - <plain> "1"

    blank4:
        type: ShortAnswer
        width: 3em
        appended_text: "%"
        hint: Blank with appended text
        correct_answer:
        - type: float
          value: 5
          rtol: 0.00001
        - <plain> "5"

"""


def get_repo_blob_side_effect(repo, full_name, commit_sha, allow_tree=True):
    # Fake the inline multiple question yaml for specific commit
    if not (full_name == "questions/multi-question-example.yml"
            and commit_sha == b"ec41a2de73a99e6022060518cb5c5c162b88cdf5"):
        return get_repo_blob(repo, full_name, commit_sha, allow_tree)
    else:
        class Blob(object):
            pass
        blob = Blob()
        blob.data = INLINE_MULTI_MARKDOWN_FEWER.encode()
        return blob


def get_page_behavior_not_show_correctness_side_effect(page,
        permissions,
        session_in_progress,
        answer_was_graded,
        generates_grade,
        is_unenrolled_session,
        viewing_prior_version=False):
    page_behavior = get_page_behavior(
        page,
        permissions,
        session_in_progress,
        answer_was_graded,
        generates_grade,
        is_unenrolled_session,
        viewing_prior_version)
    page_behavior.show_correctness = False
    return page_behavior

Dong Zhuang's avatar
Dong Zhuang committed

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_negative_width(self):
        markdown = (INLINE_MULTI_MARKDOWN_EMBEDDED_ATTR_PATTERN
                    % {"attr1": "width: -4em",
                       "attr2": "width: 5em"})
        resp = self.get_page_sandbox_preview_response(markdown)
        self.assertEqual(resp.status_code, 200)
        self.assertSandboxNotHasValidPage(resp)
        self.assertResponseContextContains(
            resp, PAGE_ERRORS,
            "blank1: 'width': unrecogonized width attribute string: '-4em'")

    def test_negative_weight(self):
        markdown = (INLINE_MULTI_MARKDOWN_EMBEDDED_ATTR_PATTERN
                    % {"attr1": "weight: 15",
                       "attr2": "weight: -5"})
        resp = self.get_page_sandbox_preview_response(markdown)
        self.assertSandboxNotHasValidPage(resp)
        self.assertResponseContextContains(
            resp, PAGE_ERRORS,
            "blank2: 'weight' must be a non-negative value, got '-5' instead")

Dong Zhuang's avatar
Dong Zhuang committed
    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, "<p>This_should_be_wrapped_by_p_tag</p>", 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('</div> <div id="div_id_blank2"', resp.content.decode())

    def test_embedded_weight_count(self):
        markdown = (INLINE_MULTI_MARKDOWN_EMBEDDED_ATTR_PATTERN
                    % {"attr1": "weight: 15",
                       "attr2": "weight: 5"})
        resp = self.get_page_sandbox_preview_response(markdown)
        self.assertEqual(resp.status_code, 200)
        self.assertSandboxHasValidPage(resp)
        self.assertSandboxWarningTextContain(resp, None)

        # 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.75)

        # blank2 has not weight set
        markdown = (INLINE_MULTI_MARKDOWN_EMBEDDED_ATTR_PATTERN
                    % {"attr1": "weight: 15",
                       "attr2": ""})
        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={'blank1': ['Bar']})
        self.assertEqual(resp.status_code, 200)
        self.assertFormErrorLoose(resp, None)
        self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 1)

        resp = self.get_page_sandbox_submit_answer_response(
            markdown,
            answer_data={'blank2': 'One'})
        self.assertEqual(resp.status_code, 200)
        self.assertFormErrorLoose(resp, None)
        self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 0)

    def test_embedded_width_attr(self):
        markdown = (INLINE_MULTI_MARKDOWN_EMBEDDED_ATTR_PATTERN
                    % {"attr1": "width: 15",
                       "attr2": "width: 85 %"})
        resp = self.get_page_sandbox_preview_response(markdown)
        self.assertEqual(resp.status_code, 200)
        self.assertSandboxHasValidPage(resp)
        self.assertSandboxWarningTextContain(resp, None)
        self.assertIn("width: 8.5em", resp.context["form"].as_p())

        markdown = (INLINE_MULTI_MARKDOWN_EMBEDDED_ATTR_PATTERN
                    % {"attr1": "width: 15pt",
                       "attr2": "width: 5pt"})
        resp = self.get_page_sandbox_preview_response(markdown)
        self.assertEqual(resp.status_code, 200)
        self.assertSandboxHasValidPage(resp)
        self.assertSandboxWarningTextContain(resp, None)

        markdown = (INLINE_MULTI_MARKDOWN_EMBEDDED_ATTR_PATTERN
                    % {"attr1": "width: one",
                       "attr2": "width: 5 pt"})
        resp = self.get_page_sandbox_preview_response(markdown)
        self.assertEqual(resp.status_code, 200)
        self.assertSandboxNotHasValidPage(resp)
        self.assertResponseContextContains(
            resp, PAGE_ERRORS,
            "unrecogonized width attribute string: 'one'")

        markdown = (INLINE_MULTI_MARKDOWN_EMBEDDED_ATTR_PATTERN
                    % {"attr1": "width: 15 pt",
                       "attr2": "width: 5 km"})
        resp = self.get_page_sandbox_preview_response(markdown)
        self.assertEqual(resp.status_code, 200)
        self.assertSandboxNotHasValidPage(resp)
        self.assertResponseContextContains(
            resp, PAGE_ERRORS,
            "unsupported length unit 'km'")

    def test_embedded_question_no_correct_answer(self):
        markdown = INLINE_MULTI_MARKDOWN_EMBEDDED_NO_CORRECT_ANSWER
        resp = self.get_page_sandbox_preview_response(markdown)
        self.assertEqual(resp.status_code, 200)
        self.assertSandboxNotHasValidPage(resp)
        self.assertResponseContextContains(
            resp, PAGE_ERRORS,
            "blank1: at least one answer must be provided")

    def test_embedded_text_question_no_stringifiable_correct_answer(self):
        markdown = INLINE_MULTI_MARKDOWN_EMBEDDED_TEXT_Q_NO_STRINGIFIABLE_CORRECT_ANSWER  # noqa
        resp = self.get_page_sandbox_preview_response(markdown)
        self.assertEqual(resp.status_code, 200)
        self.assertSandboxNotHasValidPage(resp)
        self.assertResponseContextContains(
            resp, PAGE_ERRORS,
            "blank1: no matcher is able to provide a plain-text "
            "correct answer")

    def test_embedded_choice_question_no_correct_answer(self):
        markdown = INLINE_MULTI_MARKDOWN_EMBEDDED_CHOICE_Q_NO_CORRECT_ANSWER
        resp = self.get_page_sandbox_preview_response(markdown)
        self.assertEqual(resp.status_code, 200)
        self.assertSandboxNotHasValidPage(resp)
        self.assertResponseContextContains(
            resp, PAGE_ERRORS,
            " more correct answer(s) expected  for question 'choice', "
            "0 found")

    def test_embedded_choice_not_stringifiable(self):
        expected_page_error = (
            "'choice' choice 2: unable to convert to string")

        class BadChoice(object):
            def __str__(self):
                raise Exception

        from relate.utils import dict_to_struct
        fake_page_desc = dict_to_struct(
            {'type': 'InlineMultiQuestion', 'id': 'inlinemulti',
             'prompt':
                 '\n# An InlineMultiQuestion example\n\nComplete the '
                 'following paragraph.\n',
             'question': '\nFoo and [[choice]] are often used in code '
                         'examples.\n',
             '_field_names': [
                 'type', 'id', 'prompt', 'question', 'answers', 'value'],
             'answers': {'_field_names': ['choice'],
                         'choice': {
                             '_field_names': ['type',
                                              'choices'],
                             'type': 'ChoicesAnswer',
                             'choices': [0.2,
                                         BadChoice(),
                                         '~CORRECT~ 0.25']}},
             'value': 10}
        )

        with mock.patch("relate.utils.dict_to_struct") as mock_dict_to_struct:
            mock_dict_to_struct.return_value = fake_page_desc

            markdown = INLINE_MULTI_MARKDOWN_EMBEDDED_CHOICE_QUESTION

            resp = (
                self.get_page_sandbox_preview_response(markdown))
            self.assertEqual(resp.status_code, 200)
            self.assertSandboxNotHasValidPage(resp)
            self.assertResponseContextContains(resp, PAGE_ERRORS,
                                               expected_page_error)

    def test_embedded_question_no_answer_field_defined(self):
        markdown = INLINE_MULTI_MARKDOWN_NO_ANSWER_FIELD
        resp = self.get_page_sandbox_preview_response(markdown)
        self.assertEqual(resp.status_code, 200)
        self.assertSandboxNotHasValidPage(resp)
        self.assertResponseContextContains(
            resp, PAGE_ERRORS,
            "InlineMultiQuestion requires at least one answer field to "
            "be defined.")

    def test_embedded_naming_error(self):
        markdown = INLINE_MULTI_MARKDOWN_EMBEDDED_NAMING_ERROR
        resp = self.get_page_sandbox_preview_response(markdown)
        self.assertEqual(resp.status_code, 200)
        self.assertSandboxNotHasValidPage(resp)
        self.assertResponseContextContains(
            resp, PAGE_ERRORS,
            "ValidationError")
        self.assertResponseContextContains(
            resp, PAGE_ERRORS,