Beispiel #1
0
def test_has_association(patient_factory, facility_factory, user_factory):
    u1 = user_factory.create(email="u1@a")
    u2 = user_factory.create(email="u2@a")

    f1 = facility_factory.create(healthFacilityName="F1")
    f2 = facility_factory.create(healthFacilityName="F2")

    p1 = patient_factory.create(patientId="8901")
    p2 = patient_factory.create(patientId="8902")

    manager = PatientAssociationsManager()
    manager.associate(p1, f1, u1)
    manager.associate(p2, f2, u2)

    assert has_association(patient=p1, facility=f1, user=u1)
    assert has_association(patient=p2, facility=f2, user=u2)
    assert not has_association(patient=p1, facility=f2, user=u1)
    assert not has_association(patient=p2, facility=f2, user=u1)

    assert has_association(patient=p1, facility=f1)
    assert has_association(patient=p1, user=u1)
    assert has_association(facility=f1, user=u1)

    assert not has_association(patient=p2, facility=f1)
    assert not has_association(facility=f2, user=u1)
    assert not has_association(patient=p1, user=u2)
Beispiel #2
0
def test_associate_by_id_creates_association(
    patient_factory, facility_factory, user_factory
):
    u = user_factory.create(email="u@a")
    f = facility_factory.create(healthFacilityName="F")
    p = patient_factory.create(patientId="8900")

    manager = PatientAssociationsManager()
    manager.associate_by_id(p.patientId, f.healthFacilityName, u.id)

    assert patients_for_user(u) == [p]
Beispiel #3
0
def test_patients_for_vht(user_factory, facility_factory, patient_factory):
    f = facility_factory.create(healthFacilityName="F")
    u1 = user_factory.create(email="u1@a", healthFacilityName="F")
    u2 = user_factory.create(email="u2@a", healthFacilityName="F")
    p1 = patient_factory.create(patientId="9001")
    p2 = patient_factory.create(patientId="9002")

    manager = PatientAssociationsManager()
    manager.associate(p1, f, u1)
    manager.associate(p2, f, u2)

    assert filter.patients_for_vht(u1) == [p1]
Beispiel #4
0
def test_patients_at_facility_doesnt_return_duplicate_patients(
    patient_factory, facility_factory, user_factory
):
    u1 = user_factory.create(email="u1@a")
    u2 = user_factory.create(email="u2@a")

    f = facility_factory.create(healthFacilityName="F")

    p = patient_factory.create(patientId="8900")

    manager = PatientAssociationsManager()
    manager.associate(p, f, u1)
    manager.associate(p, f, u2)

    assert patients_at_facility(f) == [p]
Beispiel #5
0
def test_patients_for_user_doesnt_return_duplicate_patients(
    patient_factory, facility_factory, user_factory
):
    u = user_factory.create(email="u@a")

    f1 = facility_factory.create(healthFacilityName="F1")
    f2 = facility_factory.create(healthFacilityName="F2")

    p = patient_factory.create(patientId="8900")

    manager = PatientAssociationsManager()
    manager.associate(p, f1, u)
    manager.associate(p, f2, u)

    assert patients_for_user(u) == [p]
Beispiel #6
0
def test_patients_for_cho(database, user_factory, facility_factory,
                          patient_factory):
    f = facility_factory.create(healthFacilityName="F")
    u1 = user_factory.create(email="u1@a", healthFacilityName="F")
    u2 = user_factory.create(email="u2@a", healthFacilityName="F")
    p1 = patient_factory.create(patientId="9001")
    p2 = patient_factory.create(patientId="9002")

    u1.vhtList.append(u2)
    database.session.commit()

    manager = PatientAssociationsManager()
    manager.associate(p1, f, u1)
    manager.associate(p2, f, u2)

    assert filter.patients_for_cho(u1) == [p1, p2]

    # Need to manually clean up this relation so that the factories can clean up their
    # objects as this table is not annotated with cascade=delete.
    u1.vhtList = []
    database.session.commit()
Beispiel #7
0
def test_patients_for_user_only_returns_patients_associated_with_user(
    patient_factory, facility_factory, user_factory
):
    u1 = user_factory.create(email="u1@a")
    u2 = user_factory.create(email="u2@a")

    f = facility_factory.create(healthFacilityName="F")

    p1 = patient_factory.create(patientId="8901")
    p2 = patient_factory.create(patientId="8902")
    p3 = patient_factory.create(patientId="8903")

    manager = PatientAssociationsManager()
    manager.associate(p1, f, u1)
    manager.associate(p2, f, u2)
    manager.associate(p3, f, u1)

    assert patients_for_user(u1) == [p1, p3]
    assert patients_for_user(u2) == [p2]
Beispiel #8
0
def test_patients_at_facility_only_returns_patients_associated_with_facility(
    patient_factory, facility_factory, user_factory
):
    u = user_factory.create(email="u@a")

    f1 = facility_factory.create(healthFacilityName="F1")
    f2 = facility_factory.create(healthFacilityName="F2")

    p1 = patient_factory.create(patientId="8901")
    p2 = patient_factory.create(patientId="8902")
    p3 = patient_factory.create(patientId="8903")

    manager = PatientAssociationsManager()
    manager.associate(p1, f1, u)
    manager.associate(p2, f2, u)
    manager.associate(p3, f1, u)

    assert patients_at_facility(f1) == [p1, p3]
    assert patients_at_facility(f2) == [p2]
    def create_referral_with_patient_and_reading(self, req_data, user_id):
        # if the patient is already created, dont create,
        try:
            validator.exists(Patient, "patientId", req_data["patient"]["patientId"])
        except Exception as e:
            print("patient does not exist yet, creating")
            # do validation here
            created_patient = patientManager.create(req_data["patient"])
            print("created_patient: ")
            pprint(created_patient)

        # if the reading already created, dont create,
        # else use the patientId and create new reading
        try:
            validator.exists(Reading, "readingId", req_data["reading"]["readingId"])
        except Exception as e:
            print("reading does not exist yet, creating")
            req_data["reading"]["patientId"] = req_data["patient"]["patientId"]
            created_reading = readingManager.create_reading(req_data["reading"])
            print("created_reading: ")
            pprint(created_reading)

        # if the health facility is created, dont create, else use the
        # healthFacilityName to create a new health facility
        try:
            validator.exists(
                HealthFacility, "healthFacilityName", req_data["healthFacilityName"]
            )
        except Exception as e:
            print("healthFacility doesnt exist, creating")
            created_hf = healthFacilityManager.create(
                {"healthFacilityName": req_data["healthFacilityName"]}
            )
            print("created health facility: ")
            pprint(created_hf)

        # add patient facility relationship
        patient_id = req_data["patient"]["patientId"]
        facility_name = req_data["healthFacilityName"]
        PatientAssociationsManager().associate_by_id(patient_id, facility_name, user_id)

        def build_ref_dict(ref_json):
            ref_dict = {}
            ref_dict["patientId"] = ref_json["patient"]["patientId"]
            ref_dict["readingId"] = ref_json["reading"]["readingId"]
            ref_dict["dateReferred"] = ref_json["date"]
            ref_dict["referralHealthFacilityName"] = ref_json["healthFacilityName"]
            ref_dict["comment"] = ref_json["comment"]
            return ref_dict

        referral_data = build_ref_dict(req_data)

        print("referral_data: ")
        pprint(referral_data)

        # validate new referral
        try:
            validator.enforce_required(referral_data)
            validator.validate(referral_data)
        except Exception as e:
            print(e)
            abort(400, message=str(e))

        created_res = self.create(referral_data)
        return referral_data