1- from transform import to_boolean
1+ from transform import to_boolean , get_nested_value
22
33
44def test_to_boolean ():
@@ -19,3 +19,44 @@ def test_to_boolean():
1919 assert to_boolean (None ) is False
2020 assert to_boolean ([]) is False
2121 assert to_boolean ({}) is False
22+
23+
24+ def test_get_nested_value ():
25+ # Test case 1: Nested dictionary with string value
26+ assert get_nested_value ({"a" : {"b" : {"c" : "d" }}}, ["a" , "b" , "c" ]) == "d"
27+
28+ # Test case 2: Nested dictionary with integer value
29+ assert get_nested_value ({"a" : {"b" : {"c" : 1 }}}, ["a" , "b" , "c" ]) == 1
30+
31+ # Test case 3: Nested dictionary with float value
32+ assert get_nested_value ({"a" : {"b" : {"c" : 1.5 }}}, ["a" , "b" , "c" ]) == 1.5
33+
34+ # Test case 4: Nested dictionary with boolean value
35+ assert get_nested_value ({"a" : {"b" : {"c" : True }}}, ["a" , "b" , "c" ]) is True
36+
37+ # Test case 5: Nested dictionary with string value that needs trimming
38+ assert get_nested_value ({"a" : {"b" : {"c" : " d " }}}, ["a" , "b" , "c" ]) == "d"
39+
40+ # Test case 6: Key not found in the dictionary
41+ assert get_nested_value ({"a" : {"b" : {"c" : "d" }}}, ["a" , "b" , "x" ]) is None
42+ assert (
43+ get_nested_value ({"a" : {"b" : {"c" : "d" }}}, ["a" , "b" , "x" ], "default" )
44+ == "default"
45+ )
46+ assert get_nested_value ({"a" : {"b" : {"c" : "d" }}}, ["a" , "b" , "x" ], []) == []
47+
48+ # Test case 7: Intermediate key not found in the dictionary
49+ assert get_nested_value ({"a" : {"b" : {"c" : "d" }}}, ["a" , "x" , "c" ]) is None
50+ assert (
51+ get_nested_value ({"a" : {"b" : {"c" : "d" }}}, ["a" , "x" , "c" ], "default" )
52+ == "default"
53+ )
54+ assert get_nested_value ({"a" : {"b" : {"c" : "d" }}}, ["a" , "x" , "c" ], []) == []
55+
56+ # Test case 8: Empty keys list
57+ assert get_nested_value ({"a" : {"b" : {"c" : "d" }}}, []) is None
58+ assert get_nested_value ({"a" : {"b" : {"c" : "d" }}}, [], {}) == {}
59+
60+ # Test case 9: Non-dictionary data
61+ assert get_nested_value ("not a dict" , ["a" , "b" , "c" ]) is None
62+ assert get_nested_value ("not a dict" , ["a" , "b" , "c" ], []) == []
0 commit comments