Newer
Older
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 course.content import get_repo_blob
from course.flow import get_page_behavior
from tests.base_test_mixins import SingleCourseQuizPageTestMixin
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
"""
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
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 = r"""
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
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 = r"""
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
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]
"""
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
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 = r"""
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
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
"""
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
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
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")
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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,