aboutsummaryrefslogtreecommitdiff
path: root/tests/models/spectrum/test_question.py
blob: ba118d7152a18b7f2ed48631e352520bcecc07f9 (plain)
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
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
from datetime import datetime, timezone

from generalresearch.models import Source
from generalresearch.models.spectrum.question import (
    SpectrumQuestionOption,
    SpectrumQuestion,
    SpectrumQuestionType,
    SpectrumQuestionClass,
)
from generalresearch.models.thl.profiling.upk_question import (
    UpkQuestion,
    UpkQuestionSelectorMC,
    UpkQuestionType,
    UpkQuestionChoice,
)


class TestSpectrumQuestion:

    def test_parse_from_api_1(self):

        example_1 = {
            "qualification_code": 213,
            "text": "My household earns approximately $%%213%% per year",
            "cat": None,
            "desc": "Income",
            "type": 5,
            "class": 1,
            "condition_codes": [],
            "format": {"min": 0, "max": 999999, "regex": "/^([0-9]{1,6})$/i"},
            "crtd_on": 1502869927688,
            "mod_on": 1706557247467,
        }
        q = SpectrumQuestion.from_api(example_1, "us", "eng")

        expected_q = SpectrumQuestion(
            question_id="213",
            country_iso="us",
            language_iso="eng",
            question_name="Income",
            question_text="My household earns approximately $___ per year",
            question_type=SpectrumQuestionType.TEXT_ENTRY,
            tags=None,
            options=None,
            class_num=SpectrumQuestionClass.CORE,
            created=datetime(2017, 8, 16, 7, 52, 7, 688000, tzinfo=timezone.utc),
            is_live=True,
            source=Source.SPECTRUM,
            category_id=None,
        )
        assert "My household earns approximately $___ per year" == q.question_text
        assert "213" == q.question_id
        assert expected_q == q
        q.to_upk_question()
        assert "s:213" == q.external_id

    def test_parse_from_api_2(self):

        example_2 = {
            "qualification_code": 211,
            "text": "I'm a %%211%%",
            "cat": None,
            "desc": "Gender",
            "type": 1,
            "class": 1,
            "condition_codes": [
                {"id": "111", "text": "Male"},
                {"id": "112", "text": "Female"},
            ],
            "format": {"min": None, "max": None, "regex": ""},
            "crtd_on": 1502869927688,
            "mod_on": 1706557249817,
        }
        q = SpectrumQuestion.from_api(example_2, "us", "eng")
        expected_q = SpectrumQuestion(
            question_id="211",
            country_iso="us",
            language_iso="eng",
            question_name="Gender",
            question_text="I'm a",
            question_type=SpectrumQuestionType.SINGLE_SELECT,
            tags=None,
            options=[
                SpectrumQuestionOption(id="111", text="Male", order=0),
                SpectrumQuestionOption(id="112", text="Female", order=1),
            ],
            class_num=SpectrumQuestionClass.CORE,
            created=datetime(2017, 8, 16, 7, 52, 7, 688000, tzinfo=timezone.utc),
            is_live=True,
            source=Source.SPECTRUM,
            category_id=None,
        )
        assert expected_q == q
        q.to_upk_question()

    def test_parse_from_api_3(self):

        example_3 = {
            "qualification_code": 220,
            "text": "My child is a %%230%% %%221%% old %%220%%",
            "cat": None,
            "desc": "Child Dependent",
            "type": 6,
            "class": 4,
            "condition_codes": [
                {"id": "111", "text": "Boy"},
                {"id": "112", "text": "Girl"},
            ],
            "format": {"min": None, "max": None, "regex": ""},
            "crtd_on": 1502869927688,
            "mod_on": 1706556781278,
        }
        q = SpectrumQuestion.from_api(example_3, "us", "eng")
        # This fails because the text has variables from other questions in it
        assert q is None

    def test_parse_from_api_4(self):

        example_4 = {
            "qualification_code": 1039,
            "text": "Do you suffer from any of the following ailments or medical conditions? (Select all that apply) "
            " %%1039%%",
            "cat": "Ailments, Illness",
            "desc": "Standard Ailments",
            "type": 3,
            "class": 2,
            "condition_codes": [
                {"id": "111", "text": "Allergies (Food, Nut, Skin)"},
                {"id": "999", "text": "None of the above"},
                {"id": "130", "text": "Other"},
                {
                    "id": "129",
                    "text": "Women's Health Conditions (Reproductive Issues)",
                },
            ],
            "format": {"min": None, "max": None, "regex": ""},
            "crtd_on": 1502869927688,
            "mod_on": 1706557241693,
        }
        q = SpectrumQuestion.from_api(example_4, "us", "eng")
        expected_q = SpectrumQuestion(
            question_id="1039",
            country_iso="us",
            language_iso="eng",
            question_name="Standard Ailments",
            question_text="Do you suffer from any of the following ailments or medical conditions? (Select all that "
            "apply)",
            question_type=SpectrumQuestionType.MULTI_SELECT,
            tags="Ailments, Illness",
            options=[
                SpectrumQuestionOption(
                    id="111", text="Allergies (Food, Nut, Skin)", order=0
                ),
                SpectrumQuestionOption(
                    id="129",
                    text="Women's Health Conditions (Reproductive Issues)",
                    order=1,
                ),
                SpectrumQuestionOption(id="130", text="Other", order=2),
                SpectrumQuestionOption(id="999", text="None of the above", order=3),
            ],
            class_num=SpectrumQuestionClass.EXTENDED,
            created=datetime(2017, 8, 16, 7, 52, 7, 688000, tzinfo=timezone.utc),
            is_live=True,
            source=Source.SPECTRUM,
            category_id=None,
        )
        assert expected_q == q

        # todo: we should have something that infers that if the choice text is "None of the above",
        #   then the choice is exclusive
        u = UpkQuestion(
            id=None,
            ext_question_id="s:1039",
            type=UpkQuestionType.MULTIPLE_CHOICE,
            selector=UpkQuestionSelectorMC.MULTIPLE_ANSWER,
            country_iso="us",
            language_iso="eng",
            text="Do you suffer from any of the following ailments or medical conditions? (Select all "
            "that apply)",
            choices=[
                UpkQuestionChoice(
                    id="111",
                    text="Allergies (Food, Nut, Skin)",
                    order=0,
                    group=None,
                    exclusive=False,
                    importance=None,
                ),
                UpkQuestionChoice(
                    id="129",
                    text="Women's Health Conditions (Reproductive Issues)",
                    order=1,
                    group=None,
                    exclusive=False,
                    importance=None,
                ),
                UpkQuestionChoice(
                    id="130",
                    text="Other",
                    order=2,
                    group=None,
                    exclusive=False,
                    importance=None,
                ),
                UpkQuestionChoice(
                    id="999",
                    text="None of the above",
                    order=3,
                    group=None,
                    exclusive=False,
                    importance=None,
                ),
            ],
        )
        assert u == q.to_upk_question()