Skip to content

Commit 2a08ecd

Browse files
committed
Add tests and comments that provide some additional clarity regarding the flattening logic in response to PR comments.
1 parent fa8fa60 commit 2a08ecd

File tree

1 file changed

+53
-0
lines changed
  • instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/utils

1 file changed

+53
-0
lines changed

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/utils/test_dict_util.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,56 @@ def flatten_not_json_serializable(key, value, **kwargs):
222222
assert output == {
223223
"cannot_serialize_directly": "blah",
224224
}
225+
226+
def test_flatten_simple_homogenous_primitive_string_list():
227+
input_dict = {
228+
"list_value": ["abc", "def"]
229+
}
230+
assert dict_util.flatten_dict(input_dict) == input_dict
231+
232+
def test_flatten_simple_homogenous_primitive_int_list():
233+
input_dict = {
234+
"list_value": [123, 456]
235+
}
236+
assert dict_util.flatten_dict(input_dict) == input_dict
237+
238+
def test_flatten_simple_homogenous_primitive_bool_list():
239+
input_dict = {
240+
"list_value": [True, False]
241+
}
242+
assert dict_util.flatten_dict(input_dict) == input_dict
243+
244+
def test_flatten_simple_heterogenous_primitive_list():
245+
input_dict = {
246+
"list_value": ["abc", 123]
247+
}
248+
assert dict_util.flatten_dict(input_dict) == {
249+
"list_value.length": 2,
250+
"list_value[0]": "abc",
251+
"list_value[1]": 123,
252+
}
253+
254+
def test_flatten_list_of_compound_types():
255+
input_dict = {
256+
"list_value": [
257+
{"a": 1, "b": 2},
258+
{"x": 100, "y": 123, "z": 321},
259+
"blah",
260+
[
261+
"abc",
262+
123,
263+
],
264+
]
265+
}
266+
assert dict_util.flatten_dict(input_dict) == {
267+
"list_value.length": 4,
268+
"list_value[0].a": 1,
269+
"list_value[0].b": 2,
270+
"list_value[1].x": 100,
271+
"list_value[1].y": 123,
272+
"list_value[1].z": 321,
273+
"list_value[2]": "blah",
274+
"list_value[3].length": 2,
275+
"list_value[3][0]": "abc",
276+
"list_value[3][1]": 123,
277+
}

0 commit comments

Comments
 (0)