|
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 |
|
10 | | -from pydantic_graph.beta import GraphBuilder, StepContext, TypeExpression |
| 10 | +from pydantic_graph.beta import GraphBuilder, Reducer, StepContext, TypeExpression |
11 | 11 |
|
12 | 12 | pytestmark = pytest.mark.anyio |
13 | 13 |
|
@@ -323,3 +323,162 @@ async def get_value(ctx: StepContext[DecisionState, None, object]) -> int: |
323 | 323 | result = await graph.run(state=state) |
324 | 324 | assert result == 6 |
325 | 325 | assert state.value == 6 # 1 + 2 + 3 |
| 326 | + |
| 327 | + |
| 328 | +async def test_decision_branch_last_fork_id_none(): |
| 329 | + """Test DecisionBranchBuilder.last_fork_id when there are no forks.""" |
| 330 | + from pydantic_graph.beta.decision import Decision, DecisionBranchBuilder |
| 331 | + from pydantic_graph.beta.id_types import NodeID |
| 332 | + from pydantic_graph.beta.paths import PathBuilder |
| 333 | + |
| 334 | + decision = Decision[DecisionState, None, int](id=NodeID('test'), branches=[], note=None) |
| 335 | + path_builder = PathBuilder[DecisionState, None, int](working_items=[]) |
| 336 | + branch_builder = DecisionBranchBuilder(decision=decision, source=int, matches=None, path_builder=path_builder) |
| 337 | + |
| 338 | + assert branch_builder.last_fork_id is None |
| 339 | + |
| 340 | + |
| 341 | +async def test_decision_branch_last_fork_id_with_map(): |
| 342 | + """Test DecisionBranchBuilder.last_fork_id after a map operation.""" |
| 343 | + g = GraphBuilder(state_type=DecisionState, output_type=int) |
| 344 | + |
| 345 | + @g.step |
| 346 | + async def return_list(ctx: StepContext[DecisionState, None, None]) -> list[int]: |
| 347 | + return [1, 2, 3] |
| 348 | + |
| 349 | + @g.step |
| 350 | + async def process_item(ctx: StepContext[DecisionState, None, int]) -> int: |
| 351 | + return ctx.inputs * 2 |
| 352 | + |
| 353 | + class SumReducer(Reducer[object, object, float, float]): |
| 354 | + """A reducer that sums values.""" |
| 355 | + |
| 356 | + value: float = 0.0 |
| 357 | + |
| 358 | + def reduce(self, ctx: StepContext[object, object, float]) -> None: |
| 359 | + self.value += ctx.inputs |
| 360 | + |
| 361 | + def finalize(self, ctx: StepContext[object, object, None]) -> float: |
| 362 | + return self.value |
| 363 | + |
| 364 | + sum_results = g.join(SumReducer) |
| 365 | + |
| 366 | + # Use decision with map to test last_fork_id |
| 367 | + g.add( |
| 368 | + g.edge_from(g.start_node).to(return_list), |
| 369 | + g.edge_from(return_list).to( |
| 370 | + g.decision().branch( |
| 371 | + g.match( |
| 372 | + TypeExpression[list[int]], |
| 373 | + matches=lambda x: isinstance(x, list) and all(isinstance(y, int) for y in x), |
| 374 | + ) |
| 375 | + .map() |
| 376 | + .to(process_item) |
| 377 | + ) |
| 378 | + ), |
| 379 | + g.edge_from(process_item).to(sum_results), |
| 380 | + g.edge_from(sum_results).to(g.end_node), |
| 381 | + ) |
| 382 | + |
| 383 | + graph = g.build() |
| 384 | + result = await graph.run(state=DecisionState()) |
| 385 | + assert result == 12 # (1+2+3) * 2 |
| 386 | + |
| 387 | + |
| 388 | +async def test_decision_branch_transform(): |
| 389 | + """Test DecisionBranchBuilder.transform method.""" |
| 390 | + g = GraphBuilder(state_type=DecisionState, output_type=str) |
| 391 | + |
| 392 | + @g.step |
| 393 | + async def get_value(ctx: StepContext[DecisionState, None, None]) -> int: |
| 394 | + return 10 |
| 395 | + |
| 396 | + @g.step |
| 397 | + async def format_result(ctx: StepContext[DecisionState, None, str]) -> str: |
| 398 | + return f'Result: {ctx.inputs}' |
| 399 | + |
| 400 | + async def double_value(ctx: StepContext[DecisionState, None, int], value: int) -> str: |
| 401 | + return str(value * 2) |
| 402 | + |
| 403 | + g.add( |
| 404 | + g.edge_from(g.start_node).to(get_value), |
| 405 | + g.edge_from(get_value).to(g.decision().branch(g.match(int).transform(double_value).to(format_result))), |
| 406 | + g.edge_from(format_result).to(g.end_node), |
| 407 | + ) |
| 408 | + |
| 409 | + graph = g.build() |
| 410 | + result = await graph.run(state=DecisionState()) |
| 411 | + assert result == 'Result: 20' |
| 412 | + |
| 413 | + |
| 414 | +async def test_decision_branch_label(): |
| 415 | + """Test DecisionBranchBuilder.label method.""" |
| 416 | + g = GraphBuilder(state_type=DecisionState, output_type=str) |
| 417 | + |
| 418 | + @g.step |
| 419 | + async def get_value(ctx: StepContext[DecisionState, None, None]) -> Literal['a', 'b']: |
| 420 | + return 'a' |
| 421 | + |
| 422 | + @g.step |
| 423 | + async def handle_a(ctx: StepContext[DecisionState, None, object]) -> str: |
| 424 | + return 'Got A' |
| 425 | + |
| 426 | + @g.step |
| 427 | + async def handle_b(ctx: StepContext[DecisionState, None, object]) -> str: |
| 428 | + return 'Got B' |
| 429 | + |
| 430 | + g.add( |
| 431 | + g.edge_from(g.start_node).to(get_value), |
| 432 | + g.edge_from(get_value).to( |
| 433 | + g.decision() |
| 434 | + .branch(g.match(TypeExpression[Literal['a']]).label('path A').to(handle_a)) |
| 435 | + .branch(g.match(TypeExpression[Literal['b']]).label('path B').to(handle_b)) |
| 436 | + ), |
| 437 | + g.edge_from(handle_a, handle_b).to(g.end_node), |
| 438 | + ) |
| 439 | + |
| 440 | + graph = g.build() |
| 441 | + result = await graph.run(state=DecisionState()) |
| 442 | + assert result == 'Got A' |
| 443 | + |
| 444 | + |
| 445 | +async def test_decision_branch_fork(): |
| 446 | + """Test DecisionBranchBuilder.fork method.""" |
| 447 | + g = GraphBuilder(state_type=DecisionState, output_type=str) |
| 448 | + |
| 449 | + @g.step |
| 450 | + async def choose_option(ctx: StepContext[DecisionState, None, None]) -> Literal['fork']: |
| 451 | + return 'fork' |
| 452 | + |
| 453 | + @g.step |
| 454 | + async def path_1(ctx: StepContext[DecisionState, None, object]) -> str: |
| 455 | + return 'Path 1' |
| 456 | + |
| 457 | + @g.step |
| 458 | + async def path_2(ctx: StepContext[DecisionState, None, object]) -> str: |
| 459 | + return 'Path 2' |
| 460 | + |
| 461 | + @g.step |
| 462 | + async def combine(ctx: StepContext[DecisionState, None, list[str]]) -> str: |
| 463 | + return ', '.join(ctx.inputs) |
| 464 | + |
| 465 | + g.add( |
| 466 | + g.edge_from(g.start_node).to(choose_option), |
| 467 | + g.edge_from(choose_option).to( |
| 468 | + g.decision().branch( |
| 469 | + g.match(TypeExpression[Literal['fork']]).fork( |
| 470 | + lambda b: [ |
| 471 | + b.decision.branch(g.match(TypeExpression[Literal['fork']]).to(path_1)), |
| 472 | + b.decision.branch(g.match(TypeExpression[Literal['fork']]).to(path_2)), |
| 473 | + ] |
| 474 | + ) |
| 475 | + ) |
| 476 | + ), |
| 477 | + g.edge_from(path_1, path_2).join().to(combine), |
| 478 | + g.edge_from(combine).to(g.end_node), |
| 479 | + ) |
| 480 | + |
| 481 | + graph = g.build() |
| 482 | + result = await graph.run(state=DecisionState()) |
| 483 | + assert 'Path 1' in result |
| 484 | + assert 'Path 2' in result |
0 commit comments