2 from __future__
import absolute_import
, division
, unicode_literals
5 from unittest
.mock
import MagicMock
7 from mock
import MagicMock
9 from cola
.compat
import odict
10 from cola
.widgets
.branch
import BranchesTreeHelper
, BranchTreeWidgetItem
13 class BranchesTreeHelperTestCase(unittest
.TestCase
):
14 """Tests the BranchesTreeHelper class."""
16 def test_should_return_a_valid_dict_on_group_branches(self
):
17 """Test the group_branches function."""
18 branches
= ['top_1/child_1/child_1_1',
19 'top_1/child_1/child_1_2',
20 'top_1/child_2/child_2_1/child_2_1_1',
21 'top_1/child_2/child_2_1/child_2_1_2']
22 tree_helper
= BranchesTreeHelper()
24 result
= tree_helper
.group_branches(branches
, '/')
25 inner_child
= {'child_2_1_2': {}, 'child_2_1_1': {}}
28 'child_1': {'child_1_2': {}, 'child_1_1': {}},
29 'child_2': {'child_2_1': inner_child
}}}, result
)
31 def test_should_create_a_valid_top_item_on_create_top_level_item(self
):
32 """Test the create_top_level_item function."""
33 dict_children
= odict()
34 dict_children
['child_1'] = {}
35 dict_children
['child_2'] = odict()
36 dict_children
['child_2']['child_2_1'] = {}
37 dict_children
['child_2']['child_2_2'] = {}
38 tree_helper
= BranchesTreeHelper()
40 result
= tree_helper
.create_top_level_item('top', dict_children
)
41 self
.assertEqual('top', result
.name
)
42 self
.assertEqual(2, result
.childCount())
43 self
.assertEqual('child_1', result
.child(0).name
)
44 self
.assertEqual('child_2', result
.child(1).name
)
45 self
.assertEqual(2, result
.child(1).childCount())
46 self
.assertEqual('child_2_1', result
.child(1).child(0).name
)
47 self
.assertEqual('child_2_2', result
.child(1).child(1).name
)
49 def test_should_return_a_valid_top_item_on_get_root(self
):
50 """Test the get_root function."""
51 items
= self
._create
_top
_item
()
52 tree_helper
= BranchesTreeHelper()
54 result
= tree_helper
.get_root(items
['child_1'])
55 self
.assertEqual(items
['top'], result
)
57 result
= tree_helper
.get_root(items
['sub_child_2_1'])
58 self
.assertEqual(items
['top'], result
)
60 def test_should_return_a_valid_branch_name_on_get_full_name(self
):
61 """Test the get_full_name function."""
62 items
= self
._create
_top
_item
()
63 tree_helper
= BranchesTreeHelper()
65 result
= tree_helper
.get_full_name(items
['child_1'], '/')
66 self
.assertEqual('child_1', result
)
68 result
= tree_helper
.get_full_name(items
['sub_child_2_2'], '/')
69 self
.assertEqual('child_2/sub_child_2_2', result
)
71 def test_should_return_a_valid_child_on_find_child(self
):
72 """Test the find_child function."""
73 items
= self
._create
_top
_item
()
74 tree_helper
= BranchesTreeHelper()
76 child
= tree_helper
.find_child(items
['top'], 'child_1')
77 self
.assertEqual('child_1', child
.name
)
79 child
= tree_helper
.find_child(items
['top'], 'child_2/sub_child_2_2')
80 self
.assertEqual('sub_child_2_2', child
.name
)
82 def test_should_return_empty_state_on_save_state(self
):
83 """Test the save_state function."""
84 top
= self
._create
_item
('top', False)
85 tree_helper
= BranchesTreeHelper()
87 result
= tree_helper
.save_state(top
)
88 self
.assertEqual({'top': {}}, result
)
90 def test_should_return_a_valid_state_on_save_state(self
):
91 """Test the save_state function."""
92 items
= self
._create
_top
_item
()
93 tree_helper
= BranchesTreeHelper()
95 result
= tree_helper
.save_state(items
['top'])
96 self
.assertEqual({'top': {'child_1': {}, 'child_2': {
97 'sub_child_2_1': {}, 'sub_child_2_2': {}}}}, result
)
99 def _create_item(self
, name
, expanded
):
100 item
= BranchTreeWidgetItem(name
)
101 item
.isExpanded
= MagicMock(return_value
=expanded
)
105 def _create_top_item(self
):
106 top
= self
._create
_item
('top', True)
107 child_1
= self
._create
_item
('child_1', False)
108 child_2
= self
._create
_item
('child_2', True)
109 sub_child_2_1
= self
._create
_item
('sub_child_2_1', False)
110 sub_child_2_2
= self
._create
_item
('sub_child_2_2', False)
112 child_2
.addChildren([sub_child_2_1
, sub_child_2_2
])
113 top
.addChildren([child_1
, child_2
])
115 return {'top': top
, 'child_1': child_1
, 'sub_child_2_1': sub_child_2_1
,
116 'sub_child_2_2': sub_child_2_2
}
119 if __name__
== '__main__':