1
|
|
|
import graph.oval_graph |
2
|
|
|
import pytest |
3
|
|
|
import tests.any_test_help |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
|
7
|
|
|
def test_bad_tree(): |
8
|
|
|
with pytest.raises(ValueError) as e: |
9
|
|
|
badTree() |
10
|
|
|
assert str( |
11
|
|
|
e.value) == 'err- true, false, error, unknown. noteval, notappl have not child!' |
12
|
|
|
|
13
|
|
|
with pytest.raises(ValueError) as e: |
14
|
|
|
treeOnlyAnd() |
15
|
|
|
assert str(e.value) == 'err- OR, XOR, ONE, AND have child!' |
16
|
|
|
|
17
|
|
|
with pytest.raises(ValueError) as e: |
18
|
|
|
treeOnlyOr() |
19
|
|
|
assert str(e.value) == 'err- OR, XOR, ONE, AND have child!' |
20
|
|
|
|
21
|
|
|
with pytest.raises(ValueError) as e: |
22
|
|
|
treeWithBadType() |
23
|
|
|
assert str(e.value) == 'err- unknown type' |
24
|
|
|
|
25
|
|
|
with pytest.raises(ValueError) as e: |
26
|
|
|
treeWithBadValueOfOperator() |
27
|
|
|
assert str(e.value) == 'err- unknown operator' |
28
|
|
|
|
29
|
|
|
with pytest.raises(ValueError) as e: |
30
|
|
|
treeWithBadValueOfValue() |
31
|
|
|
assert str(e.value) == 'err- unknown value' |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
# degenered trees |
35
|
|
|
|
36
|
|
|
def badTree(): |
37
|
|
|
""" |
38
|
|
|
t |
39
|
|
|
| |
40
|
|
|
and |
41
|
|
|
| |
42
|
|
|
t |
43
|
|
|
""" |
44
|
|
|
t = graph.oval_graph.OvalNode( |
45
|
|
|
1, "value", "true", [ |
46
|
|
|
graph.oval_graph.OvalNode( |
47
|
|
|
2, "operator", "and", [ |
48
|
|
|
graph.oval_graph.OvalNode( |
49
|
|
|
3, "value", "true")])]) |
50
|
|
|
return |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def treeOnlyOr(): |
54
|
|
|
""" |
55
|
|
|
or |
56
|
|
|
""" |
57
|
|
|
Tree = graph.oval_graph.OvalNode(1, "operator", 'or') |
58
|
|
|
return |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def treeOnlyAnd(): |
62
|
|
|
""" |
63
|
|
|
and |
64
|
|
|
""" |
65
|
|
|
Tree = graph.oval_graph.OvalNode(1, "operator", 'and') |
66
|
|
|
return |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
def treeWithBadValueOfOperator(): |
70
|
|
|
Tree = graph.oval_graph.OvalNode(1, "operator", 'nad') |
71
|
|
|
return |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def treeWithBadValueOfValue(): |
75
|
|
|
Tree = graph.oval_graph.OvalNode(1, "value", 'and') |
76
|
|
|
return |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
def treeWithBadType(): |
80
|
|
|
Tree = graph.oval_graph.OvalNode(1, "auto", 'and') |
81
|
|
|
return |
82
|
|
|
|
83
|
|
|
# normal trees |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def test_UPPERCASETree(): |
87
|
|
|
t = graph.oval_graph.OvalNode( |
88
|
|
|
1, "OPERATOR", "AND", [ |
89
|
|
|
graph.oval_graph.OvalNode( |
90
|
|
|
2, "VALUE", "TRUE",), graph.oval_graph.OvalNode( |
91
|
|
|
3, "VALUE", "NOTAPPL")]) |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
def test_bigOvalTree(): |
95
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [ |
96
|
|
|
graph.oval_graph.OvalNode(2, 'value', "false"), |
97
|
|
|
graph.oval_graph.OvalNode(3, 'operator', "xor", [ |
98
|
|
|
graph.oval_graph.OvalNode(4, 'value', 'true'), |
99
|
|
|
graph.oval_graph.OvalNode(5, 'operator', 'one', [ |
100
|
|
|
graph.oval_graph.OvalNode(6, 'value', 'noteval'), |
101
|
|
|
graph.oval_graph.OvalNode(7, 'value', 'true'), |
102
|
|
|
graph.oval_graph.OvalNode(8, 'value', 'notappl') |
103
|
|
|
] |
104
|
|
|
), |
105
|
|
|
graph.oval_graph.OvalNode(9, 'value', 'error') |
106
|
|
|
] |
107
|
|
|
), |
108
|
|
|
graph.oval_graph.OvalNode(10, 'operator', 'or', [ |
109
|
|
|
graph.oval_graph.OvalNode(11, 'value', "unknown"), |
110
|
|
|
graph.oval_graph.OvalNode(12, 'value', "true") |
111
|
|
|
] |
112
|
|
|
) |
113
|
|
|
] |
114
|
|
|
) |
115
|
|
|
|
116
|
|
|
test_data_src='test_data/bigOvalTree.json' |
117
|
|
|
dict_of_tree = tests.any_test_help.any_get_test_data_json(test_data_src) |
118
|
|
|
any_test_treeEvaluation(Tree, "false") |
119
|
|
|
any_test_tree_to_dict_of_tree(Tree, dict_of_tree) |
120
|
|
|
find_any_node(Tree, 5) |
121
|
|
|
any_test_dict_to_tree(dict_of_tree) |
122
|
|
|
|
123
|
|
|
################################################### |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
def any_test_tree_to_dict_of_tree(tree, dict_of_tree): |
127
|
|
|
assert tree.save_tree_to_dict() == dict_of_tree |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
def find_any_node(Tree, node_id): |
131
|
|
|
findTree = Tree.find_node_with_ID(node_id) |
132
|
|
|
assert findTree.node_id == node_id |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
def any_test_treeEvaluation(tree, expect): |
136
|
|
|
assert tree.evaluate_tree() == expect |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
def any_test_dict_to_tree(dict_of_tree): |
140
|
|
|
treedict_of_tree = graph.oval_graph.restore_dict_to_tree(dict_of_tree) |
141
|
|
|
assert treedict_of_tree.save_tree_to_dict() == dict_of_tree |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
def test_treeRepr(): |
145
|
|
|
""" |
146
|
|
|
and |
147
|
|
|
| |
148
|
|
|
f |
149
|
|
|
""" |
150
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [ |
151
|
|
|
graph.oval_graph.OvalNode(2, 'value', "false") |
152
|
|
|
] |
153
|
|
|
) |
154
|
|
|
assert str(Tree) == "and" |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
def test_add_to_tree(): |
158
|
|
|
""" |
159
|
|
|
and |
160
|
|
|
| |
161
|
|
|
f |
162
|
|
|
""" |
163
|
|
|
|
164
|
|
|
test_data_src='test_data/add_to_tree.json' |
165
|
|
|
dict_of_tree = tests.any_test_help.any_get_test_data_json(test_data_src) |
166
|
|
|
|
167
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [ |
168
|
|
|
graph.oval_graph.OvalNode(2, 'value', "false") |
169
|
|
|
] |
170
|
|
|
) |
171
|
|
|
Tree1 = graph.oval_graph.OvalNode(3, 'value', "true") |
172
|
|
|
Tree.add_to_tree(1, Tree1) |
173
|
|
|
assert Tree.save_tree_to_dict() == dict_of_tree |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
def test_ChangeValueTree(): |
177
|
|
|
""" |
178
|
|
|
and |
179
|
|
|
/|\ |
180
|
|
|
t t or |
181
|
|
|
/ \ |
182
|
|
|
f t |
183
|
|
|
""" |
184
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [ |
185
|
|
|
graph.oval_graph.OvalNode(2, 'value', "true"), |
186
|
|
|
graph.oval_graph.OvalNode(3, 'value', "false"), |
187
|
|
|
graph.oval_graph.OvalNode(4, 'operator', 'or', [ |
188
|
|
|
graph.oval_graph.OvalNode(5, 'value', "false"), |
189
|
|
|
graph.oval_graph.OvalNode(6, 'value', "true") |
190
|
|
|
] |
191
|
|
|
) |
192
|
|
|
] |
193
|
|
|
) |
194
|
|
|
|
195
|
|
|
Tree.change_tree_value(3, "true") |
196
|
|
|
any_test_treeEvaluation(Tree, "true") |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
|
200
|
|
|
|
201
|
|
|
|
202
|
|
|
|
203
|
|
|
|
204
|
|
|
|
205
|
|
|
|
206
|
|
|
|
207
|
|
|
|
208
|
|
|
|
209
|
|
|
|
210
|
|
|
|