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

Mock the Census API for tests so that Actions doesn't actually hit the API when running tests #864

Merged
merged 22 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bf8fc11
first pass at conversion function; will add tests next
robertmitchellv Oct 10, 2023
beabcb2
Merge branch 'main' into robert/convert-datetime-to-str-link-py
robertmitchellv Oct 11, 2023
693f92e
added function to init
robertmitchellv Oct 11, 2023
e8e53e2
tests for the datetime_to_str added
robertmitchellv Oct 11, 2023
397364e
forgot to add to __all__
robertmitchellv Oct 11, 2023
aa96e7d
added a param to include time in the output with tests
robertmitchellv Oct 11, 2023
7066b8f
separated the line that was too long
robertmitchellv Oct 11, 2023
545e0cb
Merge branch 'main' into robert/convert-datetime-to-str-link-py
robertmitchellv Oct 11, 2023
03b255a
Merge branch 'main' into robert/convert-datetime-to-str-link-py
robertmitchellv Oct 12, 2023
e7cb95b
removed functions from phdi.linkage
robertmitchellv Oct 12, 2023
e8c5861
add function to container utils
robertmitchellv Oct 12, 2023
5db5985
added mocking of census api with pytest monkeypatch
robertmitchellv Oct 13, 2023
d5880b5
Merge branch 'main' into robert/mock-census-api-tests
robertmitchellv Oct 13, 2023
520659b
Merge branch 'main' into robert/mock-census-api-tests
robertmitchellv Oct 13, 2023
6884c3a
added geocoded_response as fixture
robertmitchellv Oct 16, 2023
3729217
finished mocking the fhir census geocoder
robertmitchellv Oct 16, 2023
424661d
forgot to run black
robertmitchellv Oct 16, 2023
7c09f3a
Merge branch 'main' into robert/mock-census-api-tests
robertmitchellv Oct 16, 2023
90ac0a6
Merge branch 'main' into robert/mock-census-api-tests
robertmitchellv Oct 16, 2023
0424623
removed duplicate mocking
robertmitchellv Oct 17, 2023
9e4dde9
removed extra line
robertmitchellv Oct 17, 2023
6a4bf40
removed extra comment
robertmitchellv Oct 17, 2023
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
1 change: 1 addition & 0 deletions containers/record-linkage/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import subprocess
from typing import Literal


robertmitchellv marked this conversation as resolved.
Show resolved Hide resolved
from app.config import get_settings
from phdi.linkage import DIBBsConnectorClient

Expand Down
235 changes: 96 additions & 139 deletions tests/fhir/geospatial/test_fhir_census_client.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
from unittest import mock
import copy
import json
import pathlib
import copy
import pytest

from phdi.fhir.geospatial.census import CensusFhirGeocodeClient
from phdi.geospatial.core import GeocodeResult


def test_geocode_resource_census():
census_client = CensusFhirGeocodeClient()
assert census_client is not None
FHIR_BUNDLE_PATH = pathlib.Path(__file__).parent.parent.parent / "assets" / "general"


@pytest.fixture
def patient_bundle_census():
with open(FHIR_BUNDLE_PATH / "patient_bundle_census.json") as file:
return json.load(file)


@pytest.fixture
def patient_bundle_census_extension():
with open(FHIR_BUNDLE_PATH / "patient_bundle_census_extension.json") as file:
return json.load(file)


geocoded_response = GeocodeResult(
@pytest.fixture
def geocoded_response():
return GeocodeResult(
line=["239 Greene St", "Apt 4L"],
city="NEW YORK",
state="NY",
Expand All @@ -23,17 +36,28 @@ def test_geocode_resource_census():
census_tract="59",
)

bundle = json.load(
open(
pathlib.Path(__file__).parent.parent.parent
/ "assets"
/ "general"
/ "patient_bundle_census.json"
)

@pytest.fixture
def census_client(monkeypatch, geocoded_response):
client = CensusFhirGeocodeClient()

def mock_geocode_from_dict(*args, **kwargs):
return geocoded_response

monkeypatch.setattr(
client._CensusFhirGeocodeClient__client,
"geocode_from_dict",
mock_geocode_from_dict,
)

patient = bundle["entry"][1]["resource"]
standardized_patient = copy.deepcopy(patient)
return client


def _get_patient_from_bundle(bundle):
return bundle["entry"][1]["resource"]


def _extract_address(standardized_patient, geocoded_response, extension=False):
address = standardized_patient["address"][0]
address["city"] = geocoded_response.city
address["state"] = geocoded_response.state
Expand All @@ -48,31 +72,61 @@ def test_geocode_resource_census():
],
}
)
address["_line"] = []
address["_line"].append(
{
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/"
+ "iso21090-ADXP-censusTract",
"valueString": "59",
}
]
},
)
address["_line"].append(
{
if extension:
address["_line"][0]["extension"].append(
{
"url": "http://hl7.org/fhir/StructureDefinition/"
+ "iso21090-ADXP-censusTract",
"valueString": "59",
}
)
address["_line"][1] = {
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/"
+ "iso21090-ADXP-censusTract",
"valueString": "59",
}
]
},
)
census_client.geocode_from_str = mock.Mock()
census_client.geocode_from_str.return_value = geocoded_response
}
else:
address["_line"] = []
address["_line"].append(
{
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/"
+ "iso21090-ADXP-censusTract",
"valueString": "59",
}
]
},
)
address["_line"].append(
{
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/"
+ "iso21090-ADXP-censusTract",
"valueString": "59",
}
]
},
)
return address


def test_geocode_resource_census(
patient_bundle_census,
patient_bundle_census_extension,
geocoded_response,
census_client,
):
assert census_client is not None

patient = _get_patient_from_bundle(patient_bundle_census)
standardized_patient = copy.deepcopy(patient)
_extract_address(standardized_patient, geocoded_response)

# Case 1: Overwrite = False
returned_patient = census_client.geocode_resource(patient, overwrite=False)
Expand All @@ -89,48 +143,9 @@ def test_geocode_resource_census():
assert returned_patient == patient

# Case 3: Patient already has an extension on line, and it's preserved.
bundle = json.load(
open(
pathlib.Path(__file__).parent.parent.parent
/ "assets"
/ "general"
/ "patient_bundle_census_extension.json"
)
)

patient = bundle["entry"][1]["resource"]
patient = _get_patient_from_bundle(patient_bundle_census_extension)
standardized_patient = copy.deepcopy(patient)

address = standardized_patient["address"][0]
address["city"] = geocoded_response.city
address["state"] = geocoded_response.state
address["postalCode"] = geocoded_response.postal_code
address["extension"] = []
address["extension"].append(
{
"url": "http://hl7.org/fhir/StructureDefinition/geolocation",
"extension": [
{"url": "latitude", "valueDecimal": geocoded_response.lat},
{"url": "longitude", "valueDecimal": geocoded_response.lng},
],
}
)
address["_line"][0]["extension"].append(
{
"url": "http://hl7.org/fhir/StructureDefinition/"
+ "iso21090-ADXP-censusTract",
"valueString": "59",
}
)
address["_line"][1] = {
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/"
+ "iso21090-ADXP-censusTract",
"valueString": "59",
}
]
}
_extract_address(standardized_patient, geocoded_response, extension=True)

# Case 3: Patient already has 1 extension on _line, and it's preserved.
returned_patient = census_client.geocode_resource(patient, overwrite=True)
Expand All @@ -145,73 +160,15 @@ def test_geocode_resource_census():
)


def test_geocode_bundle_census():
census_client = CensusFhirGeocodeClient()
def test_geocode_bundle_census(patient_bundle_census, geocoded_response, census_client):
assert census_client is not None

geocoded_response = GeocodeResult(
line=["239 Greene St", "Apt 4L"],
city="NEW YORK",
state="NY",
lat=40.729656537689166,
lng=-73.99550002689155,
county_fips="36061",
county_name="New York",
postal_code="10003",
census_tract="59",
)
standardized_bundle = copy.deepcopy(patient_bundle_census)
patient = _get_patient_from_bundle(standardized_bundle)
_extract_address(patient, geocoded_response)

bundle = json.load(
open(
pathlib.Path(__file__).parent.parent.parent
/ "assets"
/ "general"
/ "patient_bundle_census.json"
)
returned_bundle = census_client.geocode_bundle(
patient_bundle_census, overwrite=False
)
standardized_bundle = copy.deepcopy(bundle)
patient = standardized_bundle["entry"][1]["resource"]
address = patient["address"][0]
address["city"] = geocoded_response.city
address["state"] = geocoded_response.state
address["postalCode"] = geocoded_response.postal_code
address["extension"] = []
address["extension"].append(
{
"url": "http://hl7.org/fhir/StructureDefinition/geolocation",
"extension": [
{"url": "latitude", "valueDecimal": geocoded_response.lat},
{"url": "longitude", "valueDecimal": geocoded_response.lng},
],
}
)
address["_line"] = []
address["_line"].append(
{
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/"
+ "iso21090-ADXP-censusTract",
"valueString": "59",
}
]
},
)
address["_line"].append(
{
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/"
+ "iso21090-ADXP-censusTract",
"valueString": "59",
}
]
},
)

census_client.geocode_from_str = mock.Mock()
census_client.geocode_from_str.return_value = geocoded_response

returned_bundle = census_client.geocode_bundle(bundle, overwrite=False)
assert standardized_bundle == returned_bundle
assert bundle != standardized_bundle
assert patient_bundle_census != standardized_bundle
Loading
Loading