Skip to content

Commit 2bf20f1

Browse files
committed
refactor(converter): extract switch field method from versions folder
1 parent b022ea5 commit 2bf20f1

File tree

7 files changed

+60
-57
lines changed

7 files changed

+60
-57
lines changed

converter/converter/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,17 @@ def add_object_to_medical_notes(
296296
}
297297

298298
json_data["medicalNote"].append(new_note)
299+
300+
301+
def switch_field_name(
302+
json_data: Dict[str, Any], previous_field_path: str, new_field_path: str
303+
):
304+
if is_field_completed(json_data, previous_field_path):
305+
value = get_field_value(json_data, previous_field_path)
306+
logger.info(
307+
"Transforming field name from %s to %s",
308+
previous_field_path,
309+
new_field_path,
310+
)
311+
set_value(json_data, new_field_path, value)
312+
delete_paths(json_data, [previous_field_path])

converter/converter/versions/create_case_health/create_case_health_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
get_field_value,
77
is_field_completed,
88
map_to_new_value,
9+
switch_field_name,
910
)
1011
from converter.versions.base_message_converter import BaseMessageConverter
1112
from converter.versions.create_case_health.v1_v2.constants import V1V2Constants
@@ -16,7 +17,6 @@
1617
from converter.versions.create_case_health.v2_v3.constants import V2V3Constants
1718
from converter.versions.utils import (
1819
reverse_map_to_new_value,
19-
switch_field_name,
2020
)
2121

2222

converter/converter/versions/resources_info/resources_info_converter.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
from typing import Dict, Any
22
import logging
33

4-
from converter.utils import delete_paths, get_field_value, map_to_new_value, set_value
4+
from converter.utils import (
5+
delete_paths,
6+
get_field_value,
7+
map_to_new_value,
8+
set_value,
9+
switch_field_name,
10+
)
511
from converter.versions.base_message_converter import BaseMessageConverter
612
from converter.versions.resources_info.resources_info_constants import (
713
ResourcesInfoConstants,
814
)
9-
from converter.versions.utils import reverse_map_to_new_value, switch_field_name
15+
from converter.versions.utils import reverse_map_to_new_value
1016

1117
logger = logging.getLogger(__name__)
1218

converter/converter/versions/resources_status/resources_status_converter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from typing import Dict, Any
22

3-
from converter.utils import map_to_new_value
3+
from converter.utils import map_to_new_value, switch_field_name
44
from converter.versions.base_message_converter import BaseMessageConverter
55
from converter.versions.resources_status.resources_status_constants import (
66
ResourcesStatusConstants,
77
)
8-
from converter.versions.utils import reverse_map_to_new_value, switch_field_name
8+
from converter.versions.utils import reverse_map_to_new_value
99

1010

1111
class ResourcesStatusConverter(BaseMessageConverter):

converter/converter/versions/utils.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
from converter.utils import (
55
get_field_value,
6-
is_field_completed,
76
update_json_value,
8-
delete_paths,
9-
set_value,
107
)
118

129
logger = logging.getLogger(__name__)
@@ -31,20 +28,6 @@ def reverse_map_to_new_value(
3128
update_json_value(json_data, json_path, new_value)
3229

3330

34-
def switch_field_name(
35-
json_data: Dict[str, Any], previous_field_path: str, new_field_path: str
36-
):
37-
if is_field_completed(json_data, previous_field_path):
38-
value = get_field_value(json_data, previous_field_path)
39-
logger.info(
40-
"Transforming field name from %s to %s",
41-
previous_field_path,
42-
new_field_path,
43-
)
44-
set_value(json_data, new_field_path, value)
45-
delete_paths(json_data, [previous_field_path])
46-
47-
4831
def convert_to_float(value: Optional[str]) -> Optional[float]:
4932
if value is None:
5033
return None

converter/tests/test_utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
update_json_value,
1212
set_value,
1313
extract_message_type_from_message_content,
14+
switch_field_name,
1415
)
1516
import unittest
1617
import json
@@ -524,3 +525,37 @@ def test_non_string_values_are_supported(self):
524525
"status": "ok",
525526
}
526527
assert extract_message_type_from_message_content(content) == "createCaseHealth"
528+
529+
530+
class TestSwitchFieldName(unittest.TestCase):
531+
def test_switch_field_name(self):
532+
json_data = {"oldFieldName": "value1", "otherField": "value2"}
533+
previous_field_name = "oldFieldName"
534+
new_field_name = "newFieldName"
535+
previous_field_path = f"$.{previous_field_name}"
536+
new_field_path = f"$.{new_field_name}"
537+
538+
self.assertNotIn(new_field_name, json_data)
539+
self.assertIn(previous_field_name, json_data)
540+
541+
switch_field_name(json_data, previous_field_path, new_field_path)
542+
543+
self.assertIn(new_field_name, json_data)
544+
self.assertEqual(json_data[new_field_name], "value1")
545+
self.assertNotIn(previous_field_name, json_data)
546+
547+
def test_switch_field_name_with_nested_field(self):
548+
json_data = {"parent": {"oldFieldName": "value1"}, "otherField": "value2"}
549+
previous_field_name = "oldFieldName"
550+
new_field_name = "newFieldName"
551+
previous_field_path = f"$.parent.{previous_field_name}"
552+
new_field_path = f"$.parent.{new_field_name}"
553+
554+
self.assertNotIn(new_field_name, json_data["parent"])
555+
self.assertIn(previous_field_name, json_data["parent"])
556+
557+
switch_field_name(json_data, previous_field_path, new_field_path)
558+
559+
self.assertIn(new_field_name, json_data["parent"])
560+
self.assertEqual(json_data["parent"][new_field_name], "value1")
561+
self.assertNotIn(previous_field_name, json_data["parent"])

converter/tests/versions/test_utils.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,9 @@
22

33
from converter.versions.utils import (
44
reverse_map_to_new_value,
5-
switch_field_name,
65
)
76

87

9-
class TestSwitchFieldName(unittest.TestCase):
10-
def test_switch_field_name(self):
11-
json_data = {"oldFieldName": "value1", "otherField": "value2"}
12-
previous_field_name = "oldFieldName"
13-
new_field_name = "newFieldName"
14-
previous_field_path = f"$.{previous_field_name}"
15-
new_field_path = f"$.{new_field_name}"
16-
17-
self.assertNotIn(new_field_name, json_data)
18-
self.assertIn(previous_field_name, json_data)
19-
20-
switch_field_name(json_data, previous_field_path, new_field_path)
21-
22-
self.assertIn(new_field_name, json_data)
23-
self.assertEqual(json_data[new_field_name], "value1")
24-
self.assertNotIn(previous_field_name, json_data)
25-
26-
def test_switch_field_name_with_nested_field(self):
27-
json_data = {"parent": {"oldFieldName": "value1"}, "otherField": "value2"}
28-
previous_field_name = "oldFieldName"
29-
new_field_name = "newFieldName"
30-
previous_field_path = f"$.parent.{previous_field_name}"
31-
new_field_path = f"$.parent.{new_field_name}"
32-
33-
self.assertNotIn(new_field_name, json_data["parent"])
34-
self.assertIn(previous_field_name, json_data["parent"])
35-
36-
switch_field_name(json_data, previous_field_path, new_field_path)
37-
38-
self.assertIn(new_field_name, json_data["parent"])
39-
self.assertEqual(json_data["parent"][new_field_name], "value1")
40-
self.assertNotIn(previous_field_name, json_data["parent"])
41-
42-
438
class TestReverseMapToNewValue(unittest.TestCase):
449
def test_reverse_map_to_new_value_with_valid_mapping(self):
4510
json_data = {"field": "value1"}

0 commit comments

Comments
 (0)