|
| 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_create_node() -> None: |
| 9 | + class AWrong: |
| 10 | + pass |
| 11 | + |
| 12 | + class BWrong: |
| 13 | + pass |
| 14 | + |
| 15 | + # Create a new node |
| 16 | + @NodeFactory.register("MyNode") |
| 17 | + class MyNode(Node[AWrong, BWrong]): # type: ignore[type-var] |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + ) -> 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( |
| 28 | + self, input_channel: str, input_message: AWrong |
| 29 | + ) -> None: # type: ignore[override] |
| 30 | + # Handle the event |
| 31 | + pass |
| 32 | + |
| 33 | + @DataModelFactory.register("data_model_a") |
| 34 | + class ACorrect(DataModel): |
| 35 | + pass |
| 36 | + |
| 37 | + @DataModelFactory.register("data_model_b") |
| 38 | + class BCorrect(DataModel): |
| 39 | + pass |
| 40 | + |
| 41 | + # Create a new node |
| 42 | + @NodeFactory.register("MyNodeCorrect") |
| 43 | + class MyNodeCorect(Node[ACorrect, BCorrect]): |
| 44 | + def __init__( |
| 45 | + self, |
| 46 | + ) -> None: |
| 47 | + super().__init__( |
| 48 | + node_name="MyNode", |
| 49 | + input_channel_types=[("input", ACorrect)], |
| 50 | + output_channel_types=[("output", BCorrect)], |
| 51 | + ) |
| 52 | + |
| 53 | + async def event_handler( |
| 54 | + self, input_channel: str, input_message: ACorrect |
| 55 | + ) -> None: # type: ignore[override] |
| 56 | + # Handle the event |
| 57 | + pass |
| 58 | + |
| 59 | + # Create an instance of the node |
| 60 | + with pytest.raises(TypeError) as excinfo: |
| 61 | + _ = MyNode() |
| 62 | + |
| 63 | + assert ( |
| 64 | + "Input channel type <class 'test_node_creation.test_create_node.<locals>.AWrong'> is not a subclass of DataModel" |
| 65 | + in str(excinfo.value) |
| 66 | + ) |
| 67 | + |
| 68 | + _ = MyNodeCorect() |
0 commit comments