test_hash()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
1
# Copyright (c) 2016 Fabian Kochem
2
3
4
from libtree import core, Node
5
from mock import patch
6
7
8
def test_basic_representation(trans, root):
9
    node = Node(trans, root.id)
10
    assert repr(node) == '<Node id=\'{}\'>'.format(root.id, root.position)
11
12
13
def test_title_representation(trans, nd1):
14
    node = Node(trans, nd1.id)
15
    expected = "<Node id=\'{}\', title='Node 1'>".format(nd1.id)
16
    assert repr(node) == expected
17
18
19
def test_it_compares_other_nodes(trans, nd1, nd2):
20
    node1a = Node(trans, nd1.id)
21
    node1b = Node(trans, nd1.id)
22
    node2 = Node(trans, nd2.id)
23
    assert node1a == node1b
24
    assert node1a != node2
25
26
27
def test_it_wont_compare_other_types(trans, nd1, nd2):
28
    node = Node(trans, nd1.id)
29
    assert node != nd1
30
31
32
def test_hash(trans, nd1, nd2):
33
    node1a = Node(trans, nd1.id)
34
    node1b = Node(trans, nd1.id)
35
    node2 = Node(trans, nd2.id)
36
    assert hash(node1a) == hash(node1b)
37
    assert hash(node1a) != hash(node2)
38
39
40
def test_get_parent(trans, nd2_1_1, nd2_leaf):
41
    node = Node(trans, nd2_leaf.id)
42
    parent = node.parent
43
    assert parent.id == nd2_1_1.id
44
45
46
def test_get_parent_returns_none_for_root(trans, root):
47
    assert Node(trans, root.id).parent is None
48
49
50
def test_get_position(trans, nd3):
51
    node = Node(trans, nd3.id)
52
    assert node.position == nd3.position
53
54
55
def test_get_properties(trans, nd3):
56
    node = Node(trans, nd3.id)
57
    assert node.properties == nd3.properties
58
59
60
def test_get_children_count(trans, cur, root):
61
    node = Node(trans, root)
62
    assert len(node) == core.get_children_count(cur, root.id)
63
64
65
def test_get_children(trans, nd2, nd2_1):
66
    node = Node(trans, nd2.id)
67
    children = [Node(trans, nd2_1.id)]
68
    assert node.children == children
69
70
71
def test_has_children(trans, nd2, nd2_leaf):
72
    node = Node(trans, nd2.id)
73
    node2_leaf = Node(trans, nd2_leaf.id)
74
    assert node.has_children
75
    assert not node2_leaf.has_children
76
77
78
def test_get_ancestors_bottom_up(trans, root, nd2, nd2_1):
79
    node = Node(trans, nd2_1.id)
80
    ancestors = [Node(trans, nd2.id), Node(trans, root.id)]
81
    assert node.ancestors == ancestors
82
83
84
def test_get_descendants(trans, nd2_1, nd2_1_1, nd2_leaf):
85
    node = Node(trans, nd2_1.id)
86
    descendants = {Node(trans, nd2_1_1.id), Node(trans, nd2_leaf.id)}
87
    assert node.descendants == descendants
88
89
90
@patch.object(core, 'insert_node')
91
def test_insert_child(mock, trans, cur):
92
    node = Node(trans, 1)
93
    properties = {'type': 'new_child'}
94
    node.insert_child(properties)
95
    mock.assert_called_with(cur, node.id, properties, position=-1,
96
                            auto_position=True, id=None)
97
98
99
@patch.object(core, 'delete_node')
100
def test_delete(mock, trans, cur):
101
    node = Node(trans, 1)
102
    node.delete()
103
    mock.assert_called_with(cur, node.id)
104
105
106
@patch.object(core, 'change_parent')
107
def test_move(mock, trans, cur):
108
    node = Node(trans, 1)
109
    target_node = Node(trans, 2)
110
    node.move(target_node)
111
    mock.assert_called_with(cur, node.id, target_node.id, position=-1,
112
                            auto_position=True)
113
114
115
@patch.object(core, 'swap_node_positions')
116
def test_swap_position(mock, trans, cur):
117
    node1 = Node(trans, 1)
118
    node2 = Node(trans, 2)
119
    node1.swap_position(node2)
120
    mock.assert_called_with(cur, node1.id, node2.id)
121
122
123
@patch.object(core, 'get_inherited_properties')
124
def test_get_inherited_properties(mock, trans, cur):
125
    node = Node(trans, 1)
126
    node.inherited_properties
127
    mock.assert_called_with(cur, node.id)
128
129
130
@patch.object(core, 'get_recursive_properties')
131
def test_get_recursive_properties(mock, trans, cur):
132
    node = Node(trans, 1)
133
    node.recursive_properties
134
    mock.assert_called_with(cur, node.id)
135
136
137
@patch.object(core, 'set_properties')
138
def test_set_properties(mock, trans, cur):
139
    node = Node(trans, 1)
140
    node.set_properties({'foo': 'bar'})
141
    mock.assert_called_with(cur, node.id, {'foo': 'bar'})
142
143
144
@patch.object(core, 'update_properties')
145
def test_update_properties(mock, trans, cur):
146
    node = Node(trans, 1)
147
    node.update_properties({'foo': 'bar'})
148
    mock.assert_called_with(cur, node.id, {'foo': 'bar'})
149
150
151
@patch.object(core, 'set_position')
152
def test_set_position(mock, trans, cur):
153
    node = Node(trans, 1)
154
    node.set_position(1337)
155
    mock.assert_called_with(cur, node.id, 1337, auto_position=True)
156
157
158
@patch.object(core, 'get_node_at_position')
159
def test_get_child_at_position(mock, trans, cur):
160
    node = Node(trans, 1)
161
    node.get_child_at_position(2)
162
    mock.assert_called_with(cur, 1, 2)
163