Test Failed
Pull Request — master (#161)
by Jan
05:02 queued 52s
created

tests.test_graph.test_node_value_negate()   A

Complexity

Conditions 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nop 0
dl 0
loc 20
rs 9.8
c 0
b 0
f 0
1
import pytest
2
3
from oval_graph.oval_node import OvalNode
4
5
import tests.any_test_help
6
7
missing_error_pattern = "Missing required argument!"
8
9
bad_value_error_pattern = (
10
    r'Wrong value of (negation|node_type|argument) (argument|value)(!| for (value|operator) node!)'
11
    )
12
13
14
def test_bad_tree():
15
    with pytest.raises(Exception, match="cannot contain any child"):
16
        assert bad_tree()
17
18
19
def test_bad_tree_only_and_no_child():
20
    with pytest.raises(Exception, match="must have a child"):
21
        assert tree_only_and()
22
23
24
def test_bad_tree_only_or_no_child():
25
    with pytest.raises(Exception, match="must have a child"):
26
        assert tree_only_or()
27
28
29
def test_bad_tree_with_bad_type_of_node():
30
    with pytest.raises(Exception, match=bad_value_error_pattern):
31
        assert tree_with_bad_type()
32
33
34
def test_bad_tree_with_bad_value_of_operator():
35
    with pytest.raises(Exception, match=bad_value_error_pattern):
36
        assert tree_with_bad_value_of_operator()
37
38
39
def test_bad_tree_with_bad_value_of_value():
40
    with pytest.raises(Exception, match=bad_value_error_pattern):
41
        assert tree_with_bad_value_of_value()
42
43
44
def test_bad_tree_with_bad_value_of_negation():
45
    with pytest.raises(Exception, match=bad_value_error_pattern):
46
        assert tree_with_bad_value_of_negation()
47
48
49
def test_bad_tree_with_miss_id_argument():
50
    with pytest.raises(Exception, match=missing_error_pattern):
51
        assert miss_id_argument()
52
53
54
def test_bad_tree_with_miss_value_argument():
55
    with pytest.raises(Exception, match=missing_error_pattern):
56
        assert miss_value_argument()
57
58
59
# degenered trees
60
61
62
def miss_id_argument():
63
    tree = OvalNode(
64
        node_type='operator',
65
        value='and',
66
        children=[
67
            OvalNode(
68
                node_id=2,
69
                node_type='value',
70
                value="false",
71
            ),
72
        ]
73
    )
74
    return
75
76
77
def miss_value_argument():
78
    tree = OvalNode(
79
        node_id=1,
80
        node_type='operator',
81
        children=[
82
            OvalNode(
83
                node_id=2,
84
                node_type='value',
85
                value="false",
86
            ),
87
        ]
88
    )
89
    return
90
91
92
def bad_tree():
93
    """
94
         t
95
         |
96
        and
97
         |
98
         t
99
    """
100
    t = OvalNode(
101
        node_id=1,
102
        node_type="value",
103
        value="true",
104
        children=[
105
            OvalNode(
106
                node_id=2,
107
                node_type="operator",
108
                value="and",
109
                children=[
110
                    OvalNode(
111
                        node_id=3,
112
                        node_type="value",
113
                        value="true",
114
                    ),
115
                ]
116
            ),
117
        ]
118
    )
119
    return
120
121
122
def tree_only_or():
123
    """
124
        or
125
    """
126
    tree = OvalNode(
127
        node_id=1,
128
        node_type="operator",
129
        value='or',
130
    )
131
    return
132
133
134
def tree_only_and():
135
    """
136
        and
137
    """
138
    tree = OvalNode(
139
        node_id=1,
140
        node_type="operator",
141
        value='and',
142
    )
143
    return
144
145
146
def tree_with_bad_value_of_operator():
147
    tree = OvalNode(
148
        node_id=1,
149
        node_type="operator",
150
        value='nad',
151
    )
152
    return
153
154
155
def tree_with_bad_value_of_value():
156
    tree = OvalNode(
157
        node_id=1,
158
        node_type="value",
159
        value='and',
160
    )
161
    return
162
163
164
def tree_with_bad_type():
165
    tree = OvalNode(
166
        node_id=1,
167
        node_type="car",
168
        value='and',
169
    )
170
    return
171
172
173
def tree_with_bad_value_of_negation():
174
    tree = OvalNode(
175
        node_id=1,
176
        node_type="operator",
177
        value="true",
178
        children=[
179
            OvalNode(
180
                node_id=2,
181
                node_type="value",
182
                value='true',
183
                negation="random_string",
184
            )
185
        ]
186
    )
187
    return
188
189
# normal trees
190
191
192
def test_UPPERCASETree():
193
    t = OvalNode(
194
        node_id=1,
195
        node_type="OPERATOR",
196
        value="AND",
197
        children=[
198
            OvalNode(
199
                node_id=2,
200
                node_type="VALUE",
201
                value="TRUE",
202
            ),
203
            OvalNode(
204
                node_id=3,
205
                node_type="VALUE",
206
                value="NOTAPPL",
207
            ),
208
        ]
209
    )
210
    return
211
212
213
def test_bigOvalTree():
214
    tree = OvalNode(
215
        node_id=1,
216
        node_type='operator',
217
        value='and',
218
        children=[
219
            OvalNode(
220
                node_id=2,
221
                node_type='value',
222
                value="false",
223
            ),
224
            OvalNode(
225
                node_id=3,
226
                node_type='operator',
227
                value="xor",
228
                children=[
229
                    OvalNode(
230
                        node_id=4,
231
                        node_type='value',
232
                        value='true',
233
                    ),
234
                    OvalNode(
235
                        node_id=5,
236
                        node_type='operator',
237
                        value='one',
238
                        children=[
239
                            OvalNode(
240
                                node_id=6,
241
                                node_type='value',
242
                                value='noteval',
243
                            ),
244
                            OvalNode(
245
                                node_id=7,
246
                                node_type='value',
247
                                value='true',
248
                            ),
249
                            OvalNode(
250
                                node_id=8,
251
                                node_type='value',
252
                                value='notappl',
253
                            ),
254
                        ]
255
                    ),
256
                    OvalNode(
257
                        node_id=9,
258
                        node_type='value',
259
                        value='error',
260
                    ),
261
                ]
262
            ),
263
            OvalNode(
264
                node_id=10,
265
                node_type='operator',
266
                value='or',
267
                children=[
268
                    OvalNode(
269
                        node_id=11,
270
                        node_type='value',
271
                        value="unknown",
272
                    ),
273
                    OvalNode(
274
                        node_id=12,
275
                        node_type='value',
276
                        value="true",
277
                    ),
278
                ]
279
            ),
280
        ]
281
    )
282
283
    test_data_src = 'test_data/bigOvalTree.json'
284
    dict_of_tree = tests.any_test_help.any_get_test_data_json(test_data_src)
285
    tests.any_test_help.any_test_treeEvaluation_with_tree(tree, "false")
286
    tests.any_test_help.any_test_tree_to_dict_of_tree(tree, dict_of_tree)
287
    tests.any_test_help.find_any_node(tree, 5)
288
    tests.any_test_help.any_test_dict_to_tree(dict_of_tree)
289
290
###################################################
291
292
293
def get_and_false_tree():
294
    """
295
        and
296
         |
297
         f
298
    """
299
    return OvalNode(
300
        node_id=1,
301
        node_type='operator',
302
        value='and',
303
        children=[
304
            OvalNode(
305
                node_id=2,
306
                node_type='value',
307
                value="false",
308
            ),
309
        ]
310
    )
311
312
313
def test_treeRepr():
314
    assert str(get_and_false_tree()) == "and"
315
316
317
def test_add_to_tree():
318
    test_data_src = 'test_data/add_to_tree.json'
319
    dict_of_tree = tests.any_test_help.any_get_test_data_json(test_data_src)
320
321
    tree = get_and_false_tree()
322
    tree1 = OvalNode(
323
        node_id=3,
324
        node_type='value',
325
        value="true",
326
    )
327
    tree.add_to_tree(1, tree1)
328
    assert tree.save_tree_to_dict() == dict_of_tree
329
330
331
def test_ChangeValueTree():
332
    """
333
        and
334
        /|\
335
       t t or
336
          / \
337
         f   t
338
    """
339
    Tree = OvalNode(
340
        node_id=1,
341
        node_type='operator',
342
        value='and',
343
        children=[
344
            OvalNode(
345
                node_id=2,
346
                node_type='value',
347
                value="true",
348
            ),
349
            OvalNode(
350
                node_id=3,
351
                node_type='value',
352
                value="false",
353
            ),
354
            OvalNode(
355
                node_id=4,
356
                node_type='operator',
357
                value='or',
358
                children=[
359
                    OvalNode(
360
                        node_id=5,
361
                        node_type='value',
362
                        value="false",
363
                    ),
364
                    OvalNode(
365
                        node_id=6,
366
                        node_type='value',
367
                        value="true",
368
                    ),
369
                ]
370
            ),
371
        ]
372
    )
373
374
    Tree.change_tree_value(3, "true")
375
    tests.any_test_help.any_test_treeEvaluation_with_tree(Tree, "true")
376
377
378
def test_node_operator_negate():
379
    """
380
        !and
381
         |
382
         f
383
    """
384
    tree = OvalNode(
385
        node_id=1,
386
        node_type='operator',
387
        value='and',
388
        negation=True,
389
        children=[
390
            OvalNode(
391
                node_id=2,
392
                node_type='value',
393
                value="false",
394
            ),
395
        ]
396
    )
397
    tests.any_test_help.any_test_treeEvaluation_with_tree(tree, "true")
398
399
400
def test_node_value_negate():
401
    """
402
        and
403
         |
404
         !f
405
    """
406
    tree = OvalNode(
407
        node_id=1,
408
        node_type='operator',
409
        value='and',
410
        children=[
411
            OvalNode(
412
                node_id=2,
413
                node_type='value',
414
                value="false",
415
                negation=True,
416
            ),
417
        ]
418
    )
419
    tests.any_test_help.any_test_treeEvaluation_with_tree(tree, "true")
420
421
422
def test_node_value_negate1():
423
    """
424
        and
425
         |
426
         !t
427
    """
428
429
    tree = OvalNode(
430
        node_id=1,
431
        node_type='operator',
432
        value='and',
433
        children=[
434
            OvalNode(
435
                node_id=2,
436
                node_type='value',
437
                value="true",
438
                negation=True,
439
            ),
440
        ]
441
    )
442
    tests.any_test_help.any_test_treeEvaluation_with_tree(tree, "false")
443