File size: 2,517 Bytes
572a729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The question-pattern placeholder every write path has to carry.

CMS started requiring a question pattern wherever a question is approved. The
real vocabulary isn't published yet — it is not in /lookups and no live question
carries a real value — so every writer stamps the same placeholder for now. What
matters is that no write path drops it, and that none of them stomp on a genuine
value once SMEs start setting one in CMS.
"""
import sys
from pathlib import Path

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parent.parent / 'backend' / 'ai_tagger'))


@pytest.fixture(scope='module')
def pt():
    import push_tags
    return push_tags


@pytest.mark.parametrize('existing', [
    None,                       # key absent entirely
    [],                         # the shape most of the bank is in
    ['', '   '],                # present but blank
])
def test_missing_pattern_is_filled(pt, existing):
    data = {} if existing is None else {'question_patterns': existing}
    assert pt.ensure_question_pattern(data) is True
    assert data['question_patterns'] == [pt.DEFAULT_QUESTION_PATTERN]


def test_null_value_is_filled(pt):
    data = {'question_patterns': None}
    assert pt.ensure_question_pattern(data) is True
    assert data['question_patterns'] == [pt.DEFAULT_QUESTION_PATTERN]


def test_placeholder_is_left_alone(pt):
    data = {'question_patterns': [pt.DEFAULT_QUESTION_PATTERN]}
    assert pt.ensure_question_pattern(data) is False
    assert data['question_patterns'] == [pt.DEFAULT_QUESTION_PATTERN]


def test_a_real_pattern_is_never_overwritten(pt):
    """The whole point of the placeholder is to be replaced. Once a human sets a
    real pattern in CMS, a tagging or solutions run must not undo it."""
    data = {'question_patterns': ['assertion_reason']}
    assert pt.ensure_question_pattern(data) is False
    assert data['question_patterns'] == ['assertion_reason']


def test_uploader_payload_carries_a_pattern(app_mod, pt):
    """The uploader builds its payload from scratch rather than fetch-modify-PUT,
    so it needs the field independently — and with the same value as the tagger,
    or approvability depends on which tool touched the question last."""
    payload = app_mod.build_payload({
        'question_text': 'What is 2 + 2?',
        'option_a': '3', 'option_b': '4', 'option_c': '5', 'option_d': '6',
        'correct_answer': 'B', 'difficulty': 'easy',
    })
    assert payload['question_patterns'] == [pt.DEFAULT_QUESTION_PATTERN]