1
|
|
|
import graph.oval_graph |
2
|
|
|
import pytest |
3
|
|
|
import tests.any_test_help |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
def test_bad_tree(): |
7
|
|
|
|
8
|
|
|
with pytest.raises(Exception, match="don't has any child"): |
9
|
|
|
assert bad_tree() |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def test_bad_tree_only_and_no_child(): |
13
|
|
|
with pytest.raises(Exception, match="has child!"): |
14
|
|
|
assert tree_only_and() |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def test_bad_tree_only_or_no_child(): |
18
|
|
|
with pytest.raises(Exception, match="has child!"): |
19
|
|
|
assert tree_only_or() |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def test_bad_tree_with_bad_value_of_operator(): |
23
|
|
|
with pytest.raises(Exception, match="err- unknown type"): |
24
|
|
|
assert tree_with_bad_type() |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def test_bad_tree_with_bad_value_of_operator(): |
28
|
|
|
with pytest.raises(Exception, match="err- unknown operator"): |
29
|
|
|
assert tree_with_bad_value_of_operator() |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def test_bad_tree_with_bad_value_of_value(): |
33
|
|
|
with pytest.raises(Exception, match="err- unknown value"): |
34
|
|
|
assert tree_with_bad_value_of_value() |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def test_bad_tree_with_bad_value_of_negation(): |
38
|
|
|
with pytest.raises(Exception, match="negation is bool"): |
39
|
|
|
assert tree_with_bad_value_of_negation() |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
# degenered trees |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def bad_tree(): |
46
|
|
|
""" |
47
|
|
|
t |
48
|
|
|
| |
49
|
|
|
and |
50
|
|
|
| |
51
|
|
|
t |
52
|
|
|
""" |
53
|
|
|
t = graph.oval_graph.OvalNode( |
54
|
|
|
1, "value", "true", False, [ |
55
|
|
|
graph.oval_graph.OvalNode( |
56
|
|
|
2, "operator", "and", False, [ |
57
|
|
|
graph.oval_graph.OvalNode( |
58
|
|
|
3, "value", "true", False)])]) |
59
|
|
|
return |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
def tree_only_or(): |
63
|
|
|
""" |
64
|
|
|
or |
65
|
|
|
""" |
66
|
|
|
Tree = graph.oval_graph.OvalNode(1, "operator", 'or', False) |
67
|
|
|
return |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
def tree_only_and(): |
71
|
|
|
""" |
72
|
|
|
and |
73
|
|
|
""" |
74
|
|
|
Tree = graph.oval_graph.OvalNode(1, "operator", 'and', False) |
75
|
|
|
return |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
def tree_with_bad_value_of_operator(): |
79
|
|
|
Tree = graph.oval_graph.OvalNode(1, "operator", 'nad', False) |
80
|
|
|
return |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
def tree_with_bad_value_of_value(): |
84
|
|
|
Tree = graph.oval_graph.OvalNode(1, "value", 'and', False) |
85
|
|
|
return |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
def tree_with_bad_type(): |
89
|
|
|
Tree = graph.oval_graph.OvalNode(1, "auto", 'and', False) |
90
|
|
|
return |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
def tree_with_bad_value_of_negation(): |
94
|
|
|
Tree = graph.oval_graph.OvalNode(1, "operator", "true", False, [ |
95
|
|
|
graph.oval_graph.OvalNode(2, "value", 'true', "random_string")]) |
96
|
|
|
return |
97
|
|
|
|
98
|
|
|
# normal trees |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
def test_UPPERCASETree(): |
102
|
|
|
t = graph.oval_graph.OvalNode( |
103
|
|
|
1, "OPERATOR", "AND", False, [ |
104
|
|
|
graph.oval_graph.OvalNode( |
105
|
|
|
2, "VALUE", "TRUE", False), graph.oval_graph.OvalNode( |
106
|
|
|
3, "VALUE", "NOTAPPL", False)]) |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
def test_bigOvalTree(): |
110
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', False, [ |
111
|
|
|
graph.oval_graph.OvalNode(2, 'value', "false", False), |
112
|
|
|
graph.oval_graph.OvalNode(3, 'operator', "xor", False, [ |
113
|
|
|
graph.oval_graph.OvalNode(4, 'value', 'true', False), |
114
|
|
|
graph.oval_graph.OvalNode(5, 'operator', 'one', False, [ |
115
|
|
|
graph.oval_graph.OvalNode(6, 'value', 'noteval', False), |
116
|
|
|
graph.oval_graph.OvalNode(7, 'value', 'true', False), |
117
|
|
|
graph.oval_graph.OvalNode(8, 'value', 'notappl', False) |
118
|
|
|
] |
119
|
|
|
), |
120
|
|
|
graph.oval_graph.OvalNode(9, 'value', 'error', False) |
121
|
|
|
] |
122
|
|
|
), |
123
|
|
|
graph.oval_graph.OvalNode(10, 'operator', 'or', False, [ |
124
|
|
|
graph.oval_graph.OvalNode(11, 'value', "unknown", False), |
125
|
|
|
graph.oval_graph.OvalNode(12, 'value', "true", False) |
126
|
|
|
] |
127
|
|
|
) |
128
|
|
|
] |
129
|
|
|
) |
130
|
|
|
|
131
|
|
|
test_data_src = 'test_data/bigOvalTree.json' |
132
|
|
|
dict_of_tree = tests.any_test_help.any_get_test_data_json(test_data_src) |
133
|
|
|
tests.any_test_help.any_test_treeEvaluation_with_tree(Tree, "false") |
134
|
|
|
tests.any_test_help.any_test_tree_to_dict_of_tree(Tree, dict_of_tree) |
135
|
|
|
tests.any_test_help.find_any_node(Tree, 5) |
136
|
|
|
tests.any_test_help.any_test_dict_to_tree(dict_of_tree) |
137
|
|
|
|
138
|
|
|
################################################### |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
def test_treeRepr(): |
142
|
|
|
""" |
143
|
|
|
and |
144
|
|
|
| |
145
|
|
|
f |
146
|
|
|
""" |
147
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', False, [ |
148
|
|
|
graph.oval_graph.OvalNode(2, 'value', "false", False) |
149
|
|
|
] |
150
|
|
|
) |
151
|
|
|
assert str(Tree) == "and" |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
def test_add_to_tree(): |
155
|
|
|
""" |
156
|
|
|
and |
157
|
|
|
| |
158
|
|
|
f |
159
|
|
|
""" |
160
|
|
|
|
161
|
|
|
test_data_src = 'test_data/add_to_tree.json' |
162
|
|
|
dict_of_tree = tests.any_test_help.any_get_test_data_json(test_data_src) |
163
|
|
|
|
164
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', False, [ |
165
|
|
|
graph.oval_graph.OvalNode(2, 'value', "false", False) |
166
|
|
|
] |
167
|
|
|
) |
168
|
|
|
Tree1 = graph.oval_graph.OvalNode(3, 'value', "true", False) |
169
|
|
|
Tree.add_to_tree(1, Tree1) |
170
|
|
|
assert Tree.save_tree_to_dict() == dict_of_tree |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
def test_ChangeValueTree(): |
174
|
|
|
""" |
175
|
|
|
and |
176
|
|
|
/|\ |
177
|
|
|
t t or |
178
|
|
|
/ \ |
179
|
|
|
f t |
180
|
|
|
""" |
181
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', False, [ |
182
|
|
|
graph.oval_graph.OvalNode(2, 'value', "true", False), |
183
|
|
|
graph.oval_graph.OvalNode(3, 'value', "false", False), |
184
|
|
|
graph.oval_graph.OvalNode(4, 'operator', 'or', False, [ |
185
|
|
|
graph.oval_graph.OvalNode(5, 'value', "false", False), |
186
|
|
|
graph.oval_graph.OvalNode(6, 'value', "true", False) |
187
|
|
|
] |
188
|
|
|
) |
189
|
|
|
] |
190
|
|
|
) |
191
|
|
|
|
192
|
|
|
Tree.change_tree_value(3, "true") |
193
|
|
|
tests.any_test_help.any_test_treeEvaluation_with_tree(Tree, "true") |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
def test_node_operator_negate(): |
197
|
|
|
""" |
198
|
|
|
!and |
199
|
|
|
| |
200
|
|
|
f |
201
|
|
|
""" |
202
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', True, [ |
203
|
|
|
graph.oval_graph.OvalNode(2, 'value', "false", False) |
204
|
|
|
] |
205
|
|
|
) |
206
|
|
|
tests.any_test_help.any_test_treeEvaluation_with_tree(Tree, "true") |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
def test_node_operator_negate1(): |
210
|
|
|
""" |
211
|
|
|
and |
212
|
|
|
| |
213
|
|
|
t |
214
|
|
|
""" |
215
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', False, [ |
216
|
|
|
graph.oval_graph.OvalNode(2, 'value', "true", False) |
217
|
|
|
] |
218
|
|
|
) |
219
|
|
|
tests.any_test_help.any_test_treeEvaluation_with_tree(Tree, "true") |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
def test_node_operator_negate1(): |
223
|
|
|
""" |
224
|
|
|
and |
225
|
|
|
| |
226
|
|
|
t |
227
|
|
|
""" |
228
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', False, [ |
229
|
|
|
graph.oval_graph.OvalNode(2, 'value', "true", False) |
230
|
|
|
] |
231
|
|
|
) |
232
|
|
|
tests.any_test_help.any_test_treeEvaluation_with_tree(Tree, "true") |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
def test_node_value_negate(): |
236
|
|
|
""" |
237
|
|
|
and |
238
|
|
|
| |
239
|
|
|
!f |
240
|
|
|
""" |
241
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', False, [ |
242
|
|
|
graph.oval_graph.OvalNode(2, 'value', "false", True) |
243
|
|
|
] |
244
|
|
|
) |
245
|
|
|
tests.any_test_help.any_test_treeEvaluation_with_tree(Tree, "true") |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
def test_node_value_negate1(): |
249
|
|
|
""" |
250
|
|
|
and |
251
|
|
|
| |
252
|
|
|
!t |
253
|
|
|
""" |
254
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', False, [ |
255
|
|
|
graph.oval_graph.OvalNode(2, 'value', "true", True) |
256
|
|
|
] |
257
|
|
|
) |
258
|
|
|
tests.any_test_help.any_test_treeEvaluation_with_tree(Tree, "false") |
259
|
|
|
|