|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from pydantic import BaseModel, RootModel |
| 4 | + |
| 5 | +from uipath_langchain.agent.react.json_utils import ( |
| 6 | + extract_values_by_paths, |
| 7 | + get_json_paths_by_type, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +class Target(BaseModel): |
| 12 | + id: str |
| 13 | + |
| 14 | + |
| 15 | +class Nested(BaseModel): |
| 16 | + target: Target |
| 17 | + name: str |
| 18 | + |
| 19 | + |
| 20 | +class WithList(BaseModel): |
| 21 | + items: list[Target] |
| 22 | + |
| 23 | + |
| 24 | +class WithOptional(BaseModel): |
| 25 | + target: Optional[Target] = None |
| 26 | + |
| 27 | + |
| 28 | +class WithNestedList(BaseModel): |
| 29 | + matrix: list[list[Target]] |
| 30 | + |
| 31 | + |
| 32 | +class Mixed(BaseModel): |
| 33 | + direct: Target |
| 34 | + items: list[Target] |
| 35 | + nested: Nested |
| 36 | + plain: str |
| 37 | + |
| 38 | + |
| 39 | +# -- get_json_paths_by_type: regular BaseModel --------------------------------- |
| 40 | + |
| 41 | + |
| 42 | +class TestGetJsonPathsByType: |
| 43 | + def test_direct_field(self): |
| 44 | + paths = get_json_paths_by_type(Nested, "Target") |
| 45 | + assert paths == ["$.target"] |
| 46 | + |
| 47 | + def test_list_field(self): |
| 48 | + paths = get_json_paths_by_type(WithList, "Target") |
| 49 | + assert paths == ["$.items[*]"] |
| 50 | + |
| 51 | + def test_optional_field(self): |
| 52 | + paths = get_json_paths_by_type(WithOptional, "Target") |
| 53 | + assert paths == ["$.target"] |
| 54 | + |
| 55 | + def test_nested_list_of_lists(self): |
| 56 | + paths = get_json_paths_by_type(WithNestedList, "Target") |
| 57 | + assert paths == ["$.matrix[*][*]"] |
| 58 | + |
| 59 | + def test_mixed_fields(self): |
| 60 | + paths = get_json_paths_by_type(Mixed, "Target") |
| 61 | + assert set(paths) == {"$.direct", "$.items[*]", "$.nested.target"} |
| 62 | + |
| 63 | + def test_no_match(self): |
| 64 | + class Unrelated(BaseModel): |
| 65 | + name: str |
| 66 | + value: int |
| 67 | + |
| 68 | + paths = get_json_paths_by_type(Unrelated, "Target") |
| 69 | + assert paths == [] |
| 70 | + |
| 71 | + def test_nested_model_in_list(self): |
| 72 | + class Outer(BaseModel): |
| 73 | + groups: list[Nested] |
| 74 | + |
| 75 | + paths = get_json_paths_by_type(Outer, "Target") |
| 76 | + assert paths == ["$.groups[*].target"] |
| 77 | + |
| 78 | + |
| 79 | +# -- get_json_paths_by_type: RootModel ----------------------------------------- |
| 80 | + |
| 81 | + |
| 82 | +class TestGetJsonPathsByTypeRootModel: |
| 83 | + def test_root_model_single_object(self): |
| 84 | + Model = RootModel[Target] |
| 85 | + paths = get_json_paths_by_type(Model, "Target") |
| 86 | + assert paths == [] |
| 87 | + |
| 88 | + def test_root_model_list_of_target(self): |
| 89 | + """Target is the leaf type itself — no nested fields of type Target within it.""" |
| 90 | + Model = RootModel[list[Target]] |
| 91 | + paths = get_json_paths_by_type(Model, "Target") |
| 92 | + assert paths == [] |
| 93 | + |
| 94 | + def test_root_model_list_of_nested(self): |
| 95 | + Model = RootModel[list[Nested]] |
| 96 | + paths = get_json_paths_by_type(Model, "Target") |
| 97 | + assert paths == ["$[*].target"] |
| 98 | + |
| 99 | + def test_root_model_list_of_list(self): |
| 100 | + Model = RootModel[list[list[Nested]]] |
| 101 | + paths = get_json_paths_by_type(Model, "Target") |
| 102 | + assert paths == ["$[*][*].target"] |
| 103 | + |
| 104 | + def test_root_model_primitive(self): |
| 105 | + Model = RootModel[str] |
| 106 | + paths = get_json_paths_by_type(Model, "Target") |
| 107 | + assert paths == [] |
| 108 | + |
| 109 | + def test_root_model_list_of_primitives(self): |
| 110 | + Model = RootModel[list[str]] |
| 111 | + paths = get_json_paths_by_type(Model, "Target") |
| 112 | + assert paths == [] |
| 113 | + |
| 114 | + def test_root_model_optional_list(self): |
| 115 | + Model = RootModel[Optional[list[Nested]]] |
| 116 | + paths = get_json_paths_by_type(Model, "Target") |
| 117 | + assert paths == ["$[*].target"] |
| 118 | + |
| 119 | + |
| 120 | +# -- extract_values_by_paths --------------------------------------------------- |
| 121 | + |
| 122 | + |
| 123 | +class TestExtractValuesByPaths: |
| 124 | + def test_extract_from_dict(self): |
| 125 | + data = {"target": {"id": "1"}, "name": "test"} |
| 126 | + values = extract_values_by_paths(data, ["$.target"]) |
| 127 | + assert values == [{"id": "1"}] |
| 128 | + |
| 129 | + def test_extract_from_list(self): |
| 130 | + data = {"items": [{"id": "1"}, {"id": "2"}]} |
| 131 | + values = extract_values_by_paths(data, ["$.items[*]"]) |
| 132 | + assert values == [{"id": "1"}, {"id": "2"}] |
| 133 | + |
| 134 | + def test_extract_from_pydantic_model(self): |
| 135 | + obj = Nested(target=Target(id="1"), name="test") |
| 136 | + values = extract_values_by_paths(obj, ["$.target"]) |
| 137 | + assert values == [{"id": "1"}] |
| 138 | + |
| 139 | + def test_extract_multiple_paths(self): |
| 140 | + data = { |
| 141 | + "direct": {"id": "1"}, |
| 142 | + "items": [{"id": "2"}, {"id": "3"}], |
| 143 | + } |
| 144 | + values = extract_values_by_paths(data, ["$.direct", "$.items[*]"]) |
| 145 | + assert values == [{"id": "1"}, {"id": "2"}, {"id": "3"}] |
| 146 | + |
| 147 | + def test_extract_no_paths(self): |
| 148 | + values = extract_values_by_paths({"a": 1}, []) |
| 149 | + assert values == [] |
| 150 | + |
| 151 | + def test_extract_path_not_found(self): |
| 152 | + values = extract_values_by_paths({"a": 1}, ["$.missing"]) |
| 153 | + assert values == [] |
0 commit comments