From 242f4258a39002bb3762b604bb461e5c606fa5a3 Mon Sep 17 00:00:00 2001 From: TraciebelWairimu Date: Fri, 1 Mar 2024 17:16:27 +0300 Subject: [PATCH 1/3] Fix organizations_min.csv, locations_min.csv build payload --- .../csv/organizations/organizations_min.csv | 4 +- importer/main.py | 37 ++++++++++++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/importer/csv/organizations/organizations_min.csv b/importer/csv/organizations/organizations_min.csv index bdb940b4..48735085 100644 --- a/importer/csv/organizations/organizations_min.csv +++ b/importer/csv/organizations/organizations_min.csv @@ -1,2 +1,2 @@ -name -Min Organization \ No newline at end of file +name,active +Min Organization,true \ No newline at end of file diff --git a/importer/main.py b/importer/main.py index a7246942..334aebd1 100644 --- a/importer/main.py +++ b/importer/main.py @@ -231,9 +231,13 @@ def create_user_resources(user_id, user): # custom extras for organizations def organization_extras(resource, payload_string): - _, active, *_, alias = resource try: - if alias: + _, active, *_, alias = resource + except ValueError: + active = resource[1] + alias = "alias" + try: + if alias and alias != "alias": payload_string = payload_string.replace("$alias", alias) else: obj = json.loads(payload_string) @@ -253,9 +257,17 @@ def organization_extras(resource, payload_string): # custom extras for locations def location_extras(resource, payload_string): - name, *_, parentName, parentID, type, typeCode, physicalType, physicalTypeCode = resource try: - if parentName: + name, *_, parentName, parentID, type, typeCode, physicalType, physicalTypeCode = resource + except ValueError: + parentName = "parentName" + type = "type" + typeCode = "typeCode" + physicalType = "physicalType" + physicalTypeCode = "physicalTypeCode" + + try: + if parentName and parentName != "parentName": payload_string = payload_string.replace("$parentName", parentName).replace( "$parentID", parentID ) @@ -269,9 +281,9 @@ def location_extras(resource, payload_string): payload_string = json.dumps(obj, indent=4) try: - if len(type.strip()) > 0: + if len(type.strip()) > 0 and type != "type": payload_string = payload_string.replace("$t_display", type) - if len(typeCode.strip()) > 0: + if len(typeCode.strip()) > 0 and typeCode != "typeCode": payload_string = payload_string.replace("$t_code", typeCode) else: obj = json.loads(payload_string) @@ -283,9 +295,9 @@ def location_extras(resource, payload_string): payload_string = json.dumps(obj, indent=4) try: - if len(physicalType.strip()) > 0: + if len(physicalType.strip()) > 0 and physicalType != "physicalType": payload_string = payload_string.replace("$pt_display", physicalType) - if len(physicalTypeCode.strip()) > 0: + if len(physicalTypeCode.strip()) > 0 and physicalTypeCode != "physicalTypeCode": payload_string = payload_string.replace("$pt_code", physicalTypeCode) else: obj = json.loads(payload_string) @@ -528,7 +540,14 @@ def build_payload(resource_type, resources, resource_payload_file): with click.progressbar(resources, label='Progress::Building payload ') as build_payload_progress: for resource in build_payload_progress: logging.info("\t") - name, status, method, id, *_ = resource + + try: + name, status, method, id, *_ = resource + except ValueError: + name, status = resource + method = "create" + id = str(uuid.uuid5(uuid.NAMESPACE_DNS, name)) + try: if method == "create": version = "1" From e86f031326def5ada37b2e5f324113c1b3a36883 Mon Sep 17 00:00:00 2001 From: TraciebelWairimu Date: Mon, 4 Mar 2024 17:05:13 +0300 Subject: [PATCH 2/3] Fix careteam_min.csv build payload --- importer/README.md | 4 +++- .../csv/organizations/organizations_min.csv | 4 ++-- importer/main.py | 22 +++++++++++-------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/importer/README.md b/importer/README.md index 502fbe5a..23d8eff1 100644 --- a/importer/README.md +++ b/importer/README.md @@ -63,6 +63,7 @@ The coverage report `coverage.html` will be at the working directory - Run `python3 main.py --csv_file csv/locations/locations_min.csv --resource_type locations --log_level info` - See example csv [here](/importer/csv/locations/locations_min.csv) - The first two columns __name__ and __status__ is the minimum required +- If the csv file has only the required columns, (e.g. [locations_min.csv](/importer/csv/locations/locations_min.csv)) the __id__ and __method__ are set to __generating a new unique_uuid__ and a default value __create__ method respectively - [locations_full](/importer/csv/locations/locations_full.csv) shows more options available - The third column is the request method, can be either create or update. Default is set to create - The fourth column is the id, which is required when updating @@ -84,6 +85,7 @@ The coverage report `coverage.html` will be at the working directory - Run `python3 main.py --csv_file csv/organizations/organizations_min.csv --resource_type organizations --log_level info` - See example csv [here](/importer/csv/organizations/organizations_min.csv) - The first column __name__ is the only one required +- If the csv file has only the required column, (e.g. [organizations_min.csv](/importer/csv/organizations/organizations_min.csv)) the __id__ , __active__, and __method__ are set to __generating a new unique_uuid__ and the default values __create__ and __true__ respectively - [organizations_full](/importer/csv/organizations/organizations_full.csv) shows more options available - The third column is the request method, can be either create or update. Default is set to create - The fourth column is the id, which is required when updating @@ -93,7 +95,7 @@ The coverage report `coverage.html` will be at the working directory - Run `python3 main.py --csv_file csv/careteams/careteam_min.csv --resource_type careTeams --log_level info` - See example csv [here](/importer/csv/careteams/careteam_min.csv) - The first column __name__ is the only one required -- If the status is not set it will default to __active__ +- If the csv file has only the required column, (e.g. [careteam_min.csv](/importer/(csv/careteams/careteam_min.csv))) the __id__ , __status__, and __method__ are set to __generating a new unique_uuid__ and the default values __create__ and __active__ respectively - [careteam_full](/importer/csv/careteams/careteam_full.csv) shows more options available - The third column is the request method, can be either create or update. Default is set to create - The fourth column is the id, which is required when updating diff --git a/importer/csv/organizations/organizations_min.csv b/importer/csv/organizations/organizations_min.csv index 48735085..bcd73e23 100644 --- a/importer/csv/organizations/organizations_min.csv +++ b/importer/csv/organizations/organizations_min.csv @@ -1,2 +1,2 @@ -name,active -Min Organization,true \ No newline at end of file +name, +Min Organization \ No newline at end of file diff --git a/importer/main.py b/importer/main.py index 334aebd1..455a7a59 100644 --- a/importer/main.py +++ b/importer/main.py @@ -234,7 +234,7 @@ def organization_extras(resource, payload_string): try: _, active, *_, alias = resource except ValueError: - active = resource[1] + active = "true" alias = "alias" try: if alias and alias != "alias": @@ -320,22 +320,26 @@ def care_team_extras( elements = [] elements2 = [] - try: - *_, organizations, participants = resource - except ValueError: - logging.info("Handling assignment") + *_, organizations, participants = resource if load_type == "min": + organizations = "organizations" + participants = "participants" try: - if organizations: + if organizations and organizations != "organizations": elements = organizations.split("|") + else: + return payload_string except IndexError: - pass + return payload_string try: - if participants: + if participants and participants != "participants": elements2 = participants.split("|") + else: + return payload_string except IndexError: - pass + return payload_string + elif load_type == "full": elements = resource else: From 799c44bbe4a2c73747f83c22b52ebfb76d2c2c86 Mon Sep 17 00:00:00 2001 From: TraciebelWairimu Date: Tue, 5 Mar 2024 15:40:08 +0300 Subject: [PATCH 3/3] Fix min csvs build payload and update tests --- .../csv/organizations/organizations_min.csv | 2 +- importer/main.py | 9 +- importer/test_main.py | 117 ++++++++++++++++++ 3 files changed, 125 insertions(+), 3 deletions(-) diff --git a/importer/csv/organizations/organizations_min.csv b/importer/csv/organizations/organizations_min.csv index bcd73e23..bdb940b4 100644 --- a/importer/csv/organizations/organizations_min.csv +++ b/importer/csv/organizations/organizations_min.csv @@ -1,2 +1,2 @@ -name, +name Min Organization \ No newline at end of file diff --git a/importer/main.py b/importer/main.py index 455a7a59..6e736286 100644 --- a/importer/main.py +++ b/importer/main.py @@ -320,7 +320,11 @@ def care_team_extras( elements = [] elements2 = [] - *_, organizations, participants = resource + try: + *_, organizations, participants = resource + except ValueError: + organizations = "organizations" + participants = "participants" if load_type == "min": organizations = "organizations" @@ -548,7 +552,8 @@ def build_payload(resource_type, resources, resource_payload_file): try: name, status, method, id, *_ = resource except ValueError: - name, status = resource + name = resource[0] + status = "" if len(resource) == 1 else resource[1] method = "create" id = str(uuid.uuid5(uuid.NAMESPACE_DNS, name)) diff --git a/importer/test_main.py b/importer/test_main.py index 484e27b8..74dcf6b3 100644 --- a/importer/test_main.py +++ b/importer/test_main.py @@ -83,6 +83,45 @@ def test_build_payload_organizations(self, mock_get_resource): } validate(payload_obj["entry"][2]["request"], request_schema) + # TestCase organizations_min.csv + csv_file = "csv/organizations/organizations_min.csv" + resource_list = read_csv(csv_file) + payload = build_payload( + "organizations", resource_list, "json_payloads/organizations_payload.json" + ) + payload_obj = json.loads(payload) + self.assertIsInstance(payload_obj, dict) + self.assertEqual(payload_obj["resourceType"], "Bundle") + self.assertEqual(len(payload_obj["entry"]), 1) + + resource_schema = { + "type": "object", + "properties": { + "resourceType": {"const": "Organization"}, + "id": {"const": "3da051e0-d743-5574-8f0e-6cb8798551f5"}, + "identifier": {"type": "array", "items": {"type": "object"}}, + "active": {"const": "true"}, + "name": {"const": "Min Organization"} + }, + "required": [ + "id", + "identifier", + "active", + "name" + ], + } + validate(payload_obj["entry"][0]["resource"], resource_schema) + + request_schema = { + "type": "object", + "properties": { + "method": {"const": "PUT"}, + "url": {"const": "Organization/3da051e0-d743-5574-8f0e-6cb8798551f5"}, + "ifMatch": {"const": "1"}, + }, + } + validate(payload_obj["entry"][0]["resource"], request_schema) + @patch("main.get_resource") def test_build_payload_locations(self, mock_get_resource): mock_get_resource.return_value = "1" @@ -169,6 +208,45 @@ def test_build_payload_locations(self, mock_get_resource): } validate(payload_obj["entry"][0]["request"], request_schema) + # TestCase locations_min.csv + csv_file = "csv/locations/locations_min.csv" + resource_list = read_csv(csv_file) + payload = build_payload( + "locations", resource_list, "json_payloads/locations_payload.json" + ) + payload_obj = json.loads(payload) + self.assertIsInstance(payload_obj, dict) + self.assertEqual(payload_obj["resourceType"], "Bundle") + self.assertEqual(len(payload_obj["entry"]), 2) + + resource_schema = { + "type": "object", + "properties": { + "resourceType": {"const": "Location"}, + "id": {"const": "c4336f73-4450-566b-b381-d07a6e857d72"}, + "identifier": {"type": "array", "items": {"type": "object"}}, + "status": {"const": "active"}, + "name": {"const": "City1"} + }, + "required": [ + "id", + "identifier", + "status", + "name" + ], + } + validate(payload_obj["entry"][0]["resource"], resource_schema) + + request_schema = { + "type": "object", + "properties": { + "method": {"const": "PUT"}, + "url": {"const": "Location/c4336f73-4450-566b-b381-d07a6e857d72"}, + "ifMatch": {"const": "1"}, + }, + } + validate(payload_obj["entry"][0]["resource"], request_schema) + @patch("main.get_resource") def test_build_payload_care_teams(self, mock_get_resource): mock_get_resource.return_value = "1" @@ -206,6 +284,45 @@ def test_build_payload_care_teams(self, mock_get_resource): } validate(payload_obj["entry"][0]["request"], request_schema) + # TestCase careteam_min.csv + csv_file = "csv/careteams/careteam_min.csv" + resource_list = read_csv(csv_file) + payload = build_payload( + "careTeams", resource_list, "json_payloads/careteams_payload.json" + ) + payload_obj = json.loads(payload) + self.assertIsInstance(payload_obj, dict) + self.assertEqual(payload_obj["resourceType"], "Bundle") + self.assertEqual(len(payload_obj["entry"]), 2) + + resource_schema = { + "type": "object", + "properties": { + "resourceType": {"const": "CareTeam"}, + "id": {"const": "17150b86-00db-599b-9984-1e7aa08291bb"}, + "identifier": {"type": "array", "items": {"type": "object"}}, + "status": {"const": ""}, + "name": {"const": "Good careteam"} + }, + "required": [ + "id", + "identifier", + "status", + "name" + ], + } + validate(payload_obj["entry"][0]["resource"], resource_schema) + + request_schema = { + "type": "object", + "properties": { + "method": {"const": "PUT"}, + "url": {"const": "Location/17150b86-00db-599b-9984-1e7aa08291bb"}, + "ifMatch": {"const": "1"}, + }, + } + validate(payload_obj["entry"][0]["resource"], request_schema) + def test_extract_matches(self): csv_file = "csv/organizations/organization_locations.csv" resource_list = read_csv(csv_file)