File size: 13,227 Bytes
8018595
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
273
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
346
347
348
349
"""Tests for the minimal strict V1 input schema."""

from datetime import date

import pytest
from pydantic import ValidationError

from sentinel.user_input import (
    AlcoholConsumption,
    # Models
    Anthropometrics,
    AspirinUse,
    BreastHealthHistory,
    CancerType,
    ChronicCondition,
    ClinicalTests,
    ComplexionLevel,
    Country,
    Demographics,
    DermatologicProfile,
    DREResult,
    Ethnicity,
    FamilyMemberCancer,
    FamilyRelation,
    FamilySide,
    FemaleSmallMolesCategory,
    FemaleSpecific,
    FemaleTanResponse,
    FrecklingIntensity,
    GeneticMutation,
    HormoneUse,
    HormoneUseHistory,
    Lifestyle,
    MaleSmallMolesCategory,
    MenstrualHistory,
    NSAIDUse,
    ParityHistory,
    PersonalMedicalHistory,
    PhysicalActivityLevel,
    PSATest,
    RelationshipDegree,
    Sex,
    SmokingHistory,
    SmokingStatus,
    SymptomEntry,
    SymptomType,
    UserInput,
    USGeographicRegion,
)


class TestStrictSchema:
    """Test schema strictness and validation."""

    def test_strict_forbids_extra_fields(self):
        """Test that extra fields are rejected."""
        with pytest.raises(ValidationError) as exc_info:
            UserInput(
                demographics=Demographics(
                    age_years=45,
                    sex=Sex.FEMALE,
                    anthropometrics=Anthropometrics(height_cm=165, weight_kg=70),
                ),
                lifestyle=Lifestyle(
                    smoking=SmokingHistory(status=SmokingStatus.NEVER),
                    alcohol_consumption=AlcoholConsumption.NONE,
                ),
                personal_medical_history=PersonalMedicalHistory(),
                extra_field="should_fail",
            )
        assert "extra inputs are not permitted" in str(exc_info.value).lower()

    def test_required_fields_enforced(self):
        """Test that required fields must be present."""
        with pytest.raises(ValidationError):
            UserInput(
                demographics=Demographics(
                    age_years=45,
                    # Missing sex
                    anthropometrics=Anthropometrics(height_cm=165, weight_kg=70),
                ),
                lifestyle=Lifestyle(
                    smoking=SmokingHistory(status=SmokingStatus.NEVER),
                    alcohol_consumption=AlcoholConsumption.NONE,
                ),
                personal_medical_history=PersonalMedicalHistory(),
            )

    def test_enum_validation(self):
        """Test that only valid enum values are accepted."""
        with pytest.raises(ValidationError):
            UserInput(
                demographics=Demographics(
                    age_years=45,
                    sex="invalid_sex",  # Should be Sex enum
                    anthropometrics=Anthropometrics(height_cm=165, weight_kg=70),
                ),
                lifestyle=Lifestyle(
                    smoking=SmokingHistory(status=SmokingStatus.NEVER),
                    alcohol_consumption=AlcoholConsumption.NONE,
                ),
                personal_medical_history=PersonalMedicalHistory(),
            )

    def test_numeric_constraints(self):
        """Test that numeric fields adhere to their defined ranges."""
        with pytest.raises(ValidationError):
            UserInput(
                demographics=Demographics(
                    age_years=150,  # Too high
                    sex=Sex.FEMALE,
                    anthropometrics=Anthropometrics(height_cm=165, weight_kg=70),
                ),
                lifestyle=Lifestyle(
                    smoking=SmokingHistory(status=SmokingStatus.NEVER),
                    alcohol_consumption=AlcoholConsumption.NONE,
                ),
                personal_medical_history=PersonalMedicalHistory(),
            )

    def test_date_parsing(self):
        """Test that date fields correctly accept datetime.date objects."""
        test_date = date(2023, 1, 15)
        user = UserInput(
            demographics=Demographics(
                age_years=45,
                sex=Sex.FEMALE,
                anthropometrics=Anthropometrics(height_cm=165, weight_kg=70),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
                alcohol_consumption=AlcoholConsumption.NONE,
            ),
            personal_medical_history=PersonalMedicalHistory(),
            clinical_tests=ClinicalTests(
                psa=PSATest(
                    value_ng_ml=2.5,
                    date=test_date,
                )
            ),
        )
        assert user.clinical_tests.psa.date == test_date

    def test_minimal_valid_input(self):
        """Test a minimal valid input."""
        user = UserInput(
            demographics=Demographics(
                age_years=45,
                sex=Sex.FEMALE,
                anthropometrics=Anthropometrics(height_cm=165, weight_kg=70),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
                alcohol_consumption=AlcoholConsumption.NONE,
            ),
            personal_medical_history=PersonalMedicalHistory(),
        )
        assert user.demographics.age_years == 45
        assert user.demographics.sex == Sex.FEMALE
        assert user.schema_version == "v1.0"

    def test_comprehensive_input(self):
        """Test a comprehensive input with all sections populated."""
        user = UserInput(
            demographics=Demographics(
                age_years=45,
                sex=Sex.FEMALE,
                ethnicity=Ethnicity.WHITE,
                anthropometrics=Anthropometrics(height_cm=165, weight_kg=70),
                country=Country.UNITED_STATES,
                education_level=16,
                townsend_index=0.5,
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(
                    status=SmokingStatus.FORMER,
                    cigarettes_per_day=20.0,
                    years_smoked=10.0,
                    years_since_quit=5.0,
                    pack_years=10.0,
                ),
                alcohol_consumption=AlcoholConsumption.MODERATE,
                physical_activity_level=PhysicalActivityLevel.MODERATE,
            ),
            family_history=[
                FamilyMemberCancer(
                    relation=FamilyRelation.MOTHER,
                    cancer_type=CancerType.BREAST,
                    age_at_diagnosis=55,
                    degree=RelationshipDegree.FIRST,
                    side=FamilySide.MATERNAL,
                )
            ],
            personal_medical_history=PersonalMedicalHistory(
                chronic_conditions=[ChronicCondition.DIABETES],
                previous_cancers=[],
                genetic_mutations=[],
                has_polyps=False,
                aspirin_use=AspirinUse.CURRENT,
                nsaid_use=NSAIDUse.NEVER,
            ),
            female_specific=FemaleSpecific(
                menstrual=MenstrualHistory(
                    age_at_menarche=12,
                    age_at_menopause=None,
                ),
                parity=ParityHistory(
                    num_live_births=2,
                    age_at_first_live_birth=28,
                ),
                hormone_use=HormoneUseHistory(estrogen_use=HormoneUse.NEVER),
                breast_health=BreastHealthHistory(
                    num_biopsies=1,
                    atypical_hyperplasia=False,
                ),
            ),
            clinical_tests=ClinicalTests(
                psa=PSATest(
                    value_ng_ml=2.5,
                    date=date(2023, 1, 15),
                )
            ),
            symptoms=[
                SymptomEntry(
                    symptom_type=SymptomType.ABDOMINAL_PAIN,
                    onset_date=date(2023, 1, 10),
                    duration_days=5,
                )
            ],
            dermatologic=DermatologicProfile(
                region=USGeographicRegion.CENTRAL,
                complexion=ComplexionLevel.MEDIUM,
                freckling=FrecklingIntensity.MILD,
                female_tan=FemaleTanResponse.MODERATE,
                female_small_moles=FemaleSmallMolesCategory.LESS_THAN_FIVE,
                male_has_two_or_more_big_moles=False,
                solar_damage=False,
            ),
        )
        assert user.demographics.ethnicity == Ethnicity.WHITE
        assert user.lifestyle.smoking.status == SmokingStatus.FORMER
        assert len(user.family_history) == 1
        assert user.family_history[0].cancer_type == CancerType.BREAST
        assert user.female_specific is not None
        assert user.female_specific.parity.num_live_births == 2

    def test_female_specific_optional(self):
        """Test that female_specific is optional."""
        user = UserInput(
            demographics=Demographics(
                age_years=45,
                sex=Sex.MALE,  # Male user
                anthropometrics=Anthropometrics(height_cm=175, weight_kg=80),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
                alcohol_consumption=AlcoholConsumption.NONE,
            ),
            personal_medical_history=PersonalMedicalHistory(),
            female_specific=None,  # Should be allowed for males
        )
        assert user.female_specific is None

    def test_nested_strict_models(self):
        """Test that nested models also forbid extra fields."""
        with pytest.raises(ValidationError):
            Demographics(
                age_years=45,
                sex=Sex.FEMALE,
                anthropometrics=Anthropometrics(height_cm=165, weight_kg=70),
                extra_field="should_fail",
            )

    def test_list_fields_default_to_empty(self):
        """Test that list fields default to empty lists."""
        user = UserInput(
            demographics=Demographics(
                age_years=45,
                sex=Sex.FEMALE,
                anthropometrics=Anthropometrics(height_cm=165, weight_kg=70),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
                alcohol_consumption=AlcoholConsumption.NONE,
            ),
            personal_medical_history=PersonalMedicalHistory(),
        )
        assert user.family_history == []
        assert user.clinical_tests.psa is None
        assert user.symptoms == []

    def test_enum_completeness(self):
        """Test that all enums are covered."""
        # Test that we can instantiate all enum values
        assert Sex.MALE == "male"
        assert Ethnicity.WHITE == "white"
        assert Country.UNITED_STATES == "us"

    def test_chronic_condition_enum_includes_anaemia_and_endometrial(self):
        """Test that new chronic conditions are in the enum."""
        assert ChronicCondition.ANAEMIA == "anaemia"
        assert ChronicCondition.ENDOMETRIAL_POLYPS == "endometrial_polyps"
        assert SmokingStatus.CURRENT == "current"
        assert AlcoholConsumption.HEAVY == "heavy"
        assert PhysicalActivityLevel.HIGH == "high"
        assert CancerType.MELANOMA == "melanoma"
        assert GeneticMutation.BRCA1 == "brca1"
        assert ChronicCondition.COPD == "copd"
        assert FamilyRelation.MOTHER == "mother"
        assert RelationshipDegree.FIRST == "1"
        assert FamilySide.MATERNAL == "maternal"
        assert DREResult.NORMAL == "normal"
        assert HormoneUse.CURRENT == "current"
        assert USGeographicRegion.SOUTHERN == "southern"

        # Test str, Enum values (converted from IntEnum)
        assert ComplexionLevel.DARK == "dark"
        assert FrecklingIntensity.SEVERE == "severe"
        assert FemaleTanResponse.NONE == "none"
        assert MaleSmallMolesCategory.SEVENTEEN_OR_MORE == "seventeen_or_more"
        assert FemaleSmallMolesCategory.TWELVE_OR_MORE == "twelve_or_more"

    def test_boolean_fields(self):
        """Test boolean fields in dermatologic profile."""
        user = UserInput(
            demographics=Demographics(
                age_years=45,
                sex=Sex.MALE,
                anthropometrics=Anthropometrics(height_cm=175, weight_kg=80),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
                alcohol_consumption=AlcoholConsumption.NONE,
            ),
            personal_medical_history=PersonalMedicalHistory(),
            dermatologic=DermatologicProfile(
                region=USGeographicRegion.CENTRAL,
                complexion=ComplexionLevel.MEDIUM,
                freckling=FrecklingIntensity.MILD,
                male_sunburn=True,
                male_has_two_or_more_big_moles=False,
                male_small_moles=MaleSmallMolesCategory.SEVEN_TO_SIXTEEN,
                solar_damage=True,
            ),
        )
        assert user.dermatologic.male_sunburn is True
        assert user.dermatologic.male_has_two_or_more_big_moles is False
        assert user.dermatologic.solar_damage is True