def testTreeInitValueError(self): """Tests TreeNode.__init__ validates input.""" with self.assertRaises(ValueError): tree.TreeNode((tree.TreeNode.LEFT, 'some-id'), 'some-data') r = tree.TreeNode((None, 'some-id'), 'some-data') with self.assertRaises(ValueError): tree.TreeNode((None, 'some-id'), 'some-data', r) with self.assertRaises(ValueError): tree.TreeNode(('foo', 'some-id'), 'some-data', r)
def testGetSingleton(self, is_iterative): root = tree.TreeNode((None, 'root-id'), 'some-data') self.assertEqual( get_path.GetPathOp()(root, { is_iterative: is_iterative, }), [(None, 'root-id')])
def testTreeNormalInit(self): """Tests TreeNode.__init__ instantiates correctly.""" n = tree.TreeNode((None, 'some-id'), 'some-data') self.assertEqual(n.parent, None) self.assertEqual(n.parent_branch, None) self.assertEqual(n.id, 'some-id') self.assertEqual(n.data, 'some-data') self.assertEqual(len(n), len('some-data')) self.assertEqual(n.metadata, {})
def testGetNestedPath(self, is_iterative): root = tree.TreeNode((None, 'root-id'), 'some-data') child = add_node.AddNodeOp()(root, { 'path': [(tree.TreeNode.LEFT, 'child-id')], 'data': 'some-data', }) self.assertEqual( get_path.GetPathOp()(child, { 'is_iterative': is_iterative, }), [(None, 'root-id'), (tree.TreeNode.LEFT, 'child-id')])
def do(self, root, args): (path, data) = (args.get('path', None), args.get('data', None)) if not len(path): raise ValueError('Invalid node path provided.') for (node_branch, node_id) in path[:-1]: root = root.children[node_branch][node_id] (node_branch, node_id) = path[-1] if node_id in root.children[node_branch]: raise ValueError('Invalid node path provided.') node = tree.TreeNode((node_branch, node_id), data, root) root.children[node_branch][node_id] = node self._update_cache(node) return node
def do_recursive(self, root, args): (path, data) = (args.get('path', None), args.get('data', None)) if not len(path): raise ValueError('Invalid node path provided.') if len(path) == 1: # end case (node_branch, node_id) = path[0] if node_id in root.children[node_branch]: raise ValueError('Invalid node path provided.') node = tree.TreeNode((node_branch, node_id), data, root) root.children[node_branch][node_id] = node self._update_cache(node) return node else: # recursive case (node_branch, node_id) = path.pop(0) return self.do_recursive(root.children[node_branch][node_id], args)
def setUp(self): self.n = tree.TreeNode((None, 'some-id'), 'some-data') self.assertEqual(len(self.n.children[tree.TreeNode.LEFT]), 0) self.assertEqual(len(self.n.children[tree.TreeNode.RIGHT]), 0)
def setUp(self): self.n = tree.TreeNode((None, 'some-id'), 'some-data')