def test_display_tree(self): root = Tree(root=True) root.insert_path('/a') expected = textwrap.dedent(''' / a ''').lstrip() self.assertEqual(root.display(), expected)
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 init_tree_from_file(input_file): with open(input_file, 'r') as f: tree = PathTree(root=True) for line in f: tree.insert_path(line.strip()) node = tree.find_path(line.strip()) node.meta = { 'type': 'file', 'id': '', 'modified': '', 'size': '', 'details': 'added from file' } return tree
def get_tree(self): tree = PathTree(root=True) for response in self.contents: for entry in response.entries: tree.insert_path(entry.path_display) node = tree.find_path(entry.path_display) if isinstance(entry, dropbox.files.FileMetadata): node.meta = { 'type': 'file', 'id': entry.id, 'modified': entry.server_modified, 'size': entry.size } if isinstance(entry, dropbox.files.FolderMetadata): node.meta = { 'type': 'folder', 'id': entry.id } return tree
class TreeDisplayTests(TestCase): def setUp(self): self.tree = Tree(root=True) self.tree.insert_path('/a/b/c/d') self.tree.insert_path('/a/b/f') self.tree.insert_path('/a/b/f/g') self.tree.insert_path('/a/b/f/i') self.tree.insert_path('/a/b/f/j') self.tree.insert_path('/a/h') def test_display_tree(self): root = Tree(root=True) root.insert_path('/a') expected = textwrap.dedent(''' / a ''').lstrip() self.assertEqual(root.display(), expected) def test_formatted_print_ascii(self): expected = textwrap.dedent(''' / .- a |- b | |- c | | .- d | .- f | |- g | |- i | .- j .- h ''').lstrip() out = StringIO() with redirect_stdout(out): self.tree.formated_print(line_type='ascii') self.assertEqual(expected, out.getvalue()) def test_formatted_print_ascii_ex(self): expected = textwrap.dedent(''' / └─ a ├─ b │ ├─ c │ │ └─ d │ └─ f │ ├─ g │ ├─ i │ └─ j └─ h ''').lstrip() out = StringIO() with redirect_stdout(out): self.tree.formated_print(line_type='ascii-ex') self.assertEqual(expected, out.getvalue()) def test_formatted_print_ascii_exr(self): expected = textwrap.dedent(''' / ╰─ a ├─ b │ ├─ c │ │ ╰─ d │ ╰─ f │ ├─ g │ ├─ i │ ╰─ j ╰─ h ''').lstrip() out = StringIO() with redirect_stdout(out): self.tree.formated_print(line_type='ascii-exr') self.assertEqual(expected, out.getvalue()) def test_formatted_print_ascii_em(self): expected = textwrap.dedent(''' / ╚═ a ╠═ b ║ ╠═ c ║ ║ ╚═ d ║ ╚═ f ║ ╠═ g ║ ╠═ i ║ ╚═ j ╚═ h ''').lstrip() out = StringIO() with redirect_stdout(out): self.tree.formated_print(line_type='ascii-em') self.assertEqual(expected, out.getvalue()) def test_formatted_print_ascii_emv(self): expected = textwrap.dedent(''' / ╙─ a ╟─ b ║ ╟─ c ║ ║ ╙─ d ║ ╙─ f ║ ╟─ g ║ ╟─ i ║ ╙─ j ╙─ h ''').lstrip() out = StringIO() with redirect_stdout(out): self.tree.formated_print(line_type='ascii-emv') self.assertEqual(expected, out.getvalue()) def test_formatted_print_ascii_emh(self): expected = textwrap.dedent(''' / ╘═ a ╞═ b │ ╞═ c │ │ ╘═ d │ ╘═ f │ ╞═ g │ ╞═ i │ ╘═ j ╘═ h ''').lstrip() out = StringIO() with redirect_stdout(out): self.tree.formated_print(line_type='ascii-emh') self.assertEqual(expected, out.getvalue())
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_adding_a_node_more_than_once_only_creates_one_instance(self): root = Tree('root') root.insert_path('a') root.insert_path('a') self.assertEqual(1, len(root.children)) self.assertEqual(['a'], [_.value for _ in root.children])