def test_insert_path_multiple_nodes_adds_to_children(self): root = Tree('root') root.insert_path('/root/a') root.insert_path('/root/b') self.assertEqual(['a', 'b'], [_.value for _ in root.children]) a = root.get_child('a') self.assertEqual(a.parent, root) b = root.get_child('b') self.assertEqual(b.parent, root)
def test_insert_path_adding_a_nested_absolute_path(self): root = Tree('root') root.insert_path('/root/a/b') self.assertIsNone(root.parent) self.assertEqual(['a'], [_.value for _ in root.children]) a = root.get_child('a') self.assertEqual(a.parent, root) self.assertEqual(['b'], [_.value for _ in a.children]) b = a.get_child('b') self.assertEqual(b.parent, a)
def test_insert_path_multiple_nodes_adds_to_children_for_nested_paths(self): root = Tree('root') root.insert_path('/root/child/a') root.insert_path('/root/child/b') self.assertEqual(['child'], [_.value for _ in root.children]) child = root.get_child('child') self.assertEqual(child.parent, root) a = child.get_child('a') self.assertEqual(a.parent, child) b = child.get_child('b') self.assertEqual(b.parent, child)
def test_insert_path_adding_a_absolute_path_from_a_nested_node(self): root = Tree('root') c1 = Tree('c1') root.add_child(c1) c1.insert_path('/root/a/b') root.formated_print() self.assertIsNone(root.parent) self.assertEqual(['c1', 'a'], [_.value for _ in root.children]) a = root.get_child('a') self.assertEqual(a.parent, root) self.assertEqual(['b'], [_.value for _ in a.children]) b = a.get_child('b') self.assertEqual(b.parent, a)
def test_adding_a_node_by_absolute_path(self): tree = Tree('root') tree.insert_path('/root/a') node = tree.get_child('a') self.assertIsNotNone(node) self.assertEqual(node.value, 'a')
def test_get_child_returns_none_if_query_is_not_a_child(self): root = Tree('root') root.add_child(Tree('diff')) result = root.get_child('c1') self.assertIsNone(result)
def test_get_child_returns_none_if_there_are_no_children(self): root = Tree('root') result = root.get_child('c1') self.assertIsNone(result)
def test_get_child_returns_child_node(self): root = Tree('root') c1 = Tree('c1') root.add_child(c1) result = root.get_child('c1') self.assertEqual(result, c1)