|
1 | 1 | """Unit tests for patient cleaning functions.""" |
2 | 2 |
|
| 3 | +from datetime import date |
| 4 | + |
3 | 5 | import polars as pl |
4 | 6 |
|
5 | | -from a4d.clean.patient import _apply_preprocessing |
| 7 | +from a4d.clean.patient import ( |
| 8 | + _apply_preprocessing, |
| 9 | + _fix_age_from_dob, |
| 10 | + _fix_t1d_diagnosis_age, |
| 11 | +) |
| 12 | +from a4d.config import settings |
| 13 | +from a4d.errors import ErrorCollector |
6 | 14 |
|
7 | 15 |
|
8 | 16 | class TestPatientIdNormalization: |
@@ -201,3 +209,210 @@ def test_preserve_hyphen_in_other_columns(self): |
201 | 209 | # These columns are not in the insulin list, so '-' is preserved |
202 | 210 | assert result["clinic_visit"][0] == "-" |
203 | 211 | assert result["active"][0] == "-" |
| 212 | + |
| 213 | + |
| 214 | +class TestFixAgeFromDob: |
| 215 | + """Tests for age calculation from DOB.""" |
| 216 | + |
| 217 | + def test_calculates_age_from_dob(self): |
| 218 | + """Should calculate age from DOB and tracker date.""" |
| 219 | + df = pl.DataFrame( |
| 220 | + { |
| 221 | + "patient_id": ["P001"], |
| 222 | + "age": [None], |
| 223 | + "dob": [date(2010, 6, 15)], |
| 224 | + "tracker_year": [2025], |
| 225 | + "tracker_month": [1], |
| 226 | + } |
| 227 | + ) |
| 228 | + collector = ErrorCollector() |
| 229 | + |
| 230 | + result = _fix_age_from_dob(df, collector) |
| 231 | + |
| 232 | + # 2025 - 2010 = 15, but Jan < June so 15 - 1 = 14 |
| 233 | + assert result["age"][0] == 14 |
| 234 | + |
| 235 | + def test_birthday_already_passed(self): |
| 236 | + """Should not subtract 1 if birthday already passed in tracker year.""" |
| 237 | + df = pl.DataFrame( |
| 238 | + { |
| 239 | + "patient_id": ["P001"], |
| 240 | + "age": [None], |
| 241 | + "dob": [date(2010, 3, 15)], |
| 242 | + "tracker_year": [2025], |
| 243 | + "tracker_month": [6], |
| 244 | + } |
| 245 | + ) |
| 246 | + collector = ErrorCollector() |
| 247 | + |
| 248 | + result = _fix_age_from_dob(df, collector) |
| 249 | + |
| 250 | + # 2025 - 2010 = 15, June > March so no adjustment |
| 251 | + assert result["age"][0] == 15 |
| 252 | + |
| 253 | + def test_missing_dob_keeps_null(self): |
| 254 | + """Should keep null age if DOB is missing.""" |
| 255 | + df = pl.DataFrame( |
| 256 | + { |
| 257 | + "patient_id": ["P001"], |
| 258 | + "age": [None], |
| 259 | + "dob": pl.Series([None], dtype=pl.Date), |
| 260 | + "tracker_year": [2025], |
| 261 | + "tracker_month": [1], |
| 262 | + } |
| 263 | + ) |
| 264 | + collector = ErrorCollector() |
| 265 | + |
| 266 | + result = _fix_age_from_dob(df, collector) |
| 267 | + |
| 268 | + assert result["age"][0] is None |
| 269 | + |
| 270 | + def test_error_date_dob_keeps_null(self): |
| 271 | + """Should keep null age if DOB is error date.""" |
| 272 | + error_date = date.fromisoformat(settings.error_val_date) |
| 273 | + df = pl.DataFrame( |
| 274 | + { |
| 275 | + "patient_id": ["P001"], |
| 276 | + "age": [None], |
| 277 | + "dob": [error_date], |
| 278 | + "tracker_year": [2025], |
| 279 | + "tracker_month": [1], |
| 280 | + } |
| 281 | + ) |
| 282 | + collector = ErrorCollector() |
| 283 | + |
| 284 | + result = _fix_age_from_dob(df, collector) |
| 285 | + |
| 286 | + assert result["age"][0] is None |
| 287 | + |
| 288 | + def test_corrects_wrong_excel_age(self): |
| 289 | + """Should replace wrong Excel age with calculated age.""" |
| 290 | + df = pl.DataFrame( |
| 291 | + { |
| 292 | + "patient_id": ["P001"], |
| 293 | + "age": [99.0], # Wrong value from Excel |
| 294 | + "dob": [date(2010, 6, 15)], |
| 295 | + "tracker_year": [2025], |
| 296 | + "tracker_month": [8], |
| 297 | + } |
| 298 | + ) |
| 299 | + collector = ErrorCollector() |
| 300 | + |
| 301 | + result = _fix_age_from_dob(df, collector) |
| 302 | + |
| 303 | + # Should be corrected to 15 |
| 304 | + assert result["age"][0] == 15 |
| 305 | + |
| 306 | + |
| 307 | +class TestFixT1dDiagnosisAge: |
| 308 | + """Tests for t1d_diagnosis_age calculation from DOB and diagnosis date.""" |
| 309 | + |
| 310 | + def test_calculates_diagnosis_age(self): |
| 311 | + """Should calculate age at diagnosis from DOB and diagnosis date.""" |
| 312 | + df = pl.DataFrame( |
| 313 | + { |
| 314 | + "patient_id": ["P001"], |
| 315 | + "dob": [date(2005, 8, 20)], |
| 316 | + "t1d_diagnosis_date": [date(2020, 3, 15)], |
| 317 | + "t1d_diagnosis_age": [None], |
| 318 | + } |
| 319 | + ) |
| 320 | + |
| 321 | + result = _fix_t1d_diagnosis_age(df) |
| 322 | + |
| 323 | + # 2020 - 2005 = 15, but March < August so 15 - 1 = 14 |
| 324 | + assert result["t1d_diagnosis_age"][0] == 14 |
| 325 | + |
| 326 | + def test_birthday_passed_before_diagnosis(self): |
| 327 | + """Should not subtract 1 if birthday passed before diagnosis.""" |
| 328 | + df = pl.DataFrame( |
| 329 | + { |
| 330 | + "patient_id": ["P001"], |
| 331 | + "dob": [date(2005, 3, 20)], |
| 332 | + "t1d_diagnosis_date": [date(2020, 8, 15)], |
| 333 | + "t1d_diagnosis_age": [None], |
| 334 | + } |
| 335 | + ) |
| 336 | + |
| 337 | + result = _fix_t1d_diagnosis_age(df) |
| 338 | + |
| 339 | + # 2020 - 2005 = 15, August > March so no adjustment |
| 340 | + assert result["t1d_diagnosis_age"][0] == 15 |
| 341 | + |
| 342 | + def test_missing_dob_returns_null(self): |
| 343 | + """Should return null if DOB is missing.""" |
| 344 | + df = pl.DataFrame( |
| 345 | + { |
| 346 | + "patient_id": ["P001"], |
| 347 | + "dob": pl.Series([None], dtype=pl.Date), |
| 348 | + "t1d_diagnosis_date": [date(2020, 3, 15)], |
| 349 | + "t1d_diagnosis_age": [None], |
| 350 | + } |
| 351 | + ) |
| 352 | + |
| 353 | + result = _fix_t1d_diagnosis_age(df) |
| 354 | + |
| 355 | + assert result["t1d_diagnosis_age"][0] is None |
| 356 | + |
| 357 | + def test_missing_diagnosis_date_returns_null(self): |
| 358 | + """Should return null if diagnosis date is missing.""" |
| 359 | + df = pl.DataFrame( |
| 360 | + { |
| 361 | + "patient_id": ["P001"], |
| 362 | + "dob": [date(2005, 8, 20)], |
| 363 | + "t1d_diagnosis_date": pl.Series([None], dtype=pl.Date), |
| 364 | + "t1d_diagnosis_age": [None], |
| 365 | + } |
| 366 | + ) |
| 367 | + |
| 368 | + result = _fix_t1d_diagnosis_age(df) |
| 369 | + |
| 370 | + assert result["t1d_diagnosis_age"][0] is None |
| 371 | + |
| 372 | + def test_error_date_dob_returns_null(self): |
| 373 | + """Should return null if DOB is error date.""" |
| 374 | + error_date = date.fromisoformat(settings.error_val_date) |
| 375 | + df = pl.DataFrame( |
| 376 | + { |
| 377 | + "patient_id": ["P001"], |
| 378 | + "dob": [error_date], |
| 379 | + "t1d_diagnosis_date": [date(2020, 3, 15)], |
| 380 | + "t1d_diagnosis_age": [None], |
| 381 | + } |
| 382 | + ) |
| 383 | + |
| 384 | + result = _fix_t1d_diagnosis_age(df) |
| 385 | + |
| 386 | + assert result["t1d_diagnosis_age"][0] is None |
| 387 | + |
| 388 | + def test_error_date_diagnosis_returns_null(self): |
| 389 | + """Should return null if diagnosis date is error date.""" |
| 390 | + error_date = date.fromisoformat(settings.error_val_date) |
| 391 | + df = pl.DataFrame( |
| 392 | + { |
| 393 | + "patient_id": ["P001"], |
| 394 | + "dob": [date(2005, 8, 20)], |
| 395 | + "t1d_diagnosis_date": [error_date], |
| 396 | + "t1d_diagnosis_age": [None], |
| 397 | + } |
| 398 | + ) |
| 399 | + |
| 400 | + result = _fix_t1d_diagnosis_age(df) |
| 401 | + |
| 402 | + assert result["t1d_diagnosis_age"][0] is None |
| 403 | + |
| 404 | + def test_replaces_excel_error_value(self): |
| 405 | + """Should replace Excel error (#NUM!) that became 999999 with calculated value.""" |
| 406 | + df = pl.DataFrame( |
| 407 | + { |
| 408 | + "patient_id": ["P001"], |
| 409 | + "dob": [date(2005, 8, 20)], |
| 410 | + "t1d_diagnosis_date": [date(2020, 3, 15)], |
| 411 | + "t1d_diagnosis_age": [999999], # Error value from Excel |
| 412 | + } |
| 413 | + ) |
| 414 | + |
| 415 | + result = _fix_t1d_diagnosis_age(df) |
| 416 | + |
| 417 | + # Should be calculated as 14 |
| 418 | + assert result["t1d_diagnosis_age"][0] == 14 |
0 commit comments