Skip to content

Commit 56f6be1

Browse files
authored
Merge pull request #397 from ansforge/feat/refactor-inplace-conversion
Feat/refactor inplace conversion
2 parents 5f8a2d8 + 1d71297 commit 56f6be1

2 files changed

Lines changed: 60 additions & 36 deletions

File tree

converter/converter/cisu/resources_info/resources_info_cisu_converter.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -276,20 +276,6 @@ def _convert_resources_to_cisu(
276276
logger.debug("Processing resource: {resource}")
277277

278278
try:
279-
vehicle_type = cls.translate_to_cisu_vehicle_type(
280-
get_field_value(
281-
resource, ResourcesInfoCISUConstants.VEHICLE_TYPE_PATH
282-
)
283-
)
284-
if vehicle_type is None:
285-
continue
286-
287-
set_value(
288-
resource,
289-
ResourcesInfoCISUConstants.VEHICLE_TYPE_PATH,
290-
vehicle_type,
291-
)
292-
293279
current_resource_path = (
294280
f"{ResourcesInfoCISUConstants.RESOURCE_PATH}[{index}]"
295281
)
@@ -300,26 +286,53 @@ def _convert_resources_to_cisu(
300286
"Transforming state to singleton for CISU at path %s",
301287
current_state_path,
302288
)
303-
cls.keep_last_state(resource)
304-
delete_paths(resource, [ResourcesInfoCISUConstants.PATIENT_ID_KEY])
289+
290+
cls._translate_to_cisu_vehicle_type(resource)
291+
cls._keep_last_state(resource)
292+
cls._remove_patient_id(resource)
293+
305294
converted_resources.append(resource)
306295
except ConversionError:
307296
continue
308297

309298
return converted_resources
310299

311300
@classmethod
312-
def translate_to_cisu_vehicle_type(cls, rs_vehicle_type: str) -> str | None:
301+
def _remove_patient_id(cls, resource):
302+
delete_paths(resource, [ResourcesInfoCISUConstants.PATIENT_ID_KEY])
303+
304+
@classmethod
305+
def _translate_to_cisu_vehicle_type(cls, resource: Dict[str, Any]) -> None:
313306
"""Translate a RS vehicle type to its CISU equivalent, or None if not mappable."""
314-
if rs_vehicle_type.startswith(ResourcesInfoCISUConstants.VEHICLE_TYPE_SIS):
315-
return ResourcesInfoCISUConstants.VEHICLE_TYPE_SIS
316-
if rs_vehicle_type.startswith(ResourcesInfoCISUConstants.VEHICLE_TYPE_SMUR):
317-
return ResourcesInfoCISUConstants.VEHICLE_TYPE_SMUR
318-
logger.info("vehicleType '%s' is not mappable to CISU", rs_vehicle_type)
319-
return None
307+
308+
vehicle_type = get_field_value(
309+
resource, ResourcesInfoCISUConstants.VEHICLE_TYPE_PATH
310+
)
311+
312+
if vehicle_type is None:
313+
raise ConversionError(
314+
f"No vehicle found in RS resource for resource: {resource.get('resourceId')}."
315+
)
316+
317+
if vehicle_type.startswith(ResourcesInfoCISUConstants.VEHICLE_TYPE_SIS):
318+
set_value(
319+
resource,
320+
ResourcesInfoCISUConstants.VEHICLE_TYPE_PATH,
321+
ResourcesInfoCISUConstants.VEHICLE_TYPE_SIS,
322+
)
323+
elif vehicle_type.startswith(ResourcesInfoCISUConstants.VEHICLE_TYPE_SMUR):
324+
set_value(
325+
resource,
326+
ResourcesInfoCISUConstants.VEHICLE_TYPE_PATH,
327+
ResourcesInfoCISUConstants.VEHICLE_TYPE_SMUR,
328+
)
329+
else:
330+
raise ConversionError(
331+
f"No valid vehicle found for resource: {resource.get('resourceId')}."
332+
)
320333

321334
@classmethod
322-
def keep_last_state(cls, resource: Dict[str, Any]) -> None:
335+
def _keep_last_state(cls, resource: Dict[str, Any]) -> None:
323336
states = get_field_value(resource, ResourcesInfoCISUConstants.STATE_PATH)
324337
if not states or len(states) == 0:
325338
raise ConversionError(

converter/tests/cisu/test_resources_info_converter.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from converter.cisu.resources_info.resources_info_cisu_converter import (
8+
ConversionError,
89
ResourcesInfoCISUConverter,
910
)
1011
from tests.constants import TestConstants
@@ -120,21 +121,21 @@ def test_keep_latest_state_should_keep_latest(self):
120121
{"datetime": "2025-01-02T08:00:00Z"},
121122
]
122123
}
123-
ResourcesInfoCISUConverter.keep_last_state(resource)
124+
ResourcesInfoCISUConverter._keep_last_state(resource)
124125

125126
expected_resource = {"state": {"datetime": "2025-01-02T12:00:00Z"}}
126127

127128
assert resource == expected_resource
128129

129130
def test_keep_last_state_should_raise_exception_if_state_empty(self):
130-
with pytest.raises(ValueError):
131+
with pytest.raises(ConversionError):
131132
resource = {"state": []}
132-
ResourcesInfoCISUConverter.keep_last_state(resource)
133+
ResourcesInfoCISUConverter._keep_last_state(resource)
133134

134135
def test_keep_last_state_should_raise_exception_if_state_undefined(self):
135-
with pytest.raises(ValueError):
136+
with pytest.raises(ConversionError):
136137
resource = {}
137-
ResourcesInfoCISUConverter.keep_last_state(resource)
138+
ResourcesInfoCISUConverter._keep_last_state(resource)
138139

139140

140141
def test_cisu_to_rs_breaking_changes():
@@ -164,16 +165,26 @@ def test_cisu_to_rs_breaking_changes():
164165
pytest.param("SIS.DRAGON", "SIS", id="translates SIS.DRAGON to SIS"),
165166
pytest.param("SMUR", "SMUR", id="translates SMUR to SMUR"),
166167
pytest.param("SMUR.VLM", "SMUR", id="translates SMUR.VLM to SMUR"),
167-
pytest.param("AUTREVEC", None, id="AUTREVEC is not mappable to CISU"),
168-
pytest.param("FSI.HELIFSI ", None, id="FSI.HELIFSI is not mappable to CISU"),
169-
pytest.param("TSU.VSL", None, id="TSU.VSL is not mappable to CISU"),
170168
],
171169
)
172170
def test_translate_vehicule_type_to_cisu(rs_vehicule_type, expected):
173-
cisu_vehicle_type = ResourcesInfoCISUConverter.translate_to_cisu_vehicle_type(
174-
rs_vehicule_type
175-
)
176-
assert cisu_vehicle_type == expected
171+
resource = {"vehicleType": rs_vehicule_type}
172+
ResourcesInfoCISUConverter._translate_to_cisu_vehicle_type(resource)
173+
assert resource["vehicleType"] == expected
174+
175+
176+
@pytest.mark.parametrize(
177+
"rs_vehicule_type",
178+
[
179+
pytest.param("AUTREVEC", id="AUTREVEC is not mappable to CISU"),
180+
pytest.param("FSI.HELIFSI", id="FSI.HELIFSI is not mappable to CISU"),
181+
pytest.param("TSU.VSL", id="TSU.VSL is not mappable to CISU"),
182+
],
183+
)
184+
def test_translate_vehicule_type_to_cisu_raises_on_unmappable(rs_vehicule_type):
185+
resource = {"vehicleType": rs_vehicule_type}
186+
with pytest.raises(ConversionError):
187+
ResourcesInfoCISUConverter._translate_to_cisu_vehicle_type(resource)
177188

178189

179190
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)