Skip to content

Commit da2d37a

Browse files
author
Malav Shastri
committed
Add unit tests for walk_and_apply_json changes
1 parent bca701f commit da2d37a

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

tests/unit/sagemaker/jumpstart/hub/test_utils.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from unittest.mock import patch, Mock
1616
from sagemaker.jumpstart.types import HubArnExtractedInfo
1717
from sagemaker.jumpstart.constants import JUMPSTART_DEFAULT_REGION_NAME
18-
from sagemaker.jumpstart.hub import utils
18+
from sagemaker.jumpstart.hub import parser_utils, utils
1919

2020

2121
def test_get_info_from_hub_resource_arn():
@@ -254,3 +254,50 @@ def test_get_hub_model_version_wildcard_char(mock_session):
254254
)
255255

256256
assert result == "2.0.0"
257+
258+
def test_walk_and_apply_json():
259+
test_json = {
260+
"CamelCaseKey": "value",
261+
"CamelCaseObjectKey": {
262+
"CamelCaseObjectChildOne": "value1",
263+
"CamelCaseObjectChildTwo": "value2",
264+
},
265+
"IgnoreMyChildren": {"ShouldNotBeTouchedOne": "const1", "ShouldNotBeTouchedTwo": "const2"},
266+
"ShouldNotIgnoreMyChildren": {"NopeNope": "no"},
267+
}
268+
269+
result = parser_utils.walk_and_apply_json(
270+
test_json, parser_utils.camel_to_snake, ["ignore_my_children"]
271+
)
272+
assert result == {
273+
"camel_case_key": "value",
274+
"camel_case_object_key": {
275+
"camel_case_object_child_one": "value1",
276+
"camel_case_object_child_two": "value2",
277+
},
278+
"ignore_my_children": {
279+
"ShouldNotBeTouchedOne": "const1",
280+
"ShouldNotBeTouchedTwo": "const2",
281+
},
282+
"should_not_ignore_my_children": {"nope_nope": "no"},
283+
}
284+
285+
def test_walk_and_apply_json_no_stop():
286+
test_json = {
287+
"CamelCaseKey": "value",
288+
"CamelCaseObjectKey": {
289+
"CamelCaseObjectChildOne": "value1",
290+
"CamelCaseObjectChildTwo": "value2",
291+
},
292+
"CamelCaseObjectListKey": {"instance.ml.type.xlarge": [{"ShouldChangeMe": "string"}]},
293+
}
294+
295+
result = parser_utils.walk_and_apply_json(test_json, parser_utils.camel_to_snake)
296+
assert result == {
297+
"camel_case_key": "value",
298+
"camel_case_object_key": {
299+
"camel_case_object_child_one": "value1",
300+
"camel_case_object_child_two": "value2",
301+
},
302+
"camel_case_object_list_key": {"instance.ml.type.xlarge": [{"should_change_me": "string"}]},
303+
}

0 commit comments

Comments
 (0)