|
1 | 1 | import typing |
2 | 2 | from dataclasses import dataclass |
| 3 | +from typing import Any |
3 | 4 |
|
4 | 5 | import pytest |
5 | 6 |
|
@@ -36,15 +37,15 @@ def simple_transform(text: cocoindex.DataSlice[str]) -> cocoindex.DataSlice[str] |
36 | 37 |
|
37 | 38 |
|
38 | 39 | @cocoindex.op.function() |
39 | | -def extract_value(child: int) -> int: |
40 | | - """Extracts the value from a Child object.""" |
41 | | - return child |
| 40 | +def extract_value(value: int) -> int: |
| 41 | + """Extracts the value.""" |
| 42 | + return value |
42 | 43 |
|
43 | 44 |
|
44 | 45 | @cocoindex.transform_flow() |
45 | 46 | def for_each_transform( |
46 | 47 | data: cocoindex.DataSlice[Parent], |
47 | | -) -> cocoindex.DataSlice[Parent]: |
| 48 | +) -> cocoindex.DataSlice[Any]: |
48 | 49 | """Transform flow that processes child rows to extract values.""" |
49 | 50 | with data["children"].row() as child: |
50 | 51 | child["new_field"] = child["value"].transform(extract_value) |
@@ -73,18 +74,30 @@ def test_for_each_transform_flow() -> None: |
73 | 74 | """Test the complex transform flow with child rows.""" |
74 | 75 | input_data = Parent(children=[Child(1), Child(2), Child(3)]) |
75 | 76 | result = for_each_transform.eval(input_data) |
76 | | - expected = Parent(children=[Child(1), Child(2), Child(3)]) |
| 77 | + expected = { |
| 78 | + "children": [ |
| 79 | + {"value": 1, "new_field": 1}, |
| 80 | + {"value": 2, "new_field": 2}, |
| 81 | + {"value": 3, "new_field": 3}, |
| 82 | + ] |
| 83 | + } |
77 | 84 | assert result == expected, f"Expected {expected}, got {result}" |
78 | 85 |
|
79 | 86 | input_data = Parent(children=[]) |
80 | 87 | result = for_each_transform.eval(input_data) |
81 | | - assert result == Parent(children=[]), f"Expected [], got {result}" |
| 88 | + assert result == {"children": []}, f"Expected {{'children': []}}, got {result}" |
82 | 89 |
|
83 | 90 |
|
84 | 91 | @pytest.mark.asyncio |
85 | 92 | async def test_for_each_transform_flow_async() -> None: |
86 | 93 | """Test the complex transform flow asynchronously.""" |
87 | 94 | input_data = Parent(children=[Child(4), Child(5)]) |
88 | 95 | result = await for_each_transform.eval_async(input_data) |
89 | | - expected = Parent(children=[Child(4), Child(5)]) |
| 96 | + expected = { |
| 97 | + "children": [ |
| 98 | + {"value": 4, "new_field": 4}, |
| 99 | + {"value": 5, "new_field": 5}, |
| 100 | + ] |
| 101 | + } |
| 102 | + |
90 | 103 | assert result == expected, f"Expected {expected}, got {result}" |
0 commit comments