Skip to content

Commit 5e2036b

Browse files
committed
chore: added new tests
1 parent f272fe6 commit 5e2036b

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

tests/test_pipeline.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,68 @@ def context_modifier_b(chunk: list[int], ctx: IContextManager) -> list[int]:
500500
# Context values should reflect the actual chunk sizes processed
501501
assert context["branch_b_processed"] == 3
502502
assert context["branch_a_processed"] == 3
503+
504+
def test_branch_conditional_router_mode(self):
505+
"""Test conditional branch with first_match=True (router mode)."""
506+
# Setup
507+
data = [1, "a", 2, 99.9, 3, "b"]
508+
pipeline = Pipeline(data)
509+
510+
branches = {
511+
"integers": (
512+
createTransformer(int).map(lambda x: x + 1),
513+
lambda x: isinstance(x, int),
514+
),
515+
"strings": (
516+
createTransformer(str).map(lambda x: x.upper()),
517+
lambda x: isinstance(x, str),
518+
),
519+
"numbers": ( # This condition also matches integers
520+
createTransformer(float).map(lambda x: x * 10),
521+
lambda x: isinstance(x, (int, float)),
522+
),
523+
}
524+
525+
# Action: Execute branch with default first_match=True
526+
result, _ = pipeline.branch(branches, first_match=True)
527+
528+
# Assert: Items are routed to the *first* matching branch only.
529+
# Integers (1, 2, 3) are caught by the 'integers' branch first.
530+
assert sorted(result["integers"]) == [2, 3, 4]
531+
# Strings ('a', 'b') are caught by the 'strings' branch.
532+
assert sorted(result["strings"]) == ["A", "B"]
533+
# The float (99.9) is caught by 'numbers'. Integers are NOT processed
534+
# here because they were already matched by the 'integers' branch.
535+
assert result["numbers"] == [999.0]
536+
537+
def test_branch_conditional_broadcast_mode(self):
538+
"""Test conditional branch with first_match=False (broadcast mode)."""
539+
# Setup
540+
data = [1, "a", 2, 99.9, 3, "b"]
541+
pipeline = Pipeline(data)
542+
543+
branches = {
544+
"integers": (
545+
createTransformer(int).map(lambda x: x + 1),
546+
lambda x: isinstance(x, int),
547+
),
548+
"strings": (
549+
createTransformer(str).map(lambda x: x.upper()),
550+
lambda x: isinstance(x, str),
551+
),
552+
"numbers": ( # This condition also matches integers
553+
createTransformer(float).map(lambda x: x * 10),
554+
lambda x: isinstance(x, (int, float)),
555+
),
556+
}
557+
558+
# Action: Execute branch with first_match=False
559+
result, _ = pipeline.branch(branches, first_match=False)
560+
561+
# Assert: Items are routed to *all* matching branches.
562+
# Integers (1, 2, 3) are processed by the 'integers' branch.
563+
assert sorted(result["integers"]) == [2, 3, 4]
564+
# Strings ('a', 'b') are processed by the 'strings' branch.
565+
assert sorted(result["strings"]) == ["A", "B"]
566+
# The float (99.9) AND the integers (1, 2, 3) are processed by the 'numbers' branch.
567+
assert sorted(result["numbers"]) == [10.0, 20.0, 30.0, 999.0]

0 commit comments

Comments
 (0)