diff --git a/tests/config/test_cmd_def_tree.py b/tests/config/test_cmd_def_tree.py index b1529ee8..4e523a8b 100644 --- a/tests/config/test_cmd_def_tree.py +++ b/tests/config/test_cmd_def_tree.py @@ -1,4 +1,5 @@ import os +from typing import Optional from unittest import TestCase from tmtccmd.config.tmtc import CmdTreeNode @@ -11,6 +12,13 @@ def base_tree(self): self.cmd_tree.add_child(CmdTreeNode("acs", "ACS Subsystem")) self.cmd_tree.add_child(CmdTreeNode("tcs", "TCS Subsystem")) + def tree_with_two_layers(self): + self.base_tree() + self.cmd_tree.add_child(CmdTreeNode("ping", "Ping Command")) + self.cmd_tree.children["acs"].add_child( + CmdTreeNode("acs_ctrl", "ACS Controller") + ) + def test_state(self): self.base_tree() self.cmd_tree.add_child(CmdTreeNode("ping", "Ping Command")) @@ -47,6 +55,40 @@ def test_path_contained(self): self.base_tree() self.assertTrue(self.cmd_tree.contains_path("/")) + def test_extract_node_not_contained(self): + self.tree_with_two_layers() + self.assertIsNone(self.cmd_tree.extract_subnode("aocs")) + + def test_extract_subnode_simple(self): + self.tree_with_two_layers() + acs_node = self.cmd_tree.extract_subnode("acs") + assert acs_node is not None + self.assertEqual(acs_node.name, "acs") + self.assertIsNotNone(acs_node.children["acs_ctrl"] is not None) + + def _generic_subnode_relativ_path_test(self, acs_ctrl_node: Optional[CmdTreeNode]): + assert acs_ctrl_node is not None + self.assertEqual(acs_ctrl_node.name, "acs_ctrl") + self.assertIsNotNone(acs_ctrl_node.children["update_params"] is not None) + + def test_extract_subnode_relative_path(self): + self.tree_with_two_layers() + self.cmd_tree["acs"]["acs_ctrl"].add_child( + CmdTreeNode("update_params", "Update Parameters") + ) + self._generic_subnode_relativ_path_test( + self.cmd_tree.extract_subnode("acs/acs_ctrl") + ) + + def test_extract_subnode_relativ_path_by_list(self): + self.tree_with_two_layers() + self.cmd_tree["acs"]["acs_ctrl"].add_child( + CmdTreeNode("update_params", "Update Parameters") + ) + self._generic_subnode_relativ_path_test( + self.cmd_tree.extract_subnode_by_node_list(["acs", "acs_ctrl"]) + ) + def test_path_contained_acs(self): self.base_tree() self.assertTrue(self.cmd_tree.contains_path("/acs")) @@ -96,11 +138,7 @@ def test_prinout_one_sublevel(self): ) def test_prinout_two_sublevels(self): - self.base_tree() - self.cmd_tree.add_child(CmdTreeNode("ping", "Ping Command")) - self.cmd_tree.children["acs"].add_child( - CmdTreeNode("acs_ctrl", "ACS Controller") - ) + self.tree_with_two_layers() print(self.cmd_tree) self.assertEqual( str(self.cmd_tree), @@ -114,11 +152,7 @@ def test_prinout_two_sublevels(self): ) def test_printout_two_sublevels_one_cutoff(self): - self.base_tree() - self.cmd_tree.add_child(CmdTreeNode("ping", "Ping Command")) - self.cmd_tree.children["acs"].add_child( - CmdTreeNode("acs_ctrl", "ACS Controller") - ) + self.tree_with_two_layers() printout = self.cmd_tree.str_for_tree(False, 1) print(printout) self.assertEqual( diff --git a/tmtccmd/config/tmtc.py b/tmtccmd/config/tmtc.py index 16fbe712..e8fc5dd7 100644 --- a/tmtccmd/config/tmtc.py +++ b/tmtccmd/config/tmtc.py @@ -96,6 +96,8 @@ def extract_subnode_by_node_list( ) -> Optional[CmdTreeNode]: if not self.contains_path_from_node_list(node_list): return None + if len(node_list) == 1 and self.children[node_list[0]] is not None: + return self.children[node_list[0]] return self.children[node_list[0]].extract_subnode_by_node_list(node_list[1:]) @property