Skip to content

Commit

Permalink
Make Age and Duration extensions lower case for urls (#29)
Browse files Browse the repository at this point in the history
* Change 'age' and 'extension' urls to be lower case, keep Uppercase for python class names

* Update .gitignore

* Increase test coverage
  • Loading branch information
pipliggins committed May 13, 2024
1 parent c6d2738 commit 8ee2e50
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 24 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
cover/
cover/

.DS_Store
4 changes: 2 additions & 2 deletions fhirflat/resources/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class Duration(_DataType):

resource_type = Field("Duration", const=True)

url = Field("Duration", const=True, alias="url")
url = Field("duration", const=True, alias="url")

valueQuantity: fhirtypes.QuantityType = Field(
None,
Expand Down Expand Up @@ -352,7 +352,7 @@ class Age(_DataType):

resource_type = Field("Age", const=True)

url = Field("Age", const=True, alias="url")
url = Field("age", const=True, alias="url")

valueQuantity: fhirtypes.QuantityType = Field(
None,
Expand Down
2 changes: 1 addition & 1 deletion fhirflat/resources/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Patient(Patient, FHIRFlatBase):
title="Additional content defined by implementations",
description=(
"""
Contains the G.H 'Age' and 'birthSex' extensions,
Contains the G.H 'age' and 'birthSex' extensions,
and allows extensions from other implementations to be included."""
),
# if property is element of this resource.
Expand Down
4 changes: 2 additions & 2 deletions fhirflat/resources/procedure.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Procedure(_Procedure, FHIRFlatBase):
title="Additional content defined by implementations",
description=(
"""
Contains the G.H 'timingPhase', 'relativePeriod' and 'Duration' extensions,
Contains the G.H 'timingPhase', 'relativePeriod' and 'duration' extensions,
and allows extensions from other implementations to be included."""
),
# if property is element of this resource.
Expand Down Expand Up @@ -75,7 +75,7 @@ def validate_extension_contents(cls, extensions):

if duration_count > 1 or tim_phase_count > 1 or rel_phase_count > 1:
raise ValueError(
"Duration, timingPhase and relativePeriod can only appear once."
"duration, timingPhase and relativePeriod can only appear once."
)

return extensions
Expand Down
7 changes: 5 additions & 2 deletions fhirflat/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_fhirtype(t: str | list[str]):
if isinstance(t, list):
return [get_fhirtype(x) for x in t]

if not hasattr(extensions, t):
if not (hasattr(extensions, t) or hasattr(extensions, t.capitalize())):
try:
return getattr(getattr(fhir.resources, t.lower()), t)
except AttributeError:
Expand Down Expand Up @@ -61,4 +61,7 @@ def get_local_extension_type(t: str):
try:
return getattr(extensions, t)
except AttributeError:
raise AttributeError(f"Could not find {t} in fhirflat extensions")
try:
return getattr(extensions, t.capitalize())
except AttributeError:
raise AttributeError(f"Could not find {t} in fhirflat extensions")
Binary file modified tests/data/patient_ext_flat.parquet
Binary file not shown.
Binary file modified tests/data/procedure_flat.parquet
Binary file not shown.
14 changes: 7 additions & 7 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ def test_approximateDate(data, expected_type_date, expected_type_str):
assert type(approximate_date.valueString) is expected_type_str


dur = {"url": "Duration", "valueQuantity": {"value": 3, "unit": "days"}}
dur = {"url": "duration", "valueQuantity": {"value": 3, "unit": "days"}}


def test_Duration():
duration = Duration(**dur)
assert isinstance(duration, DataType)
assert duration.resource_type == "Duration"
assert duration.url == "Duration"
assert type(duration.valueQuantity) is _Quantity
def test_duration():
duration_inst = Duration(**dur)
assert isinstance(duration_inst, DataType)
assert duration_inst.resource_type == "Duration"
assert duration_inst.url == "duration"
assert type(duration_inst.valueQuantity) is _Quantity


dte = {"extension": [{"url": "approximateDate", "valueDate": "2021-01-01"}, rel_day]}
Expand Down
8 changes: 4 additions & 4 deletions tests/test_patient_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_bulk_fhir_to_flat_patient():
"id": "f001",
"active": True,
"extension": [
{"url": "Age", "valueQuantity": {"value": 25, "unit": "years"}},
{"url": "age", "valueQuantity": {"value": 25, "unit": "years"}},
{
"url": "birthSex",
"valueCodeableConcept": {
Expand All @@ -112,8 +112,8 @@ def test_bulk_fhir_to_flat_patient():
PATIENT_EXT_FLAT = {
"resourceType": "Patient",
"id": "f001",
"extension.Age.value": 25,
"extension.Age.unit": "years",
"extension.age.value": 25,
"extension.age.unit": "years",
"extension.birthSex.code": "http://snomed.info/sct|248152002",
"extension.birthSex.text": "Female (finding)",
"gender": "female",
Expand All @@ -123,7 +123,7 @@ def test_bulk_fhir_to_flat_patient():
PATIENT_EXT_DICT_OUT = {
"id": "f001",
"extension": [
{"url": "Age", "valueQuantity": {"value": 25, "unit": "years"}},
{"url": "age", "valueQuantity": {"value": 25, "unit": "years"}},
{
"url": "birthSex",
"valueCodeableConcept": {
Expand Down
8 changes: 4 additions & 4 deletions tests/test_procedure_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"instantiatesCanonical": ["http://example.org/fhir/PlanDefinition/KDN5"],
"status": "completed",
"extension": [
{"url": "Duration", "valueQuantity": {"value": 1, "unit": "d"}},
{"url": "duration", "valueQuantity": {"value": 1, "unit": "d"}},
{
"url": "timingPhase",
"valueCodeableConcept": {
Expand Down Expand Up @@ -73,8 +73,8 @@

PROCEDURE_FLAT = {
"resourceType": "Procedure",
"extension.Duration.value": 1,
"extension.Duration.unit": "d",
"extension.duration.value": 1,
"extension.duration.unit": "d",
"extension.timingPhase.code": "timing.com|1234",
"extension.timingPhase.text": None,
"extension.relativePeriod.relativeStart": 2,
Expand All @@ -97,7 +97,7 @@
"resourceType": "Procedure",
"status": "completed",
"extension": [
{"url": "Duration", "valueQuantity": {"value": 1.0, "unit": "d"}},
{"url": "duration", "valueQuantity": {"value": 1.0, "unit": "d"}},
{
"url": "relativePeriod",
"extension": [
Expand Down
3 changes: 2 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fhir.resources.codeableconcept import CodeableConcept
from fhir.resources.medicationstatement import MedicationStatementAdherence

from fhirflat.resources.extensions import dateTimeExtension
from fhirflat.resources.extensions import dateTimeExtension, Duration


def test_group_keys():
Expand Down Expand Up @@ -42,6 +42,7 @@ def test_group_keys():
("CodeableConcept", CodeableConcept),
("MedicationStatementAdherence", MedicationStatementAdherence),
("dateTimeExtension", dateTimeExtension),
("duration", Duration),
],
)
def test_get_fhirtype(input, expected):
Expand Down

0 comments on commit 8ee2e50

Please sign in to comment.