Example #1
0
 def test_iterating_over_node_yields_children(self):
     root = Tree('root')
     c1 = Tree('c1')
     root.add_child(c1)
     result = [x for x in root]
     self.assertEqual(1, len(result))
     self.assertEqual(result, [c1])
Example #2
0
 def test_origin_from_path_returns_relative_to_root_if_path_starts_with_slash(self):
     root = Tree('root')
     child = Tree('child')
     root.add_child(child)
     node, parts = child._origin_from_path('/root/child')
     self.assertEqual(root, node)
     self.assertEqual(['child'], parts)
Example #3
0
 def test_display_tree(self):
     root = Tree(root=True)
     root.insert_path('/a')
     expected = textwrap.dedent('''
     /
         a
     ''').lstrip()
     self.assertEqual(root.display(), expected)
Example #4
0
 def test_find_node_with_relative_path_returns_None_if_node_does_not_exist(self):
     root = Tree('root')
     child = Tree('a')
     root.add_child(child)
     node = root.find_path('b')
     self.assertIsNone(node)
     node = root.find_path('/b')
     self.assertIsNone(node)
Example #5
0
 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)
Example #6
0
 def test_search_finds_only_nodes_with_exact_match_if_exact_param_set_to_true(self):
     root = Tree('root')
     a1 = Tree('abc')
     root.add_child(a1)
     b = Tree('b')
     a1.add_child(b)
     a2 = Tree('xyzabc')
     b.add_child(a2)
     results = root.search('abc', exact=True)
     self.assertEqual(results, [a1])
Example #7
0
 def test_search_returns_multiple_nodes_even_if_at_different_parts_of_the_tree(self):
     root = Tree('root')
     a1 = Tree('a')
     root.add_child(a1)
     b = Tree('b')
     a1.add_child(b)
     a2 = Tree('a')
     b.add_child(a2)
     results = root.search('a')
     self.assertEqual(results, [a1, a2])
Example #8
0
 def test_search_finds_only_nodes_relative_to_current_node_if_relative_set_to_true(self):
     root = Tree('root')
     a1 = Tree('abc')
     root.add_child(a1)
     b = Tree('b')
     a1.add_child(b)
     a2 = Tree('xyzabc')
     b.add_child(a2)
     results = b.search('abc', relative=True)
     self.assertEqual(results, [a2])
Example #9
0
 def test_search_finds_all_nodes_whose_value_contains_seach_string(self):
     root = Tree('root')
     a1 = Tree('abc')
     root.add_child(a1)
     b = Tree('b')
     a1.add_child(b)
     a2 = Tree('xyzabc')
     b.add_child(a2)
     results = root.search('abc')
     self.assertEqual(results, [a1, a2])
Example #10
0
 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)
Example #11
0
 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)
Example #12
0
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
Example #13
0
 def test_find_with_just_a_slash_returns_root_node_from_a_nested_path(self):
     root = Tree('root')
     a = Tree('a')
     root.add_child(a)
     b = Tree('b')
     a.add_child(b)
     node = b.find_path('/')
     self.assertEqual(node, root)
Example #14
0
 def test_get_path_string_on_nested_path(self):
     root = Tree('root')
     a = Tree('a')
     root.add_child(a)
     b = Tree('b')
     a.add_child(b)
     path = b.get_path()
     self.assertEqual(path, '/root/a/b')
Example #15
0
 def test_get_ancestors_on_nested_path(self):
     root = Tree('root')
     a = Tree('a')
     root.add_child(a)
     b = Tree('b')
     a.add_child(b)
     ancestors = list(b.get_ancestors())
     self.assertEqual(ancestors, [a, root])
Example #16
0
 def test_find_with_absolute_path_with_root_node_is_defaulted_slash(self):
     root = Tree(root=True)
     a = Tree('a')
     root.add_child(a)
     b = Tree('b')
     a.add_child(b)
     node = b.find_path('/a/b')
     self.assertEqual(node, b)
Example #17
0
 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)
Example #18
0
 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')
Example #19
0
 def test_find_nested_node_with_relative_path(self):
     root = Tree('root')
     a = Tree('a')
     root.add_child(a)
     b = Tree('b')
     a.add_child(b)
     node = root.find_path('a/b')
     self.assertEqual(node, b)
Example #20
0
 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
Example #21
0
 def test_origin_from_path_returns_relative_to_current_node_if_path_does_not_start_with_slash(self):
     root = Tree('root')
     child1 = Tree('child1')
     child2 = Tree('child2')
     root.add_child(child1)
     child1.add_child(child2)
     node, parts = child1._origin_from_path('child2')
     self.assertEqual(child1, node)
     self.assertEqual(['child2'], parts)
Example #22
0
 def test_search_returns_multiple_nodes_if_match_search_target(self):
     root = Tree('root')
     a1 = Tree('a')
     root.add_child(a1)
     a2 = Tree('a')
     root.add_child(a2)
     results = root.search('a')
     self.assertEqual(results, [a1, a2])
Example #23
0
 def test_get_child_returns_none_if_there_are_no_children(self):
     root = Tree('root')
     result = root.get_child('c1')
     self.assertIsNone(result)
Example #24
0
 def _create_node(self, val, parent, meta):
     node = Tree(val)
     if parent:
         parent.add_child(node)
     node.meta = meta
     return node
Example #25
0
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())
Example #26
0
 def test_adding_child_sets_parent(self):
     root = Tree('root')
     c1 = Tree('c1')
     root.add_child(c1)
     self.assertEqual(c1.parent, root)
Example #27
0
 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)
Example #28
0
 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)
Example #29
0
 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')
Example #30
0
 def test_in_operator_works_for_checking_if_node_is_in_children(self):
     root = Tree('root')
     c1 = Tree('c1')
     root.add_child(c1)
     self.assertTrue('c1' in root.children)