|
| 1 | +from amrita_sense.node.core import BaseNode, Node, NodeCompose, NodeComposeRendered |
| 2 | +from amrita_sense.node.wrapper import Node as NodeDecorator |
| 3 | + |
| 4 | + |
| 5 | +class TestBaseNode: |
| 6 | + """Test cases for the BaseNode class.""" |
| 7 | + |
| 8 | + def test_base_node_direct_instantiation(self): |
| 9 | + """Test that BaseNode can be instantiated directly with required parameters.""" |
| 10 | + import inspect |
| 11 | + |
| 12 | + def dummy_func(): |
| 13 | + return "dummy" |
| 14 | + |
| 15 | + frame = inspect.currentframe() |
| 16 | + node = BaseNode() |
| 17 | + node._init( |
| 18 | + func=dummy_func, |
| 19 | + tag="test_node", |
| 20 | + wrap_to_async=True, |
| 21 | + address_able=False, |
| 22 | + frame=frame, |
| 23 | + ) |
| 24 | + |
| 25 | + assert node.func == dummy_func |
| 26 | + assert node.tag == "test_node" |
| 27 | + assert node.wrap_to_async is True |
| 28 | + assert node.address_able is False |
| 29 | + |
| 30 | + |
| 31 | +class TestNode: |
| 32 | + """Test cases for the Node class.""" |
| 33 | + |
| 34 | + def test_node_creation_with_decorator(self): |
| 35 | + """Test creating a Node using the @Node decorator.""" |
| 36 | + |
| 37 | + @NodeDecorator() |
| 38 | + def simple_function(): |
| 39 | + return 42 |
| 40 | + |
| 41 | + assert isinstance(simple_function, Node) |
| 42 | + assert simple_function() == 42 |
| 43 | + |
| 44 | + def test_node_creation_with_parameters(self): |
| 45 | + """Test creating a Node with custom parameters.""" |
| 46 | + |
| 47 | + @NodeDecorator(tag="custom_tag", wrap_to_async=False, address_able=True) |
| 48 | + def parameterized_function(x: int) -> int: |
| 49 | + return x * 2 |
| 50 | + |
| 51 | + node = parameterized_function |
| 52 | + assert node.tag == "custom_tag" |
| 53 | + assert node.wrap_to_async is False |
| 54 | + assert node.address_able is True |
| 55 | + assert node(5) == 10 |
| 56 | + |
| 57 | + def test_node_with_arguments(self): |
| 58 | + """Test Node execution with arguments.""" |
| 59 | + |
| 60 | + @NodeDecorator() |
| 61 | + def function_with_args(a: int, b: str = "default") -> str: |
| 62 | + return f"{a}:{b}" |
| 63 | + |
| 64 | + result = function_with_args(42, "test") |
| 65 | + assert result == "42:test" |
| 66 | + |
| 67 | + result_default = function_with_args(42) |
| 68 | + assert result_default == "42:default" |
| 69 | + |
| 70 | + |
| 71 | +class TestNodeCompose: |
| 72 | + """Test cases for the NodeCompose class.""" |
| 73 | + |
| 74 | + def test_node_compose_creation(self): |
| 75 | + """Test creating a NodeCompose with multiple nodes.""" |
| 76 | + |
| 77 | + @NodeDecorator() |
| 78 | + def node1(): |
| 79 | + return "node1" |
| 80 | + |
| 81 | + @NodeDecorator() |
| 82 | + def node2(): |
| 83 | + return "node2" |
| 84 | + |
| 85 | + compose = NodeCompose(node1, node2) |
| 86 | + assert len(compose._graph) == 2 |
| 87 | + assert compose._graph[0] is node1 |
| 88 | + assert compose._graph[1] is node2 |
| 89 | + |
| 90 | + def test_node_compose_empty(self): |
| 91 | + """Test creating an empty NodeCompose.""" |
| 92 | + compose = NodeCompose() |
| 93 | + assert len(compose._graph) == 0 |
| 94 | + |
| 95 | + def test_node_compose_render(self): |
| 96 | + """Test that NodeCompose can be rendered.""" |
| 97 | + |
| 98 | + @NodeDecorator() |
| 99 | + def test_node(): |
| 100 | + return "test" |
| 101 | + |
| 102 | + compose = NodeCompose(test_node) |
| 103 | + rendered = compose.render() |
| 104 | + assert isinstance(rendered, NodeComposeRendered) |
| 105 | + |
| 106 | + |
| 107 | +class TestNodeComposeRendered: |
| 108 | + """Test cases for the NodeComposeRendered class.""" |
| 109 | + |
| 110 | + def test_node_compose_rendered_creation(self): |
| 111 | + """Test creating a NodeComposeRendered instance via render().""" |
| 112 | + |
| 113 | + @NodeDecorator() |
| 114 | + def test_node(): |
| 115 | + return "test" |
| 116 | + |
| 117 | + workflow = NodeCompose(test_node) |
| 118 | + rendered = workflow.render() |
| 119 | + |
| 120 | + rendered.alias2vector_map |
0 commit comments