Skip to content

Commit 2bbb30d

Browse files
committed
refactor: apply for_each iteration in the transform flow
1 parent 0070b68 commit 2bbb30d

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

python/cocoindex/tests/test_transform_flow.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,19 @@ def simple_transform(text: cocoindex.DataSlice[str]) -> cocoindex.DataSlice[str]
3636

3737

3838
@cocoindex.op.function()
39-
def extract_child_values(parent: Parent) -> list[int]:
40-
"""Extract values from each child in the Parent's children list."""
41-
return [child.value for child in parent.children]
39+
def extract_value(child: int) -> int:
40+
"""Extracts the value from a Child object."""
41+
return child
4242

4343

4444
@cocoindex.transform_flow()
4545
def for_each_transform(
4646
data: cocoindex.DataSlice[Parent],
47-
) -> cocoindex.DataSlice[list[int]]:
47+
) -> cocoindex.DataSlice[Parent]:
4848
"""Transform flow that processes child rows to extract values."""
49-
return data.transform(extract_child_values)
49+
with data["children"].row() as child:
50+
child["new_field"] = child["value"].transform(extract_value)
51+
return data
5052

5153

5254
def test_simple_transform_flow() -> None:
@@ -71,18 +73,18 @@ def test_for_each_transform_flow() -> None:
7173
"""Test the complex transform flow with child rows."""
7274
input_data = Parent(children=[Child(1), Child(2), Child(3)])
7375
result = for_each_transform.eval(input_data)
74-
expected = [1, 2, 3]
76+
expected = Parent(children=[Child(1), Child(2), Child(3)])
7577
assert result == expected, f"Expected {expected}, got {result}"
7678

7779
input_data = Parent(children=[])
7880
result = for_each_transform.eval(input_data)
79-
assert result == [], f"Expected [], got {result}"
81+
assert result == Parent(children=[]), f"Expected [], got {result}"
8082

8183

8284
@pytest.mark.asyncio
8385
async def test_for_each_transform_flow_async() -> None:
8486
"""Test the complex transform flow asynchronously."""
8587
input_data = Parent(children=[Child(4), Child(5)])
8688
result = await for_each_transform.eval_async(input_data)
87-
expected = [4, 5]
89+
expected = Parent(children=[Child(4), Child(5)])
8890
assert result == expected, f"Expected {expected}, got {result}"

0 commit comments

Comments
 (0)