|
| 1 | +import pytest |
| 2 | +from aact.messages.base import DataModel |
| 3 | +from aact.messages.registry import DataModelFactory |
| 4 | +from aact.nodes import Node |
| 5 | +from aact.nodes import NodeFactory |
| 6 | + |
| 7 | + |
| 8 | +def test_node_with_non_datamodel_types() -> None: |
| 9 | + """Test that creating a node with non-DataModel types raises a TypeError.""" |
| 10 | + |
| 11 | + class AWrong: |
| 12 | + pass |
| 13 | + |
| 14 | + class BWrong: |
| 15 | + pass |
| 16 | + |
| 17 | + # Create a node with non-DataModel types |
| 18 | + @NodeFactory.register("MyNode") |
| 19 | + class MyNode(Node[AWrong, BWrong]): # type: ignore[type-var] |
| 20 | + def __init__(self) -> None: |
| 21 | + super().__init__( |
| 22 | + node_name="MyNode", |
| 23 | + input_channel_types=[("input", AWrong)], |
| 24 | + output_channel_types=[("output", BWrong)], |
| 25 | + ) |
| 26 | + |
| 27 | + async def event_handler( # type: ignore[override] |
| 28 | + self, input_channel: str, input_message: AWrong |
| 29 | + ) -> None: |
| 30 | + # Handle the event |
| 31 | + pass |
| 32 | + |
| 33 | + # Verify the correct error is raised |
| 34 | + with pytest.raises(TypeError) as excinfo: |
| 35 | + _ = MyNode() |
| 36 | + |
| 37 | + assert ( |
| 38 | + "Input channel type <class 'test_node_creation.test_node_with_non_datamodel_types.<locals>.AWrong'> " |
| 39 | + "is not a subclass of DataModel" in str(excinfo.value) |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def test_node_with_unregistered_datamodel_types() -> None: |
| 44 | + """Test that creating a node with unregistered DataModel types raises a TypeError.""" |
| 45 | + |
| 46 | + class ACorrect(DataModel): |
| 47 | + pass |
| 48 | + |
| 49 | + class BCorrect(DataModel): |
| 50 | + pass |
| 51 | + |
| 52 | + # Create a node with unregistered DataModel types |
| 53 | + @NodeFactory.register("MyNodeCorrect") |
| 54 | + class MyNodeCorrect(Node[ACorrect, BCorrect]): |
| 55 | + def __init__(self) -> None: |
| 56 | + super().__init__( |
| 57 | + node_name="MyNode", |
| 58 | + input_channel_types=[("input", ACorrect)], |
| 59 | + output_channel_types=[("output", BCorrect)], |
| 60 | + ) |
| 61 | + |
| 62 | + async def event_handler( # type: ignore[override] |
| 63 | + self, input_channel: str, input_message: ACorrect |
| 64 | + ) -> None: |
| 65 | + # Handle the event |
| 66 | + pass |
| 67 | + |
| 68 | + # Verify the correct error is raised |
| 69 | + with pytest.raises(TypeError) as excinfo: |
| 70 | + _ = MyNodeCorrect() |
| 71 | + |
| 72 | + assert ( |
| 73 | + "The input channel type <class 'test_node_creation.test_node_with_unregistered_datamodel_types.<locals>.ACorrect'> " |
| 74 | + "needs to be registered with `@DataModelFactory.register`" in str(excinfo.value) |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +def test_node_with_registered_datamodel_types() -> None: |
| 79 | + """Test that creating a node with properly registered DataModel types succeeds.""" |
| 80 | + |
| 81 | + @DataModelFactory.register("ACorrectNew") |
| 82 | + class ACorrectNew(DataModel): |
| 83 | + pass |
| 84 | + |
| 85 | + @DataModelFactory.register("BCorrectNew") |
| 86 | + class BCorrectNew(DataModel): |
| 87 | + pass |
| 88 | + |
| 89 | + # Create a node with registered DataModel types |
| 90 | + @NodeFactory.register("MyNodeCorrect") |
| 91 | + class MyNodeCorrect(Node[ACorrectNew, BCorrectNew]): |
| 92 | + def __init__(self) -> None: |
| 93 | + super().__init__( |
| 94 | + node_name="MyNode", |
| 95 | + input_channel_types=[("input", ACorrectNew)], |
| 96 | + output_channel_types=[("output", BCorrectNew)], |
| 97 | + ) |
| 98 | + |
| 99 | + async def event_handler( # type: ignore[override] |
| 100 | + self, input_channel: str, input_message: ACorrectNew |
| 101 | + ) -> None: |
| 102 | + # Handle the event |
| 103 | + pass |
| 104 | + |
| 105 | + # Verify that node creation succeeds |
| 106 | + node = MyNodeCorrect() |
| 107 | + assert isinstance(node, MyNodeCorrect) |
0 commit comments