def test_json_with_resource(self, mock_jsonify, mock_get_store): resource = Patient(id="test") mock_get_store.return_value.normalize_resource.return_value = resource r = BaseResource(resource=resource) r.json() mock_jsonify.assert_called_once_with(resource.dict())
def test_issue_28(): """Running the following code: from fhir.resources import construct_fhir_element d = {'resourceType': 'Patient', 'contained':[{'resourceType': 'Patient'}]} construct_fhir_element('Patient', d) end up changing the value of d to {'resourceType': 'Patient', 'contained': [{}]} I would expect it to not change the given object. Moreover, because it doesn't delete resourceType from the outter dictionary, I assume this behavior is not expected. Is it on purpose? Thanks, Itay""" data = { "resourceType": "Patient", "contained": [{ "resourceType": "Patient" }] } construct_fhir_element( "Patient", data, ) # should not change data assert data == { "resourceType": "Patient", "contained": [{ "resourceType": "Patient" }], } data = { "resourceType": "Patient", "contained": [{ "resourceType": "Patient" }] } Patient.parse_obj(data) # should not change data assert data == { "resourceType": "Patient", "contained": [{ "resourceType": "Patient" }], } data = { "resourceType": "Patient", "contained": [{ "resourceType": "Patient" }] } Patient(**data) # should not change data assert data == { "resourceType": "Patient", "contained": [{ "resourceType": "Patient" }], }
def test_init_with_resource(self, mock_get_store): """Initializes resource and resource_type""" resource_data = {"gender": "female"} mock_get_store.return_value.normalize_resource.return_value = Patient( **resource_data) r = BaseResource(resource=resource_data) assert mock_get_store.call_count == 1 assert r.db == mock_get_store.return_value assert r.id is None assert r.resource == Patient(**resource_data) assert r.resource_type == "BaseResource"
def test_patch(self, mock_get_store): """Applies a patch on the resource by reading and then updating it""" resource = Patient(id="test") mock_get_store.return_value.normalize_resource.return_value = resource mock_get_store.return_value.patch.return_value = Patient( id="test", gender="other") patch_data = {"gender": "other"} r = BaseResource(id="test", resource=resource) r.patch(patch_data) mock_get_store.return_value.patch.assert_called_once_with( "BaseResource", "test", patch_data) assert r.resource == Patient(id="test", gender="other")
def test_search_not_found(self, mock_get_store): """ Calls the search method and handles the case where no result is found. """ resource = Patient(id="test") error = OperationOutcome(id="notfound", issue=[{ "severity": "error", "code": "noop", "diagnostics": "bobo" }]) mock_get_store.return_value.normalize_resource.return_value = resource mock_get_store.return_value.search.return_value = error r = BaseResource(resource=resource) res = r.search(query_string="name=NotFound") assert res == error mock_get_store.return_value.search.assert_called_once_with( "BaseResource", as_json=True, params=None, query_string="name=NotFound") assert r.resource == resource assert r.id == resource.id
def test_update_with_id(self, mock_get_store): """Test update with id. Calls the update method of the fhirstore client and registers the resource """ resource = Patient(id="test") mock_get_store.return_value.normalize_resource.return_value = resource mock_get_store.return_value.update.return_value = Patient( id="test", gender="other") update_data = {"gender": "other"} r = BaseResource(id="test", resource=resource) r.update(update_data) mock_get_store.return_value.update.assert_called_once_with( "test", update_data) assert r.resource == Patient(id="test", gender="other")
def test_read_missing_id(self, mock_get_store): """Raises an error when the id was not provided at init""" resource = Patient() mock_get_store.return_value.normalize_resource.return_value = resource r = BaseResource(resource=resource) with pytest.raises(BadRequest, match="Resource ID is required"): r.read() assert mock_get_store.return_value.read.call_count == 0
def test_delete(self, mock_get_store): """Calls the delete method of the fhirstore client""" resource = Patient(id="test") mock_get_store.return_value.normalize_resource.return_value = resource r = BaseResource(resource=resource) r.delete() mock_get_store.return_value.delete.assert_called_once_with( "BaseResource", "test") assert r.resource is None assert r.id is None
def test_patch_missing_id(self, mock_get_store): """Raises an error when the resource id was not provided at init""" resource = Patient(gender="test") mock_get_store.return_value.normalize_resource.return_value = resource r = BaseResource(resource=resource) with pytest.raises( BadRequest, match="Resource ID is required to patch a resource", ): r.patch({"some": "patch"}) assert mock_get_store.return_value.patch.call_count == 0
def test_patch_missing_data(self, mock_get_store): """Raises an error when the patch data is not provided""" resource = Patient(id="test") mock_get_store.return_value.normalize_resource.return_value = resource r = BaseResource(id="test") with pytest.raises( BadRequest, match="Patch data is required to patch a resource", ): r.patch(None) assert mock_get_store.return_value.patch.call_count == 0
def test_delete_missing_id(self, mock_get_store): """Raises an error when the patch data is not provided""" resource = Patient() mock_get_store.return_value.normalize_resource.return_value = resource r = BaseResource(resource=resource) with pytest.raises( BadRequest, match="Resource ID is required to delete it", ): r = r.delete() assert mock_get_store.return_value.update.call_count == 0
def test_create_extra_id(self, mock_get_store): """Accepts the resource data when an id was provided""" resource = Patient(id="test") mock_get_store.return_value.normalize_resource.return_value = resource mock_get_store.return_value.create.return_value = resource mock_get_store.return_value.read.return_value = None r = BaseResource(resource=resource) r.create() assert r.id == "test" assert r.resource == resource
async def test_create_resource(pg_connection): """ """ db = pg_connection resource_obj = Patient.parse_file(FHIR_EXAMPLE_RESOURCES / "Patient.json") resource_obj.id = str(uuid.uuid4()) resource_obj.meta = {"versionId": "1", "lastUpdated": datetime.utcnow()} res1 = await crud.create_resource(db, resource_obj, FHIR_VERSION.R4) await tasks.add_resource_history(db, res1, first=True) await tasks.add_resource_history(db, res1, first=False) result = await db.fetch_all( f"SELECT * FROM {ResourceHistoryModel.__tablename__} ORDER BY id ASC" ) assert len(result) == 2 assert result[0]["next_id"] == result[1]["id"] assert result[0]["id"] == result[1]["prev_id"]
def test_read(self, mock_get_store): """Test read method. Calls the read method of the fhirstore client and registers the resource. """ resource = Patient(id="test") mock_get_store.return_value.normalize_resource.return_value = resource mock_get_store.return_value.read.return_value = resource r = BaseResource(id="test") r.read() mock_get_store.return_value.read.assert_called_once_with( "BaseResource", "test") assert r.resource == resource
def test_create(self, mock_uuid, mock_get_store): """Test create method. Calls the create method of the fhirstore client and registers the ID """ resource = Patient(gender="male") mock_get_store.return_value.normalize_resource.return_value = resource mock_get_store.return_value.create.return_value = resource mock_get_store.return_value.read.return_value = None r = BaseResource(resource=resource) r.create() mock_get_store.return_value.create.assert_called_once_with(resource) assert r.id == "uuid" assert r.resource == resource
def test_search_params(self, mock_get_store): """ Calls the search method of the fhirstore client using `params` """ resource = Patient(id="test") mock_get_store.return_value.normalize_resource.return_value = resource mock_get_store.return_value.search.return_value = resource r = BaseResource(resource=resource) params = MultiDict((("name", "Vincent"), )) res = r.search(params=params) assert res == resource mock_get_store.return_value.search.assert_called_once_with( "BaseResource", as_json=True, params=params, query_string=None) assert r.resource == resource assert r.id == resource.id
def test_search_qs(self, mock_get_store): """ Calls the search method of the fhirstore client using `query_string` """ resource = Patient(id="test") mock_get_store.return_value.normalize_resource.return_value = resource mock_get_store.return_value.search.return_value = resource r = BaseResource(resource=resource) res = r.search(query_string="name=Vincent") assert res == resource mock_get_store.return_value.search.assert_called_once_with( "BaseResource", as_json=True, params=None, query_string="name=Vincent") assert r.resource == resource assert r.id == resource.id
from fhirpy import SyncFHIRClient from fhir.resources.patient import Patient from fhir.resources.observation import Observation from fhir.resources.humanname import HumanName from fhir.resources.contactpoint import ContactPoint import json client = SyncFHIRClient( url='https://dh9fytkn0e.execute-api.eu-west-2.amazonaws.com/dev', extra_headers={ "x-api-key": "EzAVxFOjEqagIbYVHK2Mz7RUp1GOpRHMVTTxVQk9", "Authorization": "eyJraWQiOiJ2ZXZ0cUNYZitiUzNBM3hNWm5IVFhRbDlucTFZaDloRzJZNkxVXC94bTd0ST0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJmOWU4YmI3OC04MjRlLTRhZGQtOTZjNS1iZjVmMGY3YTlkNTUiLCJjb2duaXRvOmdyb3VwcyI6WyJwcmFjdGl0aW9uZXIiXSwiZW1haWxfdmVyaWZpZWQiOnRydWUsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC5ldS13ZXN0LTIuYW1hem9uYXdzLmNvbVwvZXUtd2VzdC0yX0JiTkFoUTVzTSIsImNvZ25pdG86dXNlcm5hbWUiOiJmaGlyYXBpdGVzdCIsIm9yaWdpbl9qdGkiOiIxZWQ0NmU1NS1jNmE1LTQ3MjMtYTEyZi04OTYzOWQzZGVjYWMiLCJhdWQiOiIxdXJkaWlmZjJlbWMzcGgzamdiZ3AxYm43ayIsImV2ZW50X2lkIjoiYjQ4MDM0NjctMjRjYy00OWZlLTg3NTYtNTgzZDEzNDZmOGE3IiwidG9rZW5fdXNlIjoiaWQiLCJhdXRoX3RpbWUiOjE2NTgyNDQ0MDUsImV4cCI6MTY1ODMzMDgwNSwiaWF0IjoxNjU4MjQ0NDA1LCJqdGkiOiJjNjJkNTc3Zi01NWJmLTQwNjQtYjYwOC0xMzA1MTQ1ODM0M2QiLCJlbWFpbCI6ImFsbWE1Lm5oc2RAZ21haWwuY29tIn0.XxflQNEL_N8XWut2MxGNTZfnjei4qCsbvEBs31n1RMemY9DneEU1haYGGaHp4T3CyBy2-eLeaDbfI16dhEnh_5D6qSgRoMPWv5ScZop2uvoqujkufsp-C6lZsRtetyowETrcaSs46jvumqzpONUO5F5i7UdEg2lh9N0iKfUi67MepgMy9gBJMZkKQhTl6S0BRNHfyGhUT5kbN2EeFeDltiSOaDZKk7IbRoUO0OnZYNFcqmDNAjPYzLFKNnYyGcsMlPSKZaULHt3P7EhAS-e5EOG5c8-Sqp7fPOZ3cpq2QHr5RF4hZcyZiUwoyYoeW4_MDvhkdew2tdnyEsFQHwuO2g" }) patients_resources = client.resources('Patient') patient0 = Patient.parse_obj( patients_resources.search(family='Jameson', given='Matt').first().serialize()) print("Our paitent:", patient0.name)
# page_file = "temp/pg"+str(count)+".png" # page.save(page_file, "PNG") # count = count+1 # for page_num in range(count): # page_file = "temp/pg"+str(page_num)+".png" # score_assessment_form(page_file, page_num, # wThresh=W_THRESH, hThresh=H_THRESH) # print("Raw Score:", subtotal) # creating FHIR resource for patient record pat = Patient({ "id": "1", "name": [{ "given": ["John"], "family": "Doe" }], "birthDate": "2019-01-24" }) print(pat) enc = Encounter() enc.subject = pat docref_json = { "resourceType": "DocumentReference", } doc = DocumentReference() obs = Observation() print("--finish--")