Skip to content

Commit

Permalink
add some tests for new features
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed Nov 22, 2023
1 parent f6b5b6f commit 5fe5dc9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
54 changes: 44 additions & 10 deletions tests/config/test_cmd_def_tree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from typing import Optional
from unittest import TestCase
from tmtccmd.config.tmtc import CmdTreeNode

Expand All @@ -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"))
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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),
Expand All @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions tmtccmd/config/tmtc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5fe5dc9

Please sign in to comment.