|
21 | 21 | Fixes https://github.com/google/adk-python/issues/5412 |
22 | 22 | """ |
23 | 23 |
|
24 | | -import pytest |
25 | | - |
26 | 24 | from google.adk.models.lite_llm import _safe_json_serialize |
| 25 | +import pytest |
27 | 26 |
|
28 | 27 |
|
29 | 28 | def test_circular_reference_returns_str_fallback(): |
30 | | - """json.dumps raises ValueError on circular references; should fall back to str().""" |
| 29 | + """json.dumps raises ValueError on circular references; should fall back to str().""" |
| 30 | + |
| 31 | + class Node: |
31 | 32 |
|
32 | | - class Node: |
33 | | - def __init__(self): |
34 | | - self.ref = self |
| 33 | + def __init__(self): |
| 34 | + self.ref = self |
35 | 35 |
|
36 | | - obj = Node() |
37 | | - result = _safe_json_serialize(obj) |
38 | | - assert isinstance(result, str) |
39 | | - # Should return str(obj) fallback rather than raising ValueError |
| 36 | + obj = Node() |
| 37 | + result = _safe_json_serialize(obj) |
| 38 | + assert isinstance(result, str) |
| 39 | + # Should return str(obj) fallback rather than raising ValueError |
40 | 40 |
|
41 | 41 |
|
42 | 42 | def test_deeply_nested_structure_returns_str_fallback(): |
43 | | - """json.dumps raises RecursionError on deeply nested structures.""" |
44 | | - obj = current = {} |
45 | | - for _ in range(10000): |
46 | | - current["child"] = {} |
47 | | - current = current["child"] |
| 43 | + """json.dumps raises RecursionError on deeply nested structures.""" |
| 44 | + obj = current = {} |
| 45 | + for _ in range(10000): |
| 46 | + current["child"] = {} |
| 47 | + current = current["child"] |
48 | 48 |
|
49 | | - result = _safe_json_serialize(obj) |
50 | | - assert isinstance(result, str) |
51 | | - # str(obj) itself can raise RecursionError, so expect the safe fallback |
52 | | - assert "recursion" in result.lower() or result # non-empty string |
| 49 | + result = _safe_json_serialize(obj) |
| 50 | + assert isinstance(result, str) |
| 51 | + # str(obj) itself can raise RecursionError, so expect the safe fallback |
| 52 | + assert "recursion" in result.lower() or result # non-empty string |
53 | 53 |
|
54 | 54 |
|
55 | 55 | def test_normal_dict_serializes(): |
56 | | - """Normal dicts should serialize as JSON.""" |
57 | | - result = _safe_json_serialize({"key": "value", "num": 42}) |
58 | | - assert '"key"' in result |
59 | | - assert '"value"' in result |
| 56 | + """Normal dicts should serialize as JSON.""" |
| 57 | + result = _safe_json_serialize({"key": "value", "num": 42}) |
| 58 | + assert '"key"' in result |
| 59 | + assert '"value"' in result |
60 | 60 |
|
61 | 61 |
|
62 | 62 | def test_non_serializable_object_falls_back_to_str(): |
63 | | - """Objects without a JSON representation should fall back to str().""" |
64 | | - obj = object() |
65 | | - result = _safe_json_serialize(obj) |
66 | | - assert isinstance(result, str) |
67 | | - assert "object" in result.lower() |
| 63 | + """Objects without a JSON representation should fall back to str().""" |
| 64 | + obj = object() |
| 65 | + result = _safe_json_serialize(obj) |
| 66 | + assert isinstance(result, str) |
| 67 | + assert "object" in result.lower() |
0 commit comments