def test_form_to_api_format_case_3(self): mapper = {"storage": "1", "location": "2", "number_of_files": "3"} form_format = { "storage": { "location": "C:/Documents", "number_of_files": 1000 } } expected_api_format = [{ "prop": "1", "value": [ { "prop": "2", "value": "C:/Documents" }, { "prop": "3", "value": 1000 }, ], }] c = FormatConverter(key_name="prop", value_name="value", mapper=mapper) actual_api_format = c.add_form_format(form_format).get_api_format() self.assertEqual(expected_api_format, actual_api_format)
def test_form_to_api_format_case_2(self): mapper = {"username": "******"} form_format = {"username": ["Edward", "Annie"]} expected_api_format = [{"prop": "1", "value": ["Edward", "Annie"]}] c = FormatConverter(key_name="prop", value_name="value", mapper=mapper) actual_api_format = c.add_form_format(form_format).get_api_format() self.assertEqual(expected_api_format, actual_api_format)
def upload_study_related_entity(data, url, method, property_url, headers): """Send data to the API (study related entity) in form format""" entry_format = data.pop("entry_format", "form") # Format data (cleaning + conversion from "form format" to "api format") if entry_format == "form": prop_name_to_id = map_key_value(property_url, key="name", value="id") converter = FormatConverter(mapper=prop_name_to_id) converter.add_form_format(data["entries"]) elif entry_format == "api": prop_id_to_name = map_key_value(property_url, key="id", value="name") converter = FormatConverter(mapper=prop_id_to_name) converter.add_api_format(data["entries"]) converter.clean_data() data["entries"] = converter.get_api_format() data["entry_format"] = "api" # Send data to API if method == "post": res = requests.post(url=url, json=data, headers=headers) if res.status_code != 201: message = f"Failed to POST study related entity. {res.json()}" success = False else: message = "Succeded to POST study related entity" success = True elif method == "put": res = requests.put(url=url, json=data, headers=headers) if res.status_code != 200: message = f"Failed to PUT study related entity. {res.json()}" success = False else: message = "Succeded to PUT study related entity" success = True return (res.json(), message, success)
def test_form_to_api_format_case_4(self): mapper = {"contacts": "1", "name": "3", "phone": "4"} form_format = { "contacts": [ { "name": "Edward", "phone": "903 367 2072" }, { "name": "Annie", "phone": "731 222 8842" }, ] } actual_api_format = [{ "prop": "1", "value": [ [ { "prop": "3", "value": "Edward" }, { "prop": "4", "value": "903 367 2072" }, ], [ { "prop": "3", "value": "Annie" }, { "prop": "4", "value": "731 222 8842" }, ], ], }] c = FormatConverter(key_name="prop", value_name="value", mapper=mapper) expected_api_format = c.add_form_format(form_format).get_api_format() self.assertEqual(expected_api_format, actual_api_format)