add examples

This commit is contained in:
zachary62
2024-12-27 05:29:24 +00:00
parent 993e024ec4
commit 0dde58d684
16 changed files with 1753 additions and 59 deletions
+5 -5
View File
@@ -46,7 +46,7 @@ class TestAsyncBatchFlow(unittest.TestCase):
}
}
flow = SimpleTestAsyncBatchFlow(start_node=self.process_node)
flow = SimpleTestAsyncBatchFlow(start=self.process_node)
asyncio.run(flow.run_async(shared_storage))
expected_results = {
@@ -66,7 +66,7 @@ class TestAsyncBatchFlow(unittest.TestCase):
'input_data': {}
}
flow = EmptyTestAsyncBatchFlow(start_node=self.process_node)
flow = EmptyTestAsyncBatchFlow(start=self.process_node)
asyncio.run(flow.run_async(shared_storage))
self.assertEqual(shared_storage.get('results', {}), {})
@@ -85,7 +85,7 @@ class TestAsyncBatchFlow(unittest.TestCase):
}
}
flow = ErrorTestAsyncBatchFlow(start_node=AsyncErrorNode())
flow = ErrorTestAsyncBatchFlow(start=AsyncErrorNode())
with self.assertRaises(ValueError):
asyncio.run(flow.run_async(shared_storage))
@@ -126,7 +126,7 @@ class TestAsyncBatchFlow(unittest.TestCase):
}
}
flow = NestedAsyncBatchFlow(start_node=inner_node)
flow = NestedAsyncBatchFlow(start=inner_node)
asyncio.run(flow.run_async(shared_storage))
expected_results = {
@@ -162,7 +162,7 @@ class TestAsyncBatchFlow(unittest.TestCase):
}
}
flow = CustomParamAsyncBatchFlow(start_node=CustomParamAsyncNode())
flow = CustomParamAsyncBatchFlow(start=CustomParamAsyncNode())
asyncio.run(flow.run_async(shared_storage))
expected_results = {
+9 -9
View File
@@ -86,14 +86,14 @@ class TestAsyncFlow(unittest.TestCase):
"""
# Create our nodes
start_node = AsyncNumberNode(5)
start = AsyncNumberNode(5)
inc_node = AsyncIncrementNode()
# Chain them: start_node >> inc_node
start_node - "number_set" >> inc_node
# Chain them: start >> inc_node
start - "number_set" >> inc_node
# Create an AsyncFlow with start_node
flow = AsyncFlow(start_node)
# Create an AsyncFlow with start
flow = AsyncFlow(start)
# We'll run the flow synchronously (which under the hood is asyncio.run())
shared_storage = {}
@@ -135,15 +135,15 @@ class TestAsyncFlow(unittest.TestCase):
shared_storage = {"value": 10}
start_node = BranchingAsyncNode()
start = BranchingAsyncNode()
positive_node = PositiveNode()
negative_node = NegativeNode()
# Condition-based chaining
start_node - "positive_branch" >> positive_node
start_node - "negative_branch" >> negative_node
start - "positive_branch" >> positive_node
start - "negative_branch" >> negative_node
flow = AsyncFlow(start_node)
flow = AsyncFlow(start)
asyncio.run(flow.run_async(shared_storage))
self.assertEqual(shared_storage["path"], "positive",
+6 -6
View File
@@ -40,7 +40,7 @@ class TestBatchFlow(unittest.TestCase):
}
}
flow = SimpleTestBatchFlow(start_node=self.process_node)
flow = SimpleTestBatchFlow(start=self.process_node)
flow.run(shared_storage)
expected_results = {
@@ -60,7 +60,7 @@ class TestBatchFlow(unittest.TestCase):
'input_data': {}
}
flow = EmptyTestBatchFlow(start_node=self.process_node)
flow = EmptyTestBatchFlow(start=self.process_node)
flow.run(shared_storage)
self.assertEqual(shared_storage.get('results', {}), {})
@@ -77,7 +77,7 @@ class TestBatchFlow(unittest.TestCase):
}
}
flow = SingleItemBatchFlow(start_node=self.process_node)
flow = SingleItemBatchFlow(start=self.process_node)
flow.run(shared_storage)
expected_results = {
@@ -99,7 +99,7 @@ class TestBatchFlow(unittest.TestCase):
}
}
flow = ErrorTestBatchFlow(start_node=ErrorProcessNode())
flow = ErrorTestBatchFlow(start=ErrorProcessNode())
with self.assertRaises(ValueError):
flow.run(shared_storage)
@@ -136,7 +136,7 @@ class TestBatchFlow(unittest.TestCase):
}
}
flow = NestedBatchFlow(start_node=inner_node)
flow = NestedBatchFlow(start=inner_node)
flow.run(shared_storage)
expected_results = {
@@ -170,7 +170,7 @@ class TestBatchFlow(unittest.TestCase):
}
}
flow = CustomParamBatchFlow(start_node=CustomParamNode())
flow = CustomParamBatchFlow(start=CustomParamNode())
flow.run(shared_storage)
expected_results = {
+5 -5
View File
@@ -74,7 +74,7 @@ class TestBatchNode(unittest.TestCase):
chunk_node >> reduce_node
# Create and run pipeline
pipeline = Flow(start_node=chunk_node)
pipeline = Flow(start=chunk_node)
pipeline.run(shared_storage)
self.assertEqual(shared_storage['total'], expected_sum)
@@ -95,7 +95,7 @@ class TestBatchNode(unittest.TestCase):
reduce_node = SumReduceNode()
chunk_node >> reduce_node
pipeline = Flow(start_node=chunk_node)
pipeline = Flow(start=chunk_node)
pipeline.run(shared_storage)
self.assertEqual(shared_storage['total'], expected_sum)
@@ -116,7 +116,7 @@ class TestBatchNode(unittest.TestCase):
reduce_node = SumReduceNode()
chunk_node >> reduce_node
pipeline = Flow(start_node=chunk_node)
pipeline = Flow(start=chunk_node)
pipeline.run(shared_storage)
self.assertEqual(shared_storage['total'], expected_sum)
@@ -136,7 +136,7 @@ class TestBatchNode(unittest.TestCase):
reduce_node = SumReduceNode()
chunk_node >> reduce_node
pipeline = Flow(start_node=chunk_node)
pipeline = Flow(start=chunk_node)
pipeline.run(shared_storage)
self.assertEqual(shared_storage['total'], expected_sum)
@@ -153,7 +153,7 @@ class TestBatchNode(unittest.TestCase):
reduce_node = SumReduceNode()
chunk_node >> reduce_node
pipeline = Flow(start_node=chunk_node)
pipeline = Flow(start=chunk_node)
pipeline.run(shared_storage)
self.assertEqual(shared_storage['total'], 0)
+5 -5
View File
@@ -45,7 +45,7 @@ class TestNode(unittest.TestCase):
def test_single_number(self):
shared_storage = {}
start = NumberNode(5)
pipeline = Flow(start_node=start)
pipeline = Flow(start=start)
pipeline.run(shared_storage)
self.assertEqual(shared_storage['current'], 5)
@@ -65,7 +65,7 @@ class TestNode(unittest.TestCase):
# Chain them in sequence using the >> operator
n1 >> n2 >> n3
pipeline = Flow(start_node=n1)
pipeline = Flow(start=n1)
pipeline.run(shared_storage)
self.assertEqual(shared_storage['current'], 16)
@@ -94,7 +94,7 @@ class TestNode(unittest.TestCase):
check - "positive" >> add_if_positive
check - "negative" >> add_if_negative
pipeline = Flow(start_node=start)
pipeline = Flow(start=start)
pipeline.run(shared_storage)
self.assertEqual(shared_storage['current'], 15)
@@ -118,7 +118,7 @@ class TestNode(unittest.TestCase):
check - "positive" >> add_if_positive
check - "negative" >> add_if_negative
pipeline = Flow(start_node=start)
pipeline = Flow(start=start)
pipeline.run(shared_storage)
# Should have gone down the 'negative' branch
@@ -145,7 +145,7 @@ class TestNode(unittest.TestCase):
# Attach a no-op node on the negative branch to avoid warning
check - 'negative' >> no_op
pipeline = Flow(start_node=n1)
pipeline = Flow(start=n1)
pipeline.run(shared_storage)
# final result should be -2: (10 -> 7 -> 4 -> 1 -> -2)
+11 -11
View File
@@ -35,21 +35,21 @@ class TestFlowComposition(unittest.TestCase):
def test_flow_as_node(self):
"""
1) Create a Flow (f1) starting with NumberNode(5), then AddNode(10), then MultiplyNode(2).
2) Create a second Flow (f2) whose start_node is f1.
2) Create a second Flow (f2) whose start is f1.
3) Create a wrapper Flow (f3) that contains f2 to ensure proper execution.
Expected final result in shared_storage['current']: (5 + 10) * 2 = 30.
"""
shared_storage = {}
# Inner flow f1
f1 = Flow(start_node=NumberNode(5))
f1 = Flow(start=NumberNode(5))
f1 >> AddNode(10) >> MultiplyNode(2)
# f2 starts with f1
f2 = Flow(start_node=f1)
f2 = Flow(start=f1)
# Wrapper flow f3 to ensure proper execution
f3 = Flow(start_node=f2)
f3 = Flow(start=f2)
f3.run(shared_storage)
self.assertEqual(shared_storage['current'], 30)
@@ -65,15 +65,15 @@ class TestFlowComposition(unittest.TestCase):
shared_storage = {}
# Build the inner flow
inner_flow = Flow(start_node=NumberNode(5))
inner_flow = Flow(start=NumberNode(5))
inner_flow >> AddNode(3)
# Build the middle flow, whose start_node is the inner flow
middle_flow = Flow(start_node=inner_flow)
# Build the middle flow, whose start is the inner flow
middle_flow = Flow(start=inner_flow)
middle_flow >> MultiplyNode(4)
# Wrapper flow to ensure proper execution
wrapper_flow = Flow(start_node=middle_flow)
wrapper_flow = Flow(start=middle_flow)
wrapper_flow.run(shared_storage)
self.assertEqual(shared_storage['current'], 32)
@@ -91,16 +91,16 @@ class TestFlowComposition(unittest.TestCase):
# flow1
numbernode = NumberNode(10)
numbernode >> AddNode(10)
flow1 = Flow(start_node=numbernode)
flow1 = Flow(start=numbernode)
# flow2
flow2 = Flow(start_node=MultiplyNode(2))
flow2 = Flow(start=MultiplyNode(2))
# Chain flow1 to flow2
flow1 >> flow2
# Wrapper flow to ensure proper execution
wrapper_flow = Flow(start_node=flow1)
wrapper_flow = Flow(start=flow1)
wrapper_flow.run(shared_storage)
self.assertEqual(shared_storage['current'], 40)