def test_vaccination_events_sql(): session = make_session() session.add_all( [ # This patient is too old and should be ignored Patient( DateOfBirth="2002-05-04", Vaccinations=[ Vaccination( VaccinationName="Infanrix Hexa", VaccinationDate="2002-06-01", ) ], ), # This patient is too young and should be ignored Patient( DateOfBirth="2019-10-04", Vaccinations=[ Vaccination( VaccinationName="Infanrix Hexa", VaccinationDate="2019-11-04", ) ], ), Patient( DateOfBirth="2018-10-28", Vaccinations=[ Vaccination( VaccinationName="Infanrix Hexa", VaccinationDate="2018-11-01", ) ], MedicationIssues=[ MedicationIssue( MedicationDictionary=MedicationDictionary( DMD_ID="123", MultilexDrug_ID="123" ), ConsultationDate="2019-01-01", ), ], CodedEvents=[CodedEvent(CTV3Code="abc", ConsultationDate="2019-06-01")], ), ] ) session.commit() sql = vaccination_events_sql( date_of_birth_range=("2012-01-01", "2019-06-01"), tpp_vaccination_codelist=codelist( [("Infanrix Hexa", "dtap_hex")], system="tpp_vaccines", ), ctv3_codelist=codelist([("abc", "menb")], system="ctv3"), snomed_codelist=codelist([("123", "rotavirus")], system="snomed"), ) results = sql_to_dicts(sql) result_tuples = [(x["date_given"], x["vaccine_name"]) for x in results] # Results are ordered by patient ID but within each patient's results the # order is arbitrary. To make testing easier we sort them here. result_tuples = sorted(result_tuples) assert result_tuples == [ ("2018-11-01", "dtap_hex"), ("2019-01-01", "rotavirus"), ("2019-06-01", "menb"), ]
def test_meds_with_count(): session = make_session() asthma_medication = MedicationDictionary( FullName="Asthma Drug", DMD_ID="0", MultilexDrug_ID="0" ) patient_with_med = Patient() patient_with_med.MedicationIssues = [ MedicationIssue( MedicationDictionary=asthma_medication, ConsultationDate="2010-01-01" ), MedicationIssue( MedicationDictionary=asthma_medication, ConsultationDate="2015-01-01" ), MedicationIssue( MedicationDictionary=asthma_medication, ConsultationDate="2018-01-01" ), MedicationIssue( MedicationDictionary=asthma_medication, ConsultationDate="2020-01-01" ), ] patient_without_med = Patient() session.add(patient_with_med) session.add(patient_without_med) session.commit() study = StudyDefinition( population=patients.all(), asthma_meds=patients.with_these_medications( codelist(asthma_medication.DMD_ID, "snomed"), on_or_after="2012-01-01", return_number_of_matches_in_period=True, ), ) results = study.to_dicts() assert [x["asthma_meds"] for x in results] == ["3", "0"]
def test_meds(): session = make_session() asthma_medication = MedicationDictionary( FullName="Asthma Drug", DMD_ID="0", MultilexDrug_ID="0" ) patient_with_med = Patient() patient_with_med.MedicationIssues = [ MedicationIssue(MedicationDictionary=asthma_medication) ] patient_without_med = Patient() session.add(patient_with_med) session.add(patient_without_med) session.commit() study = StudyDefinition( population=patients.all(), asthma_meds=patients.with_these_medications( codelist(asthma_medication.DMD_ID, "snomed") ), ) results = study.to_dicts() assert [x["asthma_meds"] for x in results] == ["1", "0"]
def test_study_definition(tmp_path): session = make_session() session.add_all([ # This patient is too old and should be ignored Patient(Patient_ID=1, DateOfBirth="2002-05-04"), Patient( Patient_ID=2, DateOfBirth="2019-01-01", RegistrationHistory=[ RegistrationHistory( StartDate="2019-01-10", EndDate="9999-12-31", Organisation=Organisation(Organisation_ID=678), ), ], ), Patient( Patient_ID=3, DateOfBirth="2018-10-28", RegistrationHistory=[ RegistrationHistory( StartDate="2010-01-01", EndDate="2015-10-01", Organisation=Organisation(Organisation_ID=123), ), # Deliberately overlapping registration histories RegistrationHistory( StartDate="2015-04-01", EndDate="9999-12-31", Organisation=Organisation(Organisation_ID=345), ), ], Vaccinations=[ Vaccination( VaccinationName="Infanrix Hexa", VaccinationDate="2018-11-01", ) ], MedicationIssues=[ MedicationIssue( MedicationDictionary=MedicationDictionary( DMD_ID="123", MultilexDrug_ID="123"), ConsultationDate="2019-01-01", ), ], CodedEvents=[ CodedEvent(CTV3Code="abc", ConsultationDate="2019-06-01") ], ), ]) session.commit() study = VaccinationsStudyDefinition( start_date="2017-06-01", get_registered_practice_at_months=[12, 24, 60], tpp_vaccine_codelist=codelist( [ ("Infanrix Hexa", "dtap_hex"), ("Bexsero", "menb"), ("Rotarix", "rotavirus"), ("Prevenar", "pcv"), ("Prevenar - 13", "pcv"), ("Menitorix", "hib_menc"), ("Repevax", "dtap_ipv"), ("Boostrix-IPV", "dtap_ipv"), ("MMRvaxPRO", "mmr"), ("Priorix", "mmr"), ], system="tpp_vaccines", ), ctv3_vaccine_codelist=codelist([("abc", "menb")], system="ctv3"), snomed_vaccine_codelist=codelist([("123", "rotavirus")], system="snomed"), event_washout_period=14, vaccination_schedule=[ "dtap_hex_1", "menb_1", "rotavirus_1", "dtap_hex_2", "pcv_1", "rotavirus_2", "dtap_hex_3", "menb_2", "hib_menc_1", "pcv_2", "mmr_1", "menb_3", "dtap_ipv_1", "mmr_2", ], ) study.to_csv(tmp_path / "test.csv") with open(tmp_path / "test.csv", newline="") as f: reader = csv.DictReader(f) results = list(reader) assert results == [ { "patient_id": "2", "date_of_birth": "2019-01-01", "practice_id_at_month_12": "678", "practice_id_at_month_24": "678", "practice_id_at_month_60": "678", "dtap_hex_1": "", "menb_1": "", "rotavirus_1": "", "dtap_hex_2": "", "pcv_1": "", "rotavirus_2": "", "dtap_hex_3": "", "menb_2": "", "hib_menc_1": "", "pcv_2": "", "mmr_1": "", "menb_3": "", "dtap_ipv_1": "", "mmr_2": "", }, { "patient_id": "3", "date_of_birth": "2018-10-01", "practice_id_at_month_12": "345", "practice_id_at_month_24": "345", "practice_id_at_month_60": "345", "dtap_hex_1": "2018-11-01", "menb_1": "2019-06-01", "rotavirus_1": "2019-01-01", "dtap_hex_2": "", "pcv_1": "", "rotavirus_2": "", "dtap_hex_3": "", "menb_2": "", "hib_menc_1": "", "pcv_2": "", "mmr_1": "", "menb_3": "", "dtap_ipv_1": "", "mmr_2": "", }, ]