def test_post_facility(facility_name, health_facility, api_post):
    try:
        response = api_post(endpoint="/api/facilities", json=health_facility)
        assert response.status_code == 201
        assert crud.read(HealthFacility,
                         healthFacilityName=facility_name) is not None

    finally:
        crud.delete_by(HealthFacility, healthFacilityName=facility_name)
示例#2
0
def test_create_patient_with_nested_readings(database, api_post):
    patient_id = "5390160146141"
    reading_ids = [
        "65acfe28-b0d6-4a63-a484-eceb3277fb4e",
        "90293637-d763-494a-8cc7-85a88d023f3e",
    ]
    p = __make_patient(patient_id, reading_ids)
    response = api_post(endpoint="/api/patients", json=p)
    database.session.commit()

    try:
        assert response.status_code == 201
        assert crud.read(Patient, patientId=patient_id) is not None

        for r in reading_ids:
            reading = crud.read(Reading, readingId=r)
            assert reading is not None
            assert reading.trafficLightStatus == TrafficLightEnum.GREEN
    finally:
        for r in reading_ids:
            crud.delete_by(Reading, readingId=r)
        crud.delete_by(Patient, patientId=patient_id)
def test_post_reading_with_referral(reading_id, reading_referral_followup,
                                    patient_factory, api_post):
    patient_factory.create(patientId="123")
    # Remove the followup
    del reading_referral_followup["followup"]
    try:
        response = api_post(endpoint="/api/readings",
                            json=reading_referral_followup)
        assert response.status_code == 201
        assert crud.read(Reading, readingId=reading_id) is not None
        assert crud.read(Referral, readingId=reading_id) is not None
        assert crud.read(FollowUp, readingId=reading_id) is None

        # Reading should have it's traffic light computed
        reading = crud.read(Reading, readingId=reading_id)
        assert reading.trafficLightStatus.value == "GREEN"

        # Referral should be marked as not assessed
        referral = crud.read(Referral, readingId=reading_id)
        assert not referral.isAssessed
    finally:
        crud.delete_by(Referral, readingId=reading_id)
        crud.delete_by(Reading, readingId=reading_id)
示例#4
0
def test_get_mobile_patient(database, api_post, api_get):
    patient_ids = []
    reading_ids = []
    try:
        p = __make_patient("222266667",
                           ["893ddaad-1ebd-46fb-928b-ad0640115aa1"])
        patient_ids.append("222266667")
        reading_ids.append("893ddaad-1ebd-46fb-928b-ad0640115aa1")
        response = api_post(endpoint="/api/patients", json=p)
        database.session.commit()
        assert response.status_code == 201

        # Make the patient IDs so that they're on both sides of the patient IDs.
        p = __make_full_patient("9999",
                                ["7f60bbb3-c49d-425f-825c-681c8330b61d"])
        patient_ids.append("9999")
        reading_ids.append("7f60bbb3-c49d-425f-825c-681c8330b61d")
        response = api_post(endpoint="/api/patients", json=p)
        database.session.commit()
        assert response.status_code == 201

        # Make the patient IDs so that they're on both sides of the patient IDs
        p = __make_full_patient("999955551",
                                ["978e870e-c542-428a-a8bf-dabb0e52bff3"])
        patient_ids.append("999955551")
        reading_ids.append("978e870e-c542-428a-a8bf-dabb0e52bff3")
        response = api_post(endpoint="/api/patients", json=p)
        database.session.commit()
        assert response.status_code == 201

        for p in patient_ids:
            patient = crud.read(Patient, patientId=p)
            assert patient is not None

        # Add a more fleshed-out reading to the first patient.
        reading = __make_reading(
            reading_id="123dabdd-5des-7ufh-23fd-qd4308143651",
            patient_id=patient_ids[0])
        reading_ids.append("123dabdd-5des-7ufh-23fd-qd4308143651")
        reading_response = api_post(endpoint="/api/readings", json=reading)
        database.session.commit()
        assert reading_response.status_code == 201

        # Add a more minimal reading to the first patient.
        reading = __make_reading_no_extra_vitals(
            reading_id="526292b7-53d0-4e7e-8a96-f66f061477ff",
            patient_id=patient_ids[0])
        reading_ids.append("526292b7-53d0-4e7e-8a96-f66f061477ff")
        reading_response = api_post(endpoint="/api/readings", json=reading)
        database.session.commit()
        assert reading_response.status_code == 201

        # Add another fleshed-out reading to the first patient.
        reading = __make_reading(
            reading_id="2ab4f830-3cc0-4e98-bff3-174a9dcc630a",
            patient_id=patient_ids[0])
        reading_ids.append("2ab4f830-3cc0-4e98-bff3-174a9dcc630a")
        reading_response = api_post(endpoint="/api/readings", json=reading)
        database.session.commit()
        assert reading_response.status_code == 201

        for r in reading_ids:
            reading = crud.read(Reading, readingId=r)
            assert reading is not None

        # Get all the patients from /api/mobile/patients.
        responseMobile = api_get(endpoint="/api/mobile/patients")
        assert responseMobile.status_code == 200

        # Setup an error message to return when an assert fails.
        # Note: Since this is usually tested with the test seed data, there will
        # be more than just 1 patient here.
        patient_number_info = (
            f"There were {len(responseMobile.json())} patients " +
            "returned by api/mobile/patients. Dumping them all now:\n" +
            pformat(responseMobile.json(), width=48) + "\n" +
            "========================================================")

        # Loop through every single patient in the database.
        # For every patient in the database (as admin user, /api/mobile/patients returns
        # all the patients), get the patient info from the /api/patients/:id endpont
        # and then determine if they match.
        for patient_from_mobile_patients in responseMobile.json():
            patient_id = patient_from_mobile_patients["patientId"]
            # Validate that the GET requests for /api/patients/{patient_id} and
            # /api/mobile/patients give back the same information.
            # We first validate that the patient info returned is consistent.

            # Get the patient from the /api/patients/:id endpoint.
            response_patients_get = api_get(
                endpoint=f"/api/patients/{patient_id}")
            assert response_patients_get.status_code == 200
            patient_from_patients_api = response_patients_get.json()

            # Check that both results are basically equal.
            __assert_dicts_are_equal(
                patient_from_mobile_patients,
                patient_from_patients_api,
                f"patient {patient_id} from api/mobile/patients",
                f"patient {patient_id} from api/patients/:id",
                other_error_messages=patient_number_info,
                # Validate the readings later.
                ignored_keys=["readings"],
            )

            # Validate the readings now. We check that they both have the same readings.
            # Loop through both of them in case one readings list is different from the other.
            for readingFromMobile in patient_from_mobile_patients["readings"]:
                # From the reading from the api/mobile/patients, find the corresponding reading
                # from the api/patients/:id endpoint
                current_reading_id = readingFromMobile["readingId"]
                readingFromNormalApi = [
                    r for r in patient_from_patients_api["readings"]
                    if r["readingId"] == current_reading_id
                ][0]
                # Check that they give the exact same information.
                __assert_dicts_are_equal(
                    readingFromMobile,
                    readingFromNormalApi,
                    f"reading {current_reading_id} from api/mobile/patients",
                    f"reading {current_reading_id} from api/patients/:id",
                    other_error_messages=patient_number_info,
                    ignored_keys=["userId"],
                )
            for readingFromNormalApi in patient_from_patients_api["readings"]:
                # From the reading from the api/patients/:id, find the corresponding reading
                # from the api/mobile/patients endpoint
                current_reading_id = readingFromNormalApi["readingId"]
                readingFromMobile = [
                    r for r in patient_from_mobile_patients["readings"]
                    if r["readingId"] == current_reading_id
                ][0]
                # Check that they give the exact same information.
                __assert_dicts_are_equal(
                    readingFromMobile,
                    readingFromNormalApi,
                    f"reading {current_reading_id} from api/mobile/patients",
                    f"reading {current_reading_id} from api/patients/:id",
                    other_error_messages=patient_number_info,
                    ignored_keys=["userId"],
                )

    finally:
        for r in reading_ids:
            crud.delete_by(Reading, readingId=r)
        for p in patient_ids:
            crud.delete_by(Patient, patientId=p)
示例#5
0
def test_delete_by(patient_factory):
    patient_factory.create(patientId="abc")
    crud.delete_by(Patient, patientId="abc")
    assert not crud.read(Patient, patientId="abc")
示例#6
0
def test_vht_referring_new_patient_and_hcw_assessing_them(
    single_facility_actors, api, make_patient, make_assessment
):
    facility, _, hcw, _, vht = single_facility_actors

    patient_id = "35164646134547"
    reading_id = "0332bf3a-8dcd-4062-8afa-0cb374571bcb"

    try:
        # Create a new patient, reading, and referral as a VHT
        patient_dict = make_patient(
            patient_id=patient_id,
            reading_id=reading_id,
            refer_to=facility.healthFacilityName,
            created_by=vht.id,
        )
        response = api.post(
            endpoint="/api/patients",
            payload=patient_dict,
            email=vht.email,
            password="******",
        )
        assert response.status_code == 201

        # May need to force the database into committing changes
        data.db_session.commit()

        # Ensure that the patient, reading, and referral have been created
        referral = crud.read(Referral, readingId=reading_id)

        assert crud.read(Patient, patientId=patient_id) is not None
        assert crud.read(Reading, readingId=reading_id) is not None
        assert referral is not None

        # The referral should not be marked as assessed yet
        assert not referral.isAssessed

        # The patient should have an association with the facility and an association
        # with the VHT who created it
        assert assoc.has_association_by_id(
            patient_id=patient_id, facility_name=facility.healthFacilityName
        )
        assert assoc.has_association_by_id(patient_id=patient_id, user_id=vht.id)

        # Assess the patient as the health care worker
        assessment_dict = make_assessment(reading_id, assessed_by=hcw.id)
        response = api.post(
            endpoint="/api/assessments",
            payload=assessment_dict,
            email=hcw.email,
            password="******",
        )
        assert response.status_code == 201

        data.db_session.commit()

        # Ensure that the assessment has been created and that the referral has been
        # marked as assessed
        assert crud.read(FollowUp, readingId=reading_id) is not None
        assert referral.isAssessed

    finally:
        # Cleanup
        crud.delete_by(FollowUp, readingId=reading_id)
        crud.delete_by(Referral, readingId=reading_id)
        crud.delete_by(Reading, readingId=reading_id)
        crud.delete_by(Patient, patientId=patient_id)
        data.db_session.commit()