def test_check_if_required_keys_present_with_missing_keys(): required_keys = {"patientName", "villageNumber", "patientSex", "dob"} invalid_code = PatientValidation.check_if_required_keys_present( patient_request_body, required_keys) # invalid_code should return as a tuple that looks like: # ({'HTTP 400': 'The request body key {dob} is required.'}, 400) assert invalid_code[0]["HTTP 400"] is not None assert invalid_code[1] == 400
def post(self): logging.debug('Received request: POST api/patient/reading') try: patient_reading_data = _get_request_body() except: return {'HTTP 400': decoding_error}, 400 patient_reading_data = _get_request_body() # Ensure all data is valid abort_if_body_empty(patient_reading_data) is_invalid_patient = PatientValidation.check_patient_fields( patient_reading_data['patient']) is_invalid_reading = PatientValidation.check_reading_fields( patient_reading_data['reading']) if is_invalid_patient is not None: return is_invalid_patient # validate with new reading validator if is_invalid_reading is not None: return is_invalid_reading patient_data = patient_reading_data['patient'] if 'dob' in patient_data and patient_data[ 'dob'] and patient_data['patientAge'] is None: patient_reading_data['patient']['dob'] = int( patient_reading_data['patient']['dob']) patient_reading_data['patient'] = calculate_age_from_dob( patient_data) # create new reading (and patient if it does not already exist) reading_and_patient = readingManager.create_reading_and_patient( patient_reading_data['patient']['patientId'], patient_reading_data) # add patient to the facility of the user that took their reading user = userManager.read("id", patient_reading_data['reading']['userId']) userFacility = user['healthFacilityName'] patientFacilityManager.add_patient_facility_relationship( patient_reading_data['patient']['patientId'], userFacility) # associate new reading with patient reading_and_patient[ 'message'] = 'Patient reading created successfully!' return reading_and_patient, 201
def test_check_if_values_string_or_int_succeeds_with_string_and_int(): must_be_string = { "patientName", "gestationalAgeUnit", "medicalHistory", "drugHistory", } must_be_int = {"patientAge", "villageNumber", "gestationalAgeValue"} result = PatientValidation.check_if_values_string_or_int( patient_request_body, must_be_string, must_be_int) assert result is None
def put(self, patient_id): logging.debug('Received request: PUT /patient/' + patient_id) data = _get_request_body() patient = abort_if_patient_doesnt_exist(patient_id) invalid = PatientValidation.update_info_invalid(patient_id, data) if invalid is not None: return invalid response_body = patientManager.update("patientId", patient_id, data) return response_body, 200
def post(self): logging.debug('Received request: POST /patient') try: patient_data = self._get_request_body() except: return {'HTTP 400': decoding_error}, 400 patient_data = self._get_request_body() patient_data['dob'] = int(patient_data['dob']) # Ensure all data is valid abort_if_body_empty(patient_data) abort_if_patient_exists(patient_data['patientId']) invalid = PatientValidation.check_patient_fields(patient_data) if invalid is not None: return invalid # if age is not provided, populate age using dob if 'dob' in patient_data and patient_data[ 'dob'] and patient_data['patientAge'] is None: patient_data = calculate_age_from_dob(patient_data) response_body = patientManager.create(patient_data) return response_body, 201
def test_check_reading_fields_succeeds(): result = PatientValidation.check_reading_fields(reading_request_body) assert result is None
def test_check_patient_fields_fails(): result = PatientValidation.check_patient_fields( patient_request_body_missing_fields) assert result[1] == 400
def test_check_if_values_string_or_int_succeeds_with_int(): must_be_int = {"patientAge", "villageNumber", "gestationalAgeValue"} result = PatientValidation.check_if_values_string_or_int( patient_request_body, None, must_be_int) assert result is None
def test_check_if_required_keys_present_with_all_keys(): required_keys = {"patientName", "villageNumber", "patientSex"} valid = PatientValidation.check_if_required_keys_present( patient_request_body, required_keys) assert valid is None
def test_update_info_invalid_succeeds(): patient_id = 45464 result = PatientValidation.update_info_invalid( patient_id, patient_request_body_missing_fields) assert result[1] == 400
def test_update_info_invalid_succeeds(): patient_id = 45464 result = PatientValidation.update_info_invalid(patient_id, patient_request_body) assert result is None