File size: 11,801 Bytes
8018595
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7638cbd
 
 
 
 
 
 
 
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
350
351
352
353
354
355
356
357
# pylint: disable=missing-docstring
"""Lean coverage for the BOADICEA breast cancer risk model."""

from typing import Any

import pytest

from sentinel.api_clients.canrisk import BOADICEAInput, CanRiskAPIError, CanRiskClient
from sentinel.risk_models.boadicea import BOADICEARiskModel
from sentinel.user_input import (
    Anthropometrics,
    BreastHealthHistory,
    CancerType,
    Demographics,
    Ethnicity,
    FamilyMemberCancer,
    FamilyRelation,
    FamilySide,
    FemaleSpecific,
    GeneticMutation,
    HormoneUse,
    HormoneUseHistory,
    Lifestyle,
    MenstrualHistory,
    ParityHistory,
    PersonalMedicalHistory,
    RelationshipDegree,
    Sex,
    SmokingHistory,
    SmokingStatus,
    UserInput,
)


@pytest.fixture
def canrisk_client_mock(mocker) -> CanRiskClient:
    return mocker.create_autospec(CanRiskClient, instance=True)


@pytest.fixture
def boadicea_model(canrisk_client_mock: CanRiskClient) -> BOADICEARiskModel:
    return BOADICEARiskModel(client=canrisk_client_mock)


def _canrisk_payload(percent: float) -> dict[str, Any]:
    return {
        "pedigree_result": [
            {
                "ten_yr_cancer_risk": [
                    {
                        "age": 50,
                        "breast cancer risk": {
                            "decimal": percent / 100,
                            "percent": percent,
                        },
                    }
                ],
                "cancer_risks": [
                    {
                        "age": 50,
                        "breast cancer risk": {
                            "decimal": percent / 100,
                            "percent": percent,
                        },
                    }
                ],
            }
        ]
    }


def _baseline_user(
    mutations: list[GeneticMutation] | None = None,
    ethnicity: Ethnicity | None = Ethnicity.WHITE,
) -> UserInput:
    return UserInput(
        demographics=Demographics(
            age_years=45,
            sex=Sex.FEMALE,
            ethnicity=ethnicity,
            anthropometrics=Anthropometrics(height_cm=165.0, weight_kg=65.0),
        ),
        lifestyle=Lifestyle(
            smoking=SmokingHistory(status=SmokingStatus.NEVER),
        ),
        personal_medical_history=PersonalMedicalHistory(
            genetic_mutations=mutations or []
        ),
        female_specific=FemaleSpecific(
            menstrual=MenstrualHistory(age_at_menarche=13),
            parity=ParityHistory(
                num_live_births=2,
                age_at_first_live_birth=28,
            ),
            breast_health=BreastHealthHistory(),
        ),
        family_history=[
            FamilyMemberCancer(
                relation=FamilyRelation.MOTHER,
                cancer_type=CancerType.BREAST,
                age_at_diagnosis=52,
                degree=RelationshipDegree.FIRST,
                side=FamilySide.MATERNAL,
            )
        ],
    )


def test_model_metadata(boadicea_model: BOADICEARiskModel) -> None:
    assert boadicea_model.name == "boadicea"
    assert boadicea_model.cancer_type() == "breast"
    description = boadicea_model.description().lower()
    interpretation = boadicea_model.interpretation().lower()
    assert "boadicea" in description
    assert "genetic" in description and "genetic" in interpretation
    assert any("CanRisk" in ref for ref in boadicea_model.references())


@pytest.mark.parametrize(
    "user, expected",
    [
        (
            UserInput(
                demographics=Demographics(
                    age_years=40,
                    sex=Sex.MALE,
                    anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=70.0),
                ),
                lifestyle=Lifestyle(
                    smoking=SmokingHistory(status=SmokingStatus.NEVER),
                ),
                personal_medical_history=PersonalMedicalHistory(),
            ),
            "N/A: BOADICEA model is only applicable to female patients.",
        ),
        (
            UserInput(
                demographics=Demographics(
                    age_years=40,
                    sex=Sex.FEMALE,
                    anthropometrics=Anthropometrics(height_cm=165.0, weight_kg=65.0),
                ),
                lifestyle=Lifestyle(
                    smoking=SmokingHistory(status=SmokingStatus.NEVER),
                ),
                personal_medical_history=PersonalMedicalHistory(
                    genetic_mutations=[GeneticMutation.BRCA1]
                ),
            ),
            "N/A: Missing female-specific information required for BOADICEA.",
        ),
    ],
)
def test_ineligible_patients_return_messages(
    boadicea_model: BOADICEARiskModel, user: UserInput, expected: str
) -> None:
    # For male patients, validation now raises ValueError instead of returning N/A
    if user.demographics.sex == Sex.MALE:
        with pytest.raises(ValueError) as exc_info:
            boadicea_model.compute_score(user)
        assert "Invalid inputs for BOADICEA" in str(exc_info.value)
        assert "must be FEMALE" in str(exc_info.value)
    else:
        assert boadicea_model.compute_score(user) == expected


@pytest.mark.parametrize(
    "mutations, expected_brca1, expected_brca2",
    [
        ([GeneticMutation.BRCA1], True, False),
        ([GeneticMutation.BRCA2], False, True),
        ([], False, False),
    ],
)
def test_brca_flag_detection(
    boadicea_model: BOADICEARiskModel,
    canrisk_client_mock: CanRiskClient,
    mutations: list[GeneticMutation],
    expected_brca1: bool,
    expected_brca2: bool,
) -> None:
    canrisk_client_mock.submit_boadicea_assessment.return_value = _canrisk_payload(12.5)

    user = _baseline_user(mutations)
    score = boadicea_model.compute_score(user)

    assert score == "12.5%"
    boadicea_input = canrisk_client_mock.submit_boadicea_assessment.call_args.args[0]
    assert boadicea_input.brca1_mutation is expected_brca1
    assert boadicea_input.brca2_mutation is expected_brca2


def test_successful_request_populates_payload(
    boadicea_model: BOADICEARiskModel,
    canrisk_client_mock: CanRiskClient,
) -> None:
    canrisk_client_mock.submit_boadicea_assessment.return_value = _canrisk_payload(18.0)

    user = UserInput(
        demographics=Demographics(
            age_years=42,
            sex=Sex.FEMALE,
            ethnicity=Ethnicity.ASHKENAZI_JEWISH,
            anthropometrics=Anthropometrics(height_cm=165.0, weight_kg=65.0),
        ),
        lifestyle=Lifestyle(
            smoking=SmokingHistory(status=SmokingStatus.NEVER),
        ),
        personal_medical_history=PersonalMedicalHistory(
            genetic_mutations=[GeneticMutation.BRCA1, GeneticMutation.BRCA2]
        ),
        female_specific=FemaleSpecific(
            menstrual=MenstrualHistory(age_at_menarche=13),
            parity=ParityHistory(
                num_live_births=1,
                age_at_first_live_birth=28,
            ),
            breast_health=BreastHealthHistory(),
        ),
        family_history=[
            FamilyMemberCancer(
                relation=FamilyRelation.MOTHER,
                cancer_type=CancerType.BREAST,
                age_at_diagnosis=52,
                degree=RelationshipDegree.FIRST,
                side=FamilySide.MATERNAL,
            ),
            FamilyMemberCancer(
                relation=FamilyRelation.SISTER,
                cancer_type=CancerType.OVARIAN,
                age_at_diagnosis=48,
                degree=RelationshipDegree.FIRST,
                side=FamilySide.MATERNAL,
            ),
        ],
    )

    assert boadicea_model.compute_score(user) == "18.0%"
    boadicea_input = canrisk_client_mock.submit_boadicea_assessment.call_args.args[0]

    assert boadicea_input.age == 42
    assert boadicea_input.ashkenazi_ancestry is True
    assert boadicea_input.height == 1.65 and boadicea_input.weight == 65.0
    assert boadicea_input.bmi == pytest.approx(65.0 / (1.65**2), rel=1e-2)
    assert len(boadicea_input.family_history_breast) == 1
    assert len(boadicea_input.family_history_ovarian) == 1
    assert len(boadicea_input.family_history) == 2


@pytest.mark.parametrize(
    "exception, prefix",
    [
        (CanRiskAPIError("service unavailable"), "N/A: API error"),
        (ValueError("unexpected"), "N/A: Calculation error"),
    ],
)
def test_errors_are_surface_as_strings(
    boadicea_model: BOADICEARiskModel,
    canrisk_client_mock: CanRiskClient,
    exception: Exception,
    prefix: str,
) -> None:
    canrisk_client_mock.submit_boadicea_assessment.side_effect = exception
    user = _baseline_user([GeneticMutation.BRCA1])

    score = boadicea_model.compute_score(user)
    assert score.startswith(prefix)
    assert str(exception) in score


def test_response_parsing_handles_missing_ten_year_risk(
    boadicea_model: BOADICEARiskModel,
    canrisk_client_mock: CanRiskClient,
) -> None:
    responses = [
        _canrisk_payload(9.1),
        {
            "pedigree_result": [
                {"lifetime_cancer_risk": [{"breast cancer risk": {"percent": 42.0}}]}
            ]
        },
        {},
    ]
    expected = [
        "9.1%",
        "N/A: 10-year risk not available from API response.",
        "N/A: 10-year risk not available from API response.",
    ]

    user = _baseline_user([GeneticMutation.BRCA1])
    for response, outcome in zip(responses, expected, strict=True):
        canrisk_client_mock.submit_boadicea_assessment.return_value = response
        assert boadicea_model.compute_score(user) == outcome


def test_boadicea_input_from_user_input() -> None:
    user = UserInput(
        demographics=Demographics(
            age_years=40,
            sex=Sex.FEMALE,
            ethnicity=Ethnicity.ASHKENAZI_JEWISH,
            anthropometrics=Anthropometrics(height_cm=170.0, weight_kg=60.0),
        ),
        lifestyle=Lifestyle(
            smoking=SmokingHistory(status=SmokingStatus.NEVER),
        ),
        personal_medical_history=PersonalMedicalHistory(
            genetic_mutations=[GeneticMutation.BRCA1]
        ),
        female_specific=FemaleSpecific(
            menstrual=MenstrualHistory(
                age_at_menarche=12,
                age_at_menopause=50,
            ),
            parity=ParityHistory(
                num_live_births=3,
                age_at_first_live_birth=25,
            ),
            hormone_use=HormoneUseHistory(
                estrogen_use=HormoneUse.CURRENT,
            ),
            breast_health=BreastHealthHistory(),
        ),
        family_history=[
            FamilyMemberCancer(
                relation=FamilyRelation.MOTHER,
                cancer_type=CancerType.BREAST,
                age_at_diagnosis=45,
                degree=RelationshipDegree.FIRST,
                side=FamilySide.MATERNAL,
            ),
            FamilyMemberCancer(
                relation=FamilyRelation.MATERNAL_AUNT,
                cancer_type=CancerType.OVARIAN,
                age_at_diagnosis=55,
                degree=RelationshipDegree.SECOND,
                side=FamilySide.MATERNAL,
            ),
        ],
    )

    boadicea_input = BOADICEAInput.from_user_input(user)

    assert boadicea_input.brca1_mutation is True
    assert boadicea_input.ashkenazi_ancestry is True
    assert boadicea_input.bmi == pytest.approx(60.0 / (1.70**2), rel=1e-2)
    assert len(boadicea_input.family_history) == 2
    assert boadicea_input.age_at_menopause == 50
    assert boadicea_input.hormone_therapy_use == "current"


def test_bmi_property_handles_missing_values() -> None:
    assert BOADICEAInput(age=30, height=1.75, weight=70.0).bmi == pytest.approx(
        22.86, rel=1e-2
    )
    assert BOADICEAInput(age=30).bmi is None