test_transform_patients.py 3.16 KB
Newer Older
Venkat Malladi's avatar
Venkat Malladi committed
1
#!/usr/bin/env python3
Venkat Malladi's avatar
Venkat Malladi committed
2
3
4

import pytest
import pandas as pd
Venkat Malladi's avatar
Venkat Malladi committed
5
from io import StringIO
Venkat Malladi's avatar
Venkat Malladi committed
6
7
8
9
10
11
12
13
import transform_patients


DEMOGRAPHIC_STRING = """MRN,PAT_ID,PAT_LAST_NAME,PAT_FIRST_NAME,GENDER,BIRTH_DATE,DEATH_DATE,DEATH_SOURCE,RACE,ETHNICITY,LAST_ALIVE,LAST_MED,LAST_LAB,SURGERY_DATE,MAX_SURGERY_DATE,MAX_METS_DATE
934,Z583990,Smith,Erin,FEMALE,27-JUN-1957,,,AmIndAN,@,22-MAY-2013,25-DEC-2010,13-JUN-2017,27-NOV-2017,27-NOV-2017,15-DEC-2012
"""


Venkat Malladi's avatar
Venkat Malladi committed
14
15
16
DEMO_MERGED_STRING = """MRN,GENDER,ETHNICITY,RACE,BIRTH_DATE,DEATH_DATE,DEATH_SOURCE,Date of Last Contact-Date,Vital Status
934,FEMALE,AmIndAN,@,27-JUN-1957,11/27/2017,EPIC,,
822,FEMALE,White,@,29-JUL-1913,,,03/10/2015,0
Venkat Malladi's avatar
Venkat Malladi committed
17
18
19
20
21
22
23
24
25
26
27
"""


@pytest.fixture
def demographic():
    demographics_file = StringIO(DEMOGRAPHIC_STRING)
    demographics_df = pd.read_csv(demographics_file, index_col='MRN')
    return demographics_df


@pytest.fixture
Venkat Malladi's avatar
Venkat Malladi committed
28
def demographic_death():
Venkat Malladi's avatar
Venkat Malladi committed
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
    merged_file = StringIO(DEMO_MERGED_STRING)
    merged_df = pd.read_csv(merged_file)
    return merged_df


@pytest.fixture
def demographic_1(demographic):
    demographic.loc[934, 'GENDER'] = 'UNKNOWN'
    return demographic


@pytest.fixture
def demographic_2(demographic):
    demographic.loc[934, 'RACE'] = 'HawPacIs'
    return demographic


@pytest.mark.unit
def test_check_title_case_gender(demographic):
    title_case_gender = transform_patients.convert_gender(demographic)
    assert title_case_gender.loc[934,'GENDER'] == 'Female'


@pytest.mark.unit
def test_check_title_case_gender_unknown(demographic_1):
    title_case_gender_unknown = transform_patients.convert_gender(demographic_1)
    assert title_case_gender_unknown.loc[934,'GENDER'] == 'Unknown'


@pytest.mark.unit
def test_check_ethnicity(demographic):
    check_ethnicity = transform_patients.convert_ethnicity(demographic)
    assert check_ethnicity.loc[934,'ETHNICITY'] == 'Unknown'


@pytest.mark.unit
def test_check_race_amindan(demographic):
    check_race_amindan = transform_patients.convert_race(demographic)
    assert check_race_amindan.loc[934,'RACE'] == 'American Indian'


@pytest.mark.unit
def test_check_race_hawpacis(demographic_2):
    check_race_hawpacis = transform_patients.convert_race(demographic_2)
    assert check_race_hawpacis.loc[934,'RACE'] == 'Hawaiian Pacific Islander'


76
77
@pytest.mark.unit
def test_check_race_unknown(demographic_2):
Venkat Malladi's avatar
Venkat Malladi committed
78
    demographic_2.loc[934, 'RACE'] = 'Unknown'
79
    check_race_hawpacis = transform_patients.convert_race(demographic_2)
Venkat Malladi's avatar
Venkat Malladi committed
80
    assert check_race_hawpacis.loc[934,'RACE'] == 'Unknown'
81
82


Venkat Malladi's avatar
Venkat Malladi committed
83
@pytest.mark.unit
Venkat Malladi's avatar
Venkat Malladi committed
84
85
86
87
def test_check_shift(demographic_death):
    check_shift = transform_patients.calculate_shift(demographic_death)
    check_shift['DEATH_DATE'] = check_shift['DEATH_DATE'].astype(str)
    assert check_shift.loc[0,'DEATH_DATE'] == '1860-06-03'
Venkat Malladi's avatar
Venkat Malladi committed
88
89
90
91
    assert check_shift.loc[0,'Shift'] == -4969728000.0


@pytest.mark.unit
Venkat Malladi's avatar
Venkat Malladi committed
92
93
94
95
96
def test_check_shift_tumor_registry(demographic_death):
    check_shift_tumor_registry = transform_patients.calculate_shift(demographic_death)
    check_shift_tumor_registry['DEATH_DATE'] = check_shift_tumor_registry['DEATH_DATE'].astype(str)
    assert check_shift_tumor_registry.loc[1,'DEATH_DATE'] == '1901-08-14'
    assert check_shift_tumor_registry.loc[1,'Shift'] == -3583958400.0