|
1 | 1 | import unittest |
| 2 | + |
| 3 | +from ieds_db_operations import extract_patient_resource_from_item |
2 | 4 | from unittest.mock import patch, MagicMock |
3 | 5 | from exceptions.id_sync_exception import IdSyncException |
4 | | - |
5 | 6 | import ieds_db_operations |
6 | 7 |
|
7 | 8 |
|
| 9 | +class TestExtractPatientResourceFromItem(unittest.TestCase): |
| 10 | + |
| 11 | + def test_extract_from_dict_with_contained_patient(self): |
| 12 | + item = { |
| 13 | + "Resource": { |
| 14 | + "resourceType": "Immunization", |
| 15 | + "contained": [ |
| 16 | + {"resourceType": "Patient", "id": "P1", "name": [{"family": "Doe"}]} |
| 17 | + ], |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + patient = extract_patient_resource_from_item(item) |
| 22 | + self.assertIsNotNone(patient) |
| 23 | + self.assertIsInstance(patient, dict) |
| 24 | + self.assertEqual(patient.get("resourceType"), "Patient") |
| 25 | + self.assertEqual(patient.get("id"), "P1") |
| 26 | + |
| 27 | + def test_extract_from_json_string(self): |
| 28 | + resource_json = '{"resourceType": "Immunization", "contained": [{"resourceType": "Patient", "id": "P2"}]}' |
| 29 | + item = {"Resource": resource_json} |
| 30 | + |
| 31 | + patient = extract_patient_resource_from_item(item) |
| 32 | + self.assertIsNotNone(patient) |
| 33 | + self.assertEqual(patient.get("id"), "P2") |
| 34 | + |
| 35 | + def test_malformed_json_string_returns_none(self): |
| 36 | + # A malformed JSON string should not raise, but return None |
| 37 | + item = {"Resource": "{not: valid json}"} |
| 38 | + self.assertIsNone(extract_patient_resource_from_item(item)) |
| 39 | + |
| 40 | + def test_non_dict_resource_returns_none(self): |
| 41 | + item = {"Resource": 12345} |
| 42 | + self.assertIsNone(extract_patient_resource_from_item(item)) |
| 43 | + |
| 44 | + def test_missing_resource_returns_none(self): |
| 45 | + item = {} |
| 46 | + self.assertIsNone(extract_patient_resource_from_item(item)) |
| 47 | + |
| 48 | + |
8 | 49 | class TestIedsDbOperations(unittest.TestCase): |
9 | 50 | """Base test class for IEDS database operations""" |
10 | 51 |
|
|
0 commit comments