def testClosedCases(self): # run through all four forms, verify that the case closed at the clinic # behaves properly folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "closed_cases") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_general.xml")) [case] = updated_patient.cases self.assertTrue(case.closed) self.assertEqual("tb", case.type) self.assertEqual(Outcome.CLOSED_AT_CLINIC, case.outcome) self.assertEqual(0, len(case.commcare_cases)) updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_sick_pregnancy.xml")) [_, case] = updated_patient.cases self.assertTrue(case.closed) self.assertEqual("malaria", case.type) self.assertEqual(Outcome.CLOSED_AT_CLINIC, case.outcome) self.assertEqual(0, len(case.commcare_cases)) updated_patient, form_doc3 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "003_underfive.xml")) [_, _, case] = updated_patient.cases self.assertTrue(case.closed) self.assertEqual("meningitis", case.type) self.assertEqual(Outcome.CLOSED_AT_CLINIC, case.outcome) self.assertEqual(0, len(case.commcare_cases))
def testFirstVisitSick(self): # initial visit sick folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "pregnancy_sickvisit") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_sick_pregnancy.xml")) self.assertEqual(1, len(updated_patient.pregnancies)) [preg] = updated_patient.pregnancies self.assertEqual(date(2011,1,5), preg.edd) self.assertEqual(form_doc1.get_id, preg.anchor_form_id) self.assertEqual(0, len(preg.other_form_ids)) # add a healthy visit after, should be part of the same pregnancy updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "003_pregnancy.xml")) [preg] = updated_patient.pregnancies self.assertEqual(date(2011,1,5), preg.edd) self.assertEqual(form_doc1.get_id, preg.anchor_form_id) self.assertEqual(1, len(preg.other_form_ids)) self.assertEqual(form_doc2.get_id, preg.other_form_ids[0]) # add a healthy visit before, should be part of the same pregnancy, updated_patient, form_doc3 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_pregnancy.xml")) [preg] = updated_patient.pregnancies self.assertEqual(date(2011,1,5), preg.edd) # should not change anchor self.assertEqual(form_doc1.get_id, preg.anchor_form_id) self.assertEqual(2, len(preg.other_form_ids))
def testSickPregnancyPhoneCaseGeneration(self): # Sick pregnancy folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "sick_pregnancy") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) # forms are loaded one at a time. If you need to run tests at # intermediate states, put them in between whatever forms you # want loaded # a. Severe symptom updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_sick_pregnancy.xml")) self.assertEqual(1, len(updated_patient.cases)) case = updated_patient.cases[-1] self.assertTrue(case.send_to_phone) self.assertTrue("severe_symptom_checked" in case.send_to_phone_reason) # b. Missed appointment < 5 days updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_sick_pregnancy.xml")) self.assertEqual(2, len(updated_patient.cases)) case = updated_patient.cases[-1] self.assertTrue(case.send_to_phone) self.assertEqual("urgent_clinic_followup", case.send_to_phone_reason) # c. None of the above (no case) updated_patient, form_doc3 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "003_sick_pregnancy.xml")) self.assertEqual(3, len(updated_patient.cases)) case = updated_patient.cases[-1] self.assertFalse(case.send_to_phone) self.assertEqual("sending_criteria_not_met", case.send_to_phone_reason)
def _run_phone_visit_case_test(self, folder_name, type): patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) # a. Severe symptom updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_%s.xml" % type)) self.assertEqual(1, len(updated_patient.cases)) case = updated_patient.cases[-1] self.assertTrue(case.send_to_phone) self.assertTrue("severe_symptom_checked" in case.send_to_phone_reason) # b. Danger sign updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_%s.xml" % type)) self.assertEqual(2, len(updated_patient.cases)) case = updated_patient.cases[-1] self.assertTrue(case.send_to_phone) self.assertEqual("danger_sign_present", case.send_to_phone_reason) # c. Missed appointment < 5 days updated_patient, form_doc3 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "003_%s.xml" % type)) self.assertEqual(3, len(updated_patient.cases)) case = updated_patient.cases[-1] self.assertTrue(case.send_to_phone) self.assertEqual("urgent_clinic_followup", case.send_to_phone_reason) # d. None of the above (no case) updated_patient, form_doc4 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "004_%s.xml" % type)) self.assertEqual(4, len(updated_patient.cases)) case = updated_patient.cases[-1] self.assertFalse(case.send_to_phone) self.assertEqual("sending_criteria_not_met", case.send_to_phone_reason)
def testHugeFollow(self): folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "huge_follow") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) # these all have huge follow up dates, so these functions not throwing errors # is enough to test. updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_delivery.xml")) updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_general.xml")) updated_patient, form_doc3 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "003_sick_pregnancy.xml"))
def testDeathAfter(self): folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "death_after") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) self.assertFalse(patient.is_deceased) # enter a form with outcome death updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_general.xml")) # enter a form with a different outcome that should create a case updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_general.xml")) # make sure the second case is closed and the outcome is also death self.assertTrue(updated_patient.is_deceased) self.assertEqual(2, len(updated_patient.cases)) for case in updated_patient.cases: self.assertTrue(case.closed) self.assertEqual(Outcome.PATIENT_DIED, case.outcome)
def testSingleSickVisitOpenClose(self): folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "open_close_preg") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_sick_pregnancy.xml")) [preg] = updated_patient.pregnancies [case] = updated_patient.cases self.assertTrue(case.closed) self.assertEqual(Outcome.FETAL_DEATH, case.outcome)
def testPostSaveDrugBug(self): self.assertEqual(0, len(ExceptionRecord.view("couchlog/all_by_date", include_docs=True).all())) folder_name = os.path.join(os.path.dirname(__file__), "testdata", "test_post_save_drug_bug") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_underfive.xml")) self.assertEqual(0, len(ExceptionRecord.view("couchlog/all_by_date", include_docs=True).all()))
def testDeliveryPhoneCaseGeneration(self): # Delivery folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "delivery_tester") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) # a. Missed appointment < 5 days updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_delivery.xml")) self.assertEqual(1, len(updated_patient.cases)) case = updated_patient.cases[-1] self.assertTrue(case.send_to_phone) self.assertEqual("urgent_clinic_followup", case.send_to_phone_reason) # b. None of the above (no case) updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_delivery.xml")) self.assertEqual(2, len(updated_patient.cases)) case = updated_patient.cases[-1] self.assertFalse(case.send_to_phone) self.assertEqual("sending_criteria_not_met", case.send_to_phone_reason)
def testImportFromFolder(self): folder_name = os.path.join(os.path.dirname(__file__), "testdata", "export_test") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) # forms are loaded one at a time. If you need to run tests at # intermediate states, put them in between whatever forms you # want loaded self.assertEqual("EXPORT", patient.first_name) self.assertEqual("TEST", patient.last_name) self.assertEqual("f", patient.gender) self.assertEqual(0, len(patient.encounters)) updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_pregnancy.xml")) updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_sick_pregnancy.xml")) updated_patient, form_doc3 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "003_general.xml")) # custom test conditions after all forms are loaded go here self.assertEqual(3, len(updated_patient.encounters))
def testReturnToClinicSupercedes(self): folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "ltfu_test2") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_general.xml")) updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_general.xml")) updated_patient, form_doc3 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "003_general.xml")) reprocess(updated_patient.get_id) updated_patient = CPatient.get(updated_patient.get_id) self.assertEqual(3, len(updated_patient.cases)) [c1, c2, c3] = sorted(updated_patient.cases, key=lambda case: case.opened_on) # after reprocessing the first case should be closed with returned to clinic, # but the second should be ltfu self.assertTrue(c1.closed) self.assertEqual(const.Outcome.RETURNED_TO_CLINIC, c1.outcome) self.assertTrue(c2.closed) self.assertEqual(const.Outcome.LOST_TO_FOLLOW_UP, c2.outcome)
def testOutOfOrderLoss(self): folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "ltfu_test2") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) # make sure we properly handle when the forms come out of order updated_patient, form_doc3 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "003_general.xml")) updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_general.xml")) [c2, c3] = sorted(updated_patient.cases, key=lambda case: case.opened_on) self.assertTrue(c2.closed) self.assertEqual(const.Outcome.LOST_TO_FOLLOW_UP, c2.outcome) updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_general.xml")) [c1, c2, c3] = sorted(updated_patient.cases, key=lambda case: case.opened_on) self.assertTrue(c1.closed) self.assertEqual(const.Outcome.RETURNED_TO_CLINIC, c1.outcome)
def testDeath(self): folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "death_test") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) self.assertFalse(patient.is_deceased) updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_general.xml")) self.assertTrue(updated_patient.is_deceased) [case] = updated_patient.cases self.assertTrue(case.closed) self.assertTrue(updated_patient.is_deceased) self.assertEqual("cardiac_failure", case.type) self.assertEqual(Outcome.PATIENT_DIED, case.outcome) self.assertEqual(0, len(case.commcare_cases))
def testHivPi(self): folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "hiv_pitest") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) # first visit has a recent test so it doesn't count to denominator updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_general.xml")) [res] = get_db().view(const.get_view_name("adult_pi"), group=True, group_level=4, key=[2011,4, "9999999", "hiv_test"]).all() num, denom = res["value"] self.assertEqual(0, num) self.assertEqual(0, denom) updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_general.xml")) # second visit has no recent test so it counts [res] = get_db().view(const.get_view_name("adult_pi"), group=True, group_level=4, key=[2011,5, "9999999", "hiv_test"]).all() num, denom = res["value"] self.assertEqual(0, num) self.assertEqual(1, denom)
def testLostImmediatelyCloses(self): """ Creates a case that is too long ago and is lost and therefore is closed as such. """ folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "ltfu_test") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) updated_patient, _ = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_general.xml")) [case] = updated_patient.cases self.assertTrue(case.closed) self.assertTrue(case.outcome.startswith("lost_to_followup")) self.assertEqual(case.closed_on.date(), case.ltfu_date) [ccase] = case.commcare_cases self.assertTrue(ccase.closed)
def testFansidar(self): folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "fansidar") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) def _latest_pregnancy(): return PregnancyReportRecord.view("reports/pregnancies_for_patient", key=updated_patient.get_id, include_docs=True).one() # dose 1 updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_pregnancy.xml")) pregnancy = _latest_pregnancy() self.assertFalse(pregnancy.eligible_three_doses_fansidar) self.assertFalse(pregnancy.got_three_doses_fansidar) # dose 2 updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_pregnancy.xml")) pregnancy = _latest_pregnancy() self.assertFalse(pregnancy.eligible_three_doses_fansidar) self.assertFalse(pregnancy.got_three_doses_fansidar) # sick visit, doesn't count updated_patient, form_doc3 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "003_sick_pregnancy.xml")) pregnancy = _latest_pregnancy() self.assertFalse(pregnancy.eligible_three_doses_fansidar) self.assertFalse(pregnancy.got_three_doses_fansidar) # healthy visit, no fansidar count updated_patient, form_doc4 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "004_pregnancy.xml")) pregnancy = _latest_pregnancy() self.assertTrue(pregnancy.eligible_three_doses_fansidar) self.assertFalse(pregnancy.got_three_doses_fansidar) # healthy visit fansidar updated_patient, form_doc5 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "005_pregnancy.xml")) pregnancy = _latest_pregnancy() self.assertTrue(pregnancy.eligible_three_doses_fansidar) self.assertTrue(pregnancy.got_three_doses_fansidar)
def testHealthyPregnancyMisingEDD(self): folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "edd_conditions") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) updated_patient, form_doc4 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "004_pregnancy.xml")) [preg] = updated_patient.pregnancies self.assertEqual(None, preg.edd)
def testSickPregnancyGestationalAge(self): folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "edd_conditions") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) updated_patient, form_doc5 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "005_sick_pregnancy.xml")) [preg] = updated_patient.pregnancies self.assertEqual(date(2011,3,21), preg.edd)