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

122 generate unique UUID #132

Merged
merged 13 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
13 changes: 7 additions & 6 deletions importer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ def create_user_resources(user_id, user):
logging.info("Creating user resources")
# generate uuids
if len(str(user[4]).strip()) == 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please refactor all the code in this function by destructuring user and removing references by index.

eg.

a, b, c, d = user

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

practitioner_uuid = str(uuid.uuid4())
practitioner_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, user[2] + user[7] + "practitioner_uuid"))
else:
practitioner_uuid = user[4]

group_uuid = str(uuid.uuid4())
practitioner_role_uuid = str(uuid.uuid4())
group_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, user[2] + user[7] + "group_uuid"))
practitioner_role_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, user[2] + user[7] + "practitioner_role_uuid"))

# get payload and replace strings
initial_string = """{"resourceType": "Bundle","type": "transaction","meta": {"lastUpdated": ""},"entry": """
Expand Down Expand Up @@ -188,7 +188,7 @@ def create_user_resources(user_id, user):
ff = json.dumps(obj, indent=4)

payload = initial_string + ff + "}"
handle_request("POST", payload, config.fhir_base_url)
return payload


# custom extras for organizations
Expand Down Expand Up @@ -437,7 +437,7 @@ def build_org_affiliation(resources, resource_list):

for key in resources:
rp = ""
unique_uuid = str(uuid.uuid4())
unique_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, key))
org_name = get_org_name(key, resource_list)

rp = (
Expand Down Expand Up @@ -872,7 +872,8 @@ def main(
# check practitioner
practitioner_exists = confirm_practitioner(user, user_id)
if not practitioner_exists:
create_user_resources(user_id, user)
payload = create_user_resources(user_id, user)
handle_request("POST", payload, config.fhir_base_url)
logging.info("Processing complete!")
elif resource_type == "locations":
logging.info("Processing locations")
Expand Down
72 changes: 71 additions & 1 deletion importer/test_main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import unittest
from jsonschema import validate
from main import read_csv, build_payload, build_org_affiliation, extract_matches
from main import read_csv, build_payload, build_org_affiliation, extract_matches, create_user_resources


class TestMain(unittest.TestCase):
Expand Down Expand Up @@ -187,6 +187,76 @@ def test_build_org_affiliation(self):
self.assertEqual(payload_obj["resourceType"], "Bundle")
self.assertEqual(len(payload_obj["entry"]), 2)

def test_uuid_generated_in_creating_user_resources_is_unique_and_repeatable(self):
users = [['Jane', 'Doe', 'Janey', '[email protected]', '', 'Practitioner', 'TRUE',
'a715b562-27f2-432a-b1ba-e57db35e0f93', 'test', 'demo', 'pa$$word'],
['John', 'Doe', 'Janey', '[email protected]', '', 'Practitioner', 'TRUE',
'a715b562-27f2-432a-b1ba-e57db35e0f93', 'test', 'demo', 'pa$$word'],
['Janice', 'Doe', 'Jenn', '[email protected]', '99d54e3c-c26f-4500-a7f9-3f4cb788673f', 'Supervisor',
'TRUE', 'a715b562-27f2-432a-b1ba-e57db35e0f93', 'test', 'demo', 'pa$$word']]

users_uuids = {}
for user_id, user in enumerate(users):
payload = create_user_resources(user[4], user)
payload_obj = json.loads(payload)
practitioner_uuid = payload_obj["entry"][0]["resource"]["id"]
group_uuid = payload_obj["entry"][1]["resource"]["id"]
practitioner_role_uuid = payload_obj["entry"][2]["resource"]["id"]
users_uuids[user_id] = [practitioner_uuid, group_uuid, practitioner_role_uuid]

# practitioner_uuid
self.assertEqual(users_uuids[0][0], users_uuids[1][0])
self.assertNotEqual(users_uuids[1][0], users_uuids[2][0])

# group_uuid
self.assertEqual(users_uuids[0][1], users_uuids[1][1])
self.assertNotEqual(users_uuids[1][1], users_uuids[2][1])

# practitioner_role_uuid
self.assertEqual(users_uuids[0][2], users_uuids[1][2])
self.assertNotEqual(users_uuids[1][2], users_uuids[2][2])

def test_uuid_generated_for_locations_is_unique_and_repeatable(self):
resources = [
['City1', 'active', 'create', '', 'test location-1', '18fcbc2e-4240-4a84-a270-7a444523d7b6', 'jurisdiction',
'jurisdiction'],
['Building1', 'active', 'create', '', 'test location-1', '18fcbc2e-4240-4a84-a270-7a444523d7b6', 'building',
'building'],
['City1', 'active', 'create', '', 'test location-1', '18fcbc2e-4240-4a84-a270-7a444523d7b6', 'jurisdiction',
'jurisdiction']]

payload = build_payload("locations", resources, "json_payloads/locations_payload.json")
payload_obj = json.loads(payload)
location1 = payload_obj["entry"][0]["resource"]["id"]
location2 = payload_obj["entry"][1]["resource"]["id"]
location3 = payload_obj["entry"][2]["resource"]["id"]
print(location1, location2, location3)

self.assertNotEqual(location1, location2)
self.assertEqual(location1, location3)

def test_uuid_generated_in_build_org_affiliation_is_unique_and_repeatable(self):
resource_list = [['HealthyU', 'a9137781-eb94-4d5f-8d39-471a92aec9f2', 'World', '138396'],
['HealthyU', 'a9137781-eb94-4d5f-8d39-471a92aec9f2', 'Kenya', '54876'],
['HealthyU', 'a9137781-eb94-4d5f-8d39-471a92aec9f2', 'Nairobi', '105167'],
['One Org', '8342dd77-aecd-48ab-826b-75c7c33039ed', 'World', '138396']]

resources = extract_matches(resource_list)
payload = build_org_affiliation(resources, resource_list)
payload_obj = json.loads(payload)
organization_affiliation1 = payload_obj["entry"][0]["resource"]["id"]
organization_affiliation2 = payload_obj["entry"][1]["resource"]["id"]

self.assertNotEqual(organization_affiliation1, organization_affiliation2)

payload2 = build_org_affiliation(resources, resource_list)
payload2_obj = json.loads(payload2)
organization_affiliation3 = payload2_obj["entry"][0]["resource"]["id"]
organization_affiliation4 = payload2_obj["entry"][1]["resource"]["id"]

self.assertEqual(organization_affiliation1, organization_affiliation3)
self.assertEqual(organization_affiliation2, organization_affiliation4)


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