File: branch_test.py

package info (click to toggle)
git-cola 4.13.0-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 6,480 kB
  • sloc: python: 36,938; sh: 304; makefile: 223; xml: 100; tcl: 62
file content (316 lines) | stat: -rw-r--r-- 8,104 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""Tests related to the branches widget"""

from cola.widgets import branch

from .helper import Mock


def test_create_tree_entries():
    names = [
        'abc',
        'cat/abc',
        'cat/def',
        'xyz/xyz',
    ]
    root = branch.create_tree_entries(names, '')
    expect = 3
    actual = len(root.children)
    assert expect == actual

    # 'abc'
    abc = root.children[0]
    expect = 'abc'
    actual = abc.basename
    assert expect == actual
    expect = 'abc'
    actual = abc.refname
    assert expect == actual
    expect = []
    actual = abc.children
    assert expect == actual

    # 'cat'
    cat = root.children[1]
    expect = 'cat'
    actual = 'cat'
    assert expect == actual
    assert cat.refname is None
    expect = 2
    actual = len(cat.children)
    assert expect == actual

    # 'cat/abc'
    cat_abc = cat.children[0]
    expect = 'abc'
    actual = cat_abc.basename
    assert expect == actual
    expect = 'cat/abc'
    actual = cat_abc.refname
    assert expect == actual
    expect = []
    actual = cat_abc.children
    assert expect == actual

    # 'cat/def'
    cat_def = cat.children[1]
    expect = 'def'
    actual = cat_def.basename
    assert expect == actual
    expect = 'cat/def'
    actual = cat_def.refname
    assert expect == actual
    expect = []
    actual = cat_def.children
    assert expect == actual

    # 'xyz'
    xyz = root.children[2]
    expect = 'xyz'
    actual = xyz.basename
    assert expect == actual
    assert xyz.refname is None
    expect = 1
    actual = len(xyz.children)
    assert expect == actual

    # 'xyz/xyz'
    xyz_xyz = xyz.children[0]
    expect = 'xyz'
    actual = xyz_xyz.basename
    assert expect == actual

    expect = 'xyz/xyz'
    actual = xyz_xyz.refname
    assert expect == actual

    expect = []
    actual = xyz_xyz.children
    assert expect == actual


def test_create_tree_entries_with_filter():
    """Test the filtering of branches by name"""
    names = [
        'qqq/ttt',
        'abc',
        'cat/abc',
        'cat/def',
        'xyz/xyz',
    ]
    root = branch.create_tree_entries(names, 'abc')
    expect = 2  # abc and cat/abc
    actual = len(root.children)
    assert expect == actual

    # 'cat'
    cat = root.children[1]
    expect = 'cat'
    actual = 'cat'
    assert expect == actual
    assert cat.refname is None
    expect = 1  # cat/abc
    actual = len(cat.children)
    assert expect == actual
    # 'cat/abc'
    cat_abc = cat.children[0]
    expect = 'abc'
    actual = cat_abc.basename
    assert expect == actual
    expect = 'cat/abc'
    actual = cat_abc.refname
    assert expect == actual

    # 'cat/def' is the only match.
    root = branch.create_tree_entries(names, 'def')
    expect = 1
    actual = len(root.children)
    assert expect == actual
    # 'cat' is the parent
    cat = root.children[0]
    assert cat.refname is None
    expect = 'cat'
    actual = cat.basename
    assert expect == actual
    # 'cat/def' is the only child.
    expect = 1
    actual = len(cat.children)
    assert expect == actual
    cat_def = cat.children[0]
    expect = 'cat/def'
    actual = cat_def.refname
    assert expect == actual
    expect = 'def'
    actual = cat_def.basename
    assert expect == actual


def test_create_name_dict():
    """Test transforming unix path-like names into a nested dict"""
    branches = [
        'top_1/child_1/child_1_1',
        'top_1/child_1/child_1_2',
        'top_1/child_2/child_2_1/child_2_1_1',
        'top_1/child_2/child_2_1/child_2_1_2',
    ]
    inner_child = {'child_2_1_2': {}, 'child_2_1_1': {}}
    expect = {
        'top_1': {
            'child_1': {'child_1_2': {}, 'child_1_1': {}},
            'child_2': {'child_2_1': inner_child},
        }
    }
    actual = branch.create_name_dict(branches)
    assert expect == actual


def test_create_toplevel_item():
    names = [
        'child_1',
        'child_2/child_2_1',
        'child_2/child_2_2',
    ]
    tree = branch.create_tree_entries(names, '')
    tree.basename = 'top'
    top = branch.create_toplevel_item(tree)

    expect = 'top'
    actual = top.name
    assert expect == actual

    expect = 2
    actual = top.childCount()
    assert expect == actual

    expect = 'child_1'
    actual = top.child(0).name
    assert expect == actual

    expect = 'child_1'
    actual = top.child(0).refname
    assert expect == actual

    expect = 'child_2'
    actual = top.child(1).name
    assert expect == actual

    assert top.child(1).refname is None

    expect = 2
    actual = top.child(1).childCount()
    assert expect == actual

    expect = 'child_2_1'
    actual = top.child(1).child(0).name
    assert expect == actual

    expect = 'child_2_2'
    actual = top.child(1).child(1).name
    assert expect == actual

    expect = 'child_2/child_2_1'
    actual = top.child(1).child(0).refname
    assert expect == actual

    expect = 'child_2/child_2_2'
    actual = top.child(1).child(1).refname
    assert expect == actual


def test_get_toplevel_item():
    items = _create_top_item()
    actual = branch.get_toplevel_item(items['child_1'])
    assert items['top'] is actual

    actual = branch.get_toplevel_item(items['sub_child_2_1'])
    assert items['top'] is actual


def test_refname_attribute():
    items = _create_top_item()

    actual = items['child_1'].refname
    expect = 'child_1'
    assert expect == actual

    actual = items['sub_child_2_2'].refname
    expect = 'child_2/sub_child_2_2'
    assert expect == actual


def test_should_return_a_valid_child_on_find_child():
    """Test the find_child function."""
    items = _create_top_item()
    child = branch.find_by_refname(items['top'], 'child_1')
    assert child.refname == 'child_1'

    child = branch.find_by_refname(items['top'], 'child_2/sub_child_2_2')
    assert child.name == 'sub_child_2_2'


def test_should_return_empty_state_on_save_state():
    """Test the save_state function."""
    top = _create_item('top', None, False)
    tree_helper = branch.BranchesTreeHelper(Mock())
    actual = tree_helper.save_state(top)
    assert {'top': {'children': {}, 'expanded': False, 'selected': False}} == actual


def test_should_return_a_valid_state_on_save_state():
    """Test the save_state function."""
    items = _create_top_item()
    tree_helper = branch.BranchesTreeHelper(Mock())
    actual = tree_helper.save_state(items['top'])
    expect = {
        'top': {
            'children': {
                'child_1': {
                    'children': {},
                    'expanded': False,
                    'selected': False,
                },
                'child_2': {
                    'children': {
                        'sub_child_2_1': {
                            'children': {},
                            'expanded': False,
                            'selected': False,
                        },
                        'sub_child_2_2': {
                            'children': {},
                            'expanded': False,
                            'selected': False,
                        },
                    },
                    'expanded': True,
                    'selected': False,
                },
            },
            'expanded': True,
            'selected': False,
        }
    }
    assert expect == actual


def _create_top_item():
    top = _create_item('top', None, True)
    child_1 = _create_item('child_1', 'child_1', False)
    child_2 = _create_item('child_2', None, True)
    sub_child_2_1 = _create_item('sub_child_2_1', 'child_2/sub_child_2_1', False)
    sub_child_2_2 = _create_item('sub_child_2_2', 'child_2/sub_child_2_2', False)

    child_2.addChildren([sub_child_2_1, sub_child_2_2])
    top.addChildren([child_1, child_2])

    return {
        'top': top,
        'child_1': child_1,
        'sub_child_2_1': sub_child_2_1,
        'sub_child_2_2': sub_child_2_2,
    }


def _create_item(name, refname, expanded):
    item = branch.BranchTreeWidgetItem(name, refname=refname)
    item.isExpanded = Mock(return_value=expanded)
    return item