Spaces:
Running
Running
File size: 6,371 Bytes
0451415 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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 | """The shipped Word template must stay a clean paste-over target for SMEs.
samples/sample_questions.docx is generated by make_sample_docx.py; regenerate with
`python make_sample_docx.py --check` if a parser change moves it.
"""
import re
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parent.parent
SAMPLE = ROOT / 'samples' / 'sample_questions.docx'
@pytest.fixture(scope='module')
def sample(app_mod):
if not SAMPLE.exists():
pytest.skip(f'{SAMPLE.name} missing β run make_sample_docx.py')
qs = app_mod.parse_word(str(SAMPLE))
app_mod.validate_questions(qs)
return qs
@pytest.fixture(scope='module')
def paragraphs():
if not SAMPLE.exists():
pytest.skip(f'{SAMPLE.name} missing β run make_sample_docx.py')
from docx import Document
return [p.text.strip() for p in Document(str(SAMPLE)).paragraphs if p.text.strip()]
def test_all_five_questions_parse(sample):
assert len(sample) == 5, [q['question_text'][:40] for q in sample]
def test_no_question_is_flagged(sample):
bad = [(i, q['issues']) for i, q in enumerate(sample, 1) if q['issues']]
assert bad == [], f'the template must be clean: {bad}'
def test_every_question_has_an_answer_and_a_solution(sample):
for i, q in enumerate(sample, 1):
assert str(q['correct_answer']).strip(), f'Q{i} has no answer key'
assert q['solution'].strip(), f'Q{i} has no solution'
# ββ only the four line kinds an SME needs ββββββββββββββββββββββββββββββββββββ
def test_template_has_no_headings_or_metadata_lines(paragraphs):
"""question -> options -> answer -> solution, and nothing else. No subject
heading, no title, no 'Question Type:'/'Difficulty Level:' lines."""
banned = re.compile(r'^(MATHEMATICS|PHYSICS|CHEMISTRY|BOTANY|ZOOLOGY|BIOLOGY)\b'
r'|^Question\s*Type\s*[:\-]|^Difficulty', re.I)
offenders = [p for p in paragraphs if banned.match(p)]
assert offenders == [], offenders
def test_template_opens_with_a_question(paragraphs):
"""Any other opening line would itself be parsed as a question."""
assert re.match(r'^(?:Q\s*)?\d+[.:]\s*\S', paragraphs[0]), paragraphs[0]
def test_no_phantom_questions(sample):
"""Every parsed question must have an answer β a phantom made from a stray
line (a heading, or a solution paragraph) would have none."""
for i, q in enumerate(sample, 1):
assert q['correct_answer'], f'Q{i} looks like a phantom question'
# ββ both option marker styles ββββββββββββββββββββββββββββββββββββββββββββββββ
def test_template_shows_both_option_styles(paragraphs):
joined = '\n'.join(paragraphs)
assert re.search(r'^[a-d]\)\s', joined, re.M), 'no lowercase a) b) c) d) example'
assert re.search(r'^\([A-D]\)\s', joined, re.M), 'no (A) (B) (C) (D) example'
assert re.search(r'^1\)\s.*\s2\)\s', joined, re.M), 'no numbered 1) 2) 3) 4) example'
@pytest.mark.parametrize('idx,expected_answer', [(0, 'b'), (1, '3'), (4, 'B')])
def test_both_styles_parse_their_options_and_answer(app_mod, sample, idx, expected_answer):
q = sample[idx]
filled = sum(1 for k in 'abcd' if app_mod._option_filled(q.get(f'option_{k}', '')))
assert filled == 4, f'Q{idx + 1}: {filled}/4 options'
assert q['correct_answer'] == expected_answer
# the marker itself must not survive into the option HTML
assert not re.match(r'^<p>\s*\(?[a-dA-D1-4]\)', q['option_a']), q['option_a']
def test_letter_answers_map_to_positions_in_the_payload(app_mod, sample):
"""'Answer: b' and 'Answer: B' must both reach the CMS as option 2."""
assert app_mod.build_payload(sample[0])['question']['correct_answer'] == ['2']
assert app_mod.build_payload(sample[4])['question']['correct_answer'] == ['2']
# ββ types, maths, figure, band βββββββββββββββββββββββββββββββββββββββββββββββ
def test_types_are_inferred_without_metadata_lines(app_mod, sample):
"""4 options -> single correct; no options -> numerical. No type hint needed."""
types = [app_mod.normalise_qtype(q['question_type']) for q in sample]
assert types.count('single correct mcq') == 3
assert types.count('numerical value question') == 2
def test_numerical_questions_have_no_options(app_mod, sample):
for i, q in enumerate(sample, 1):
if 'numerical' in app_mod.normalise_qtype(q['question_type']):
filled = sum(1 for k in 'abcd'
if app_mod._option_filled(q.get(f'option_{k}', '')))
assert filled == 0, f'Q{i} is numerical but has {filled} options'
def test_template_demonstrates_maths_and_a_figure(sample):
html = ''.join(q['question_text'] + q['solution'] +
''.join(q.get(f'option_{k}', '') for k in 'abcd') for q in sample)
if '<math' not in html:
pytest.skip('OMML2MML.XSL not available β see vendor/README.md')
assert '<img src="data:image/png;base64,' in html, 'the figure did not survive'
def test_nothing_failed_to_convert(app_mod, sample):
fields = [q[f] for q in sample for f in
('question_text', 'option_a', 'option_b', 'option_c', 'option_d', 'solution')]
assert app_mod.conversion_failures(*fields) == []
def test_numerical_band_is_carried_through(app_mod, sample):
"""Q4 is written "Answer: 0.28-0.32" β the hyphen form is the only band the
answer-line pattern accepts."""
banded = [q for q in sample if q.get('answer_range')]
assert len(banded) == 1, 'expected exactly one banded numerical question'
ranges = app_mod.build_payload(banded[0])['question']['solutionRanges']
assert ranges == [{'name': 'Default Range', 'start': 0.28, 'end': 0.32}], ranges
def test_every_question_builds_a_valid_payload(app_mod, sample):
for i, q in enumerate(sample, 1):
p = app_mod.build_payload(q)
assert p['questiontype'], f'Q{i}: question type did not resolve to a CMS uuid'
assert p['question']['correct_answer'], f'Q{i}: empty answer in payload'
assert p['question']['solution'], f'Q{i}: empty solution in payload'
assert p['status'] == 'draft'
|