Skip to content

Commit

Permalink
Refactoring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Filienko committed Dec 14, 2023
1 parent e60fe9c commit d1c0ed4
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 18 deletions.
144 changes: 126 additions & 18 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 85 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L85

Too many blank lines (3) (E303)
def internal_patient_duplicate_inactive_match(datadir):
return load_json(datadir, "internal_patient_duplicate_inactive_match.json")
Expand All @@ -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
):

Check failure on line 96 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L96

Continuation line with same indent as next logical line (E125)
"""Without finding a matching patient, should insert new and return"""

Expand All @@ -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
):

Check failure on line 122 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L122

Continuation line with same indent as next logical line (E125)
"""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
):

Check failure on line 148 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L148

Continuation line with same indent as next logical line (E125)
"""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(
Expand All @@ -135,10 +179,7 @@ def test_existing(
mocker,

Check failure on line 179 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L179

Trailing whitespace (W291)
faux_token,

Check failure on line 180 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L180

Trailing whitespace (W291)
external_patient_search,

Check failure on line 181 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L181

Trailing whitespace (W291)
external_patient_search_active,
internal_patient_match,

Check failure on line 182 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L182

Trailing whitespace (W291)
internal_patient_active_match,
internal_patient_inactive_match
):
"""Finding a matching patient, return existing"""

Expand All @@ -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,

Check failure on line 197 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L197

Trailing whitespace (W291)
mocker,

Check failure on line 198 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L198

Trailing whitespace (W291)
faux_token,

Check failure on line 199 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L199

Trailing whitespace (W291)
external_patient_search_active,
internal_patient_active_match,

Check failure on line 201 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L201

Trailing whitespace (W291)
):
"""Finding a matching active patient from active external search, return existing"""

# Mock HAPI search finding a matching active patient
Expand All @@ -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,

Check failure on line 216 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L216

Trailing whitespace (W291)
mocker,

Check failure on line 217 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L217

Trailing whitespace (W291)
faux_token,

Check failure on line 218 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L218

Trailing whitespace (W291)
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

Check failure on line 224 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L224

Trailing whitespace (W291)
# 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),
Expand All @@ -175,8 +232,6 @@ def test_existing(
assert result == internal_patient_inactive_match["entry"][0]["resource"]




def test_existing_modified(
client,
mocker,

Check failure on line 237 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L237

Trailing whitespace (W291)
Expand Down Expand Up @@ -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"""

Expand All @@ -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

Check failure on line 349 in tests/test_sync.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/test_sync.py#L349

Blank line contains whitespace (W293)
# 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"]
83 changes: 83 additions & 0 deletions tests/test_sync/internal_patient_duplicate_mismatch.json
Original file line number Diff line number Diff line change
@@ -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": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><div class=\"hapiHeaderText\">luke <b>SKYWALKER </b></div><table class=\"hapiPropertyTable\"><tbody><tr><td>Date of birth</td><td><span>12 January 1977</span></td></tr></tbody></table></div>"
},
"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": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><div class=\"hapiHeaderText\">luke <b>SKYWALKER </b></div><table class=\"hapiPropertyTable\"><tbody><tr><td>Date of birth</td><td><span>12 January 1977</span></td></tr></tbody></table></div>"
},
"active": false,
"name": [
{
"family": "skywalker",
"given": [
"luke"
]
}
],
"gender": "male",
"birthDate": "1977-01-12"
},
"search": {
"mode": "match"
},
"response": {
"status": "201 Created",
"etag": "W/\"1\""
}
}
]
}

0 comments on commit d1c0ed4

Please sign in to comment.