"""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'^

\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 '