diff --git a/tests/test_sync.py b/tests/test_sync.py index 8bee21eb..47b4c0ad 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -76,6 +76,12 @@ def internal_patient_duplicate_active_match(datadir): return load_json(datadir, "internal_patient_duplicate_active_match.json") +@fixture +def internal_patient_duplicate_mismatch(datadir): + return load_json(datadir, "internal_patient_duplicate_mismatch.json") + + + @fixture def internal_patient_duplicate_inactive_match(datadir): return load_json(datadir, "internal_patient_duplicate_inactive_match.json") @@ -85,10 +91,8 @@ def test_new_upsert( mocker, faux_token, external_patient_search, - external_patient_search_active, internal_patient_miss, new_patient, - new_patient_active ): """Without finding a matching patient, should insert new and return""" @@ -108,7 +112,47 @@ def test_new_upsert( assert result == new_patient - """Finding inactive patient, user specified to not restore, should insert new and return""" +def test_new_upsert_active( + client, + mocker, + faux_token, + external_patient_search_active, + internal_patient_miss, + new_patient_active + ): + """Without finding a matching patient, should insert new and return""" + + # Mock HAPI search failing to find a matching patient + mocker.patch( + "patientsearch.models.sync.requests.get", + return_value=mock_response(internal_patient_miss), + ) + + # Mock POST to generate new patient on HAPI + mocker.patch( + "patientsearch.models.sync.requests.post", + return_value=mock_response(new_patient_active), + ) + + result = sync_bundle(faux_token, external_patient_search_active) + assert result == new_patient_active + + +def test_upsert_inactive( + client, + mocker, + faux_token, + external_patient_search_active, + internal_patient_miss, + new_patient_active + ): + """Finding a matching inactive patient, user chose to generate new patient""" + + # Mock HAPI search finding a matching inactive + mocker.patch( + "patientsearch.models.sync.requests.get", + return_value=mock_response(internal_patient_miss), + ) # Mock POST to generate new patient on HAPI mocker.patch( @@ -135,10 +179,7 @@ def test_existing( mocker, faux_token, external_patient_search, - external_patient_search_active, internal_patient_match, - internal_patient_active_match, - internal_patient_inactive_match ): """Finding a matching patient, return existing""" @@ -151,6 +192,14 @@ def test_existing( result = sync_bundle(faux_token, external_patient_search) assert result == internal_patient_match["entry"][0]["resource"] + +def test_existing_active( + client, + mocker, + faux_token, + external_patient_search_active, + internal_patient_active_match, +): """Finding a matching active patient from active external search, return existing""" # Mock HAPI search finding a matching active patient @@ -162,10 +211,18 @@ def test_existing( result = sync_bundle(faux_token, external_patient_search_active) assert result == internal_patient_active_match["entry"][0]["resource"] - """Finding a matching inactive patient from active external search, return existing restored/new""" + +def test_existing_inactive( + client, + mocker, + faux_token, + external_patient_search_active, + internal_patient_inactive_match +): + """Finding a matching inactive patient from active external search, return existing restored""" # Mock HAPI search finding a matching inactive patient - # when the service is called to create to be restored + # when the service is called for the patient to be restored mocker.patch( "patientsearch.models.sync.requests.get", return_value=mock_response(internal_patient_inactive_match), @@ -175,8 +232,6 @@ def test_existing( assert result == internal_patient_inactive_match["entry"][0]["resource"] - - def test_existing_modified( client, mocker, @@ -220,10 +275,7 @@ def test_duplicate( mocker, faux_token, external_patient_search, - external_patient_search_active, internal_patient_duplicate_match, - internal_patient_duplicate_active_match, - internal_patient_duplicate_inactive_match ): """Finding a matching patient with duplicates, handle well""" @@ -237,14 +289,70 @@ def test_duplicate( result = sync_bundle(faux_token, external_patient_search) assert result == internal_patient_duplicate_match["entry"][0]["resource"] + +def test_duplicate_active( + client, + mocker, + faux_token, + external_patient_search_active, + internal_patient_duplicate_active_match, +): + """Finding a matching active patient with duplicates, handle well""" + + # Mock HAPI search finding duplicate matching patients + mocker.patch( + "patientsearch.models.sync.requests.get", + return_value=mock_response(internal_patient_duplicate_active_match), + ) + # Shouldn't kill the process, but return the first result = sync_bundle(faux_token, external_patient_search_active) assert result == internal_patient_duplicate_active_match["entry"][0]["resource"] - # Shouldn't kill the process, but return the first, restoring/initiating new patient + +def test_duplicate_inactive( + client, + mocker, + faux_token, + external_patient_search_active, + internal_patient_duplicate_inactive_match, +): + """Finding a matching inactive patient with duplicates, handle well""" + + # Mock HAPI search finding duplicate matching patients + mocker.patch( + "patientsearch.models.sync.requests.get", + return_value=mock_response(internal_patient_duplicate_inactive_match), + ) + + # Shouldn't kill the process, but return the first result = sync_bundle(faux_token, external_patient_search_active) - assert result == internal_patient_duplicate_inactive_match["entry"][0]["resource"] + assert result != internal_patient_duplicate_inactive_match["entry"][0]["resource"] - # # TODO: test inactive/active configuration, should return active one? - # result = sync_bundle(faux_token, external_patient_search_active) - # assert result == internal_patient_duplicate_inactive_match["entry"][1]["resource"] + +def test_duplicate_mismatch( + client, + mocker, + faux_token, + external_patient_search_active, + internal_patient_duplicate_mismatch, + internal_patient_duplicate_active_match, +): + """Finding mistmatching active/inactive patient with duplicates, handle well""" + + # Mock HAPI search finding duplicate matching patients + mocker.patch( + "patientsearch.models.sync.requests.get", + return_value=mock_response(internal_patient_duplicate_mismatch), + ) + # TODO: add logic to handle if true is not first + + # Shouldn't kill the process, but return the first + result = sync_bundle(faux_token, external_patient_search_active) + # First duplicate is true, should be the same + assert result == internal_patient_duplicate_active_match["entry"][0]["resource"] + + # Shouldn't kill the process, but return the first + result = sync_bundle(faux_token, external_patient_search_active) + # Second duplicate is false, should not be the same + assert result != internal_patient_duplicate_active_match["entry"][1]["resource"] diff --git a/tests/test_sync/internal_patient_duplicate_mismatch.json b/tests/test_sync/internal_patient_duplicate_mismatch.json new file mode 100644 index 00000000..f0e129ef --- /dev/null +++ b/tests/test_sync/internal_patient_duplicate_mismatch.json @@ -0,0 +1,83 @@ +{ + "resourceType": "Bundle", + "id": "c865c9ee-2432-4f6d-87bd-3a6d5243bd77", + "meta": { + "lastUpdated": "2020-06-19T13:04:43.062+00:00" + }, + "type": "searchset", + "total": 2, + "link": [ + { + "relation": "self", + "url": "http://localhost:8080/hapi-fhir-jpaserver/fhir/Patient?family=skywalker" + } + ], + "entry": [ + { + "fullUrl": "http://localhost:8080/hapi-fhir-jpaserver/fhir/Patient/1102", + "resource": { + "resourceType": "Patient", + "id": "1102", + "meta": { + "versionId": "1", + "lastUpdated": "2020-06-19T12:54:39.363+00:00" + }, + "text": { + "status": "generated", + "div": "
luke SKYWALKER
Date of birth12 January 1977
" + }, + "active": true, + "name": [ + { + "family": "skywalker", + "given": [ + "luke" + ] + } + ], + "gender": "male", + "birthDate": "1977-01-12" + }, + "search": { + "mode": "match" + }, + "response": { + "status": "201 Created", + "etag": "W/\"1\"" + } + }, + { + "fullUrl": "http://localhost:8080/hapi-fhir-jpaserver/fhir/Patient/1103", + "resource": { + "resourceType": "Patient", + "id": "1103", + "meta": { + "versionId": "1", + "lastUpdated": "2020-06-19T12:54:39.363+00:00" + }, + "text": { + "status": "generated", + "div": "
luke SKYWALKER
Date of birth12 January 1977
" + }, + "active": false, + "name": [ + { + "family": "skywalker", + "given": [ + "luke" + ] + } + ], + "gender": "male", + "birthDate": "1977-01-12" + }, + "search": { + "mode": "match" + }, + "response": { + "status": "201 Created", + "etag": "W/\"1\"" + } + } + ] +}