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

Check and Throw error when ID is Non-existent, when method is update … #135

Merged
merged 5 commits into from
Feb 16, 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
46 changes: 24 additions & 22 deletions importer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,11 @@ def get_valid_resource_type(resource_type):


# This function gets the current resource version from the API
def get_resource_version(resource_id, resource_type):
logging.debug("Getting resource version")
modified_resource_type = get_valid_resource_type(resource_type)
resource_url = "/".join([config.fhir_base_url, modified_resource_type, resource_id])
def get_resource(resource_id, resource_type):
resource_type = get_valid_resource_type(resource_type)
resource_url = "/".join([config.fhir_base_url, resource_type, resource_id])
response = handle_request("GET", "", resource_url)
json_response = json.loads(response[0])
return json_response["meta"]["versionId"]
return json.loads(response[0])["meta"]["versionId"] if response[1] == 200 else "0"


# This function builds a json payload
Expand All @@ -502,15 +500,7 @@ def build_payload(resource_type, resources, resource_payload_file):
name, status, method, id, *_ = resource
try:
if method == "create":
if len(id.strip()) > 0:
# use the provided id
unique_uuid = id.strip()
identifier_uuid = id.strip()
else:
# generate a new uuid
unique_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, name))
identifier_uuid = unique_uuid
elif method == "update":
version = "1"
if len(id.strip()) > 0:
# use the provided id
unique_uuid = id.strip()
Expand All @@ -523,26 +513,38 @@ def build_payload(resource_type, resources, resource_payload_file):
# default if method is not provided
unique_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, name))
identifier_uuid = unique_uuid
version = "1"

try:
if method == "update":
if len(id.strip()) > 0:
version = get_resource(id, resource_type)
if version != "0":
# use the provided id
unique_uuid = id.strip()
identifier_uuid = id.strip()
else:
logging.info("Failed to get resource!")
raise ValueError("Trying to update a Non-existent resource")
else:
logging.info("The id is required!")
raise ValueError("The id is required to update a resource")
except IndexError:
raise ValueError("The id is required to update a resource")

# ps = payload_string
ps = (
payload_string.replace("$name", name)
.replace("$unique_uuid", unique_uuid)
.replace("$identifier_uuid", identifier_uuid)
.replace("$version", version)
)

try:
ps = ps.replace("$status", status)
except IndexError:
ps = ps.replace("$status", "active")

# Get resource versions from API
try:
version = get_resource_version(id, resource_type)
ps = ps.replace("$version", version)
except Exception:
ps = ps.replace("$version", "1")

if resource_type == "organizations":
ps = organization_extras(resource, ps)
elif resource_type == "locations":
Expand Down
1 change: 1 addition & 0 deletions importer/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ urllib3==2.0.3
backoff==2.2.1
pytest==7.4.2
jsonschema==4.21.1
mock==5.1.0
73 changes: 69 additions & 4 deletions importer/test_main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import json
import unittest
from jsonschema import validate
from main import read_csv, build_payload, build_org_affiliation, extract_matches, create_user_resources
from mock import patch
from main import (
read_csv,
build_payload,
build_org_affiliation,
extract_matches,
create_user_resources,
)


class TestMain(unittest.TestCase):
Expand All @@ -11,7 +18,10 @@ def test_read_csv(self):
self.assertIsInstance(records, list)
self.assertEqual(len(records), 3)

def test_build_payload_organizations(self):
@patch("main.get_resource")
def test_build_payload_organizations(self, mock_get_resource):
mock_get_resource.return_value = "1"

csv_file = "csv/organizations/organizations_full.csv"
resource_list = read_csv(csv_file)
payload = build_payload(
Expand Down Expand Up @@ -46,7 +56,10 @@ def test_build_payload_organizations(self):
}
validate(payload_obj["entry"][2]["request"], request_schema)

def test_build_payload_locations(self):
@patch("main.get_resource")
def test_build_payload_locations(self, mock_get_resource):
mock_get_resource.return_value = "1"

csv_file = "csv/locations/locations_full.csv"
resource_list = read_csv(csv_file)
payload = build_payload(
Expand Down Expand Up @@ -129,7 +142,10 @@ def test_build_payload_locations(self):
}
validate(payload_obj["entry"][0]["request"], request_schema)

def test_build_payload_care_teams(self):
@patch("main.get_resource")
def test_build_payload_care_teams(self, mock_get_resource):
mock_get_resource.return_value = "1"

csv_file = "csv/careteams/careteam_full.csv"
resource_list = read_csv(csv_file)
payload = build_payload(
Expand Down Expand Up @@ -325,6 +341,55 @@ def test_uuid_generated_in_build_org_affiliation_is_unique_and_repeatable(self):
self.assertEqual(organization_affiliation1, organization_affiliation3)
self.assertEqual(organization_affiliation2, organization_affiliation4)

def test_update_resource_with_no_id_fails(self):
resources = [
[
"City1",
"active",
"update",
"",
"test location-1",
"18fcbc2e-4240-4a84-a270-7a444523d7b6",
"site",
"si",
"ward",
"wa",
]
]
with self.assertRaises(ValueError) as raised_error:
build_payload(
"locations", resources, "json_payloads/locations_payload.json"
)
self.assertEqual(
"The id is required to update a resource", str(raised_error.exception)
)

@patch("main.get_resource")
def test_update_resource_with_non_existing_id_fails(self, mock_get_resource):
mock_get_resource.return_value = "0"
non_existing_id = "123"
resources = [
[
"City1",
"active",
"update",
non_existing_id,
"test location-1",
"18fcbc2e-4240-4a84-a270-7a444523d7b6",
"site",
"si",
"ward",
"wa",
]
]
with self.assertRaises(ValueError) as raised_error:
build_payload(
"locations", resources, "json_payloads/locations_payload.json"
)
self.assertEqual(
"Trying to update a Non-existent resource", str(raised_error.exception)
)


if __name__ == "__main__":
unittest.main()
Loading