Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix organizations_min.csv, locations_min.csv and careteam_min.csv build payload #158

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion importer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
56 changes: 42 additions & 14 deletions importer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "true"
alias = "alias"
try:
if alias and alias != "alias":
payload_string = payload_string.replace("$alias", alias)
else:
obj = json.loads(payload_string)
Expand All @@ -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
)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -311,19 +323,27 @@ def care_team_extras(
try:
*_, organizations, participants = resource
except ValueError:
logging.info("Handling assignment")
organizations = "organizations"
participants = "participants"

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:
Expand Down Expand Up @@ -528,7 +548,15 @@ 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 = resource[0]
status = "" if len(resource) == 1 else resource[1]
method = "create"
id = str(uuid.uuid5(uuid.NAMESPACE_DNS, name))

try:
if method == "create":
version = "1"
Expand Down
117 changes: 117 additions & 0 deletions importer/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
Loading