test_oval_evaluate   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 76
dl 0
loc 96
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A test_evaluation_of_oval_tree() 0 37 2
A get_path_to_data_source() 0 14 5
A test_evaluate_oval_result() 0 12 1
1
import json
2
from pathlib import Path
3
4
import pytest
5
6
from oval_graph.oval_tree.builder import Builder
7
from oval_graph.oval_tree.oval_result import OvalResult
8
9
BAD_RESULT_COUNTS = {
10
    "number_of_true": -1,
11
    "number_of_false": -1,
12
    "number_of_error": -1,
13
    "number_of_unknown": -1,
14
    "number_of_noteval": -1,
15
    "number_of_notappl": -1
16
}
17
18
19
RESULT_COUNTS_1 = {
20
    "number_of_true": 3,
21
    "number_of_false": 3,
22
    "number_of_error": 3,
23
    "number_of_unknown": 0,
24
    "number_of_noteval": -1,
25
    "number_of_notappl": 3
26
}
27
28
29
def get_path_to_data_source(data_source):
30
    directory = ''
31
    if data_source.startswith('AND'):
32
        directory = 'and'
33
    elif data_source.startswith('OR'):
34
        directory = 'or'
35
    elif data_source.startswith('XOR'):
36
        directory = 'xor'
37
    elif data_source.startswith('ONE'):
38
        directory = 'one'
39
    else:
40
        directory = 'NONE'
41
42
    return Path(__file__).parent / 'test_data' / directory / data_source
43
44
45
@pytest.mark.parametrize("data_source, expected_result", [
46
    ('ANDTreeTrue.json', "true"),
47
    ('ANDTreeFalse.json', 'false'),
48
    ('ANDTreeError.json', "error"),
49
    ('ANDTreeUnknown.json', "unknown"),
50
    ('ANDTreeNoteval.json', "noteval"),
51
    ("ANDTreeNotappl.json", 'notappl'),
52
53
    ('ONETreeTrue.json', "true"),
54
    ('ONETreeFalse.json', 'false'),
55
    ('ONETreeFalse1.json', 'false'),
56
    ('ONETreeError.json', "error"),
57
    ('ONETreeUnknown.json', "unknown"),
58
    ('ONETreeNoteval.json', "noteval"),
59
    ("ONETreeNotappl.json", 'notappl'),
60
61
    ('ORTreeTrue.json', "true"),
62
    ('ORTreeFalse.json', 'false'),
63
    ('ORTreeError.json', "error"),
64
    ('ORTreeUnknown.json', "unknown"),
65
    ('ORTreeNoteval.json', "noteval"),
66
    ("ORTreeNotappl.json", 'notappl'),
67
68
    ('XORTreeTrue.json', "true"),
69
    ('XORTreeFalse.json', 'false'),
70
    ('XORTreeError.json', "error"),
71
    ('XORTreeUnknown.json', "unknown"),
72
    ('XORTreeNoteval.json', "noteval"),
73
    ("XORTreeNotappl.json", 'notappl'),
74
])
75
def test_evaluation_of_oval_tree(data_source, expected_result):
76
    path = get_path_to_data_source(data_source)
77
    data = dict()
78
    with open(path, "r", encoding="utf-8") as file_:
79
        data = json.load(file_)
80
    oval_tree = Builder.dict_to_oval_tree(data)
81
    assert oval_tree.evaluate_tree() == expected_result
82
83
84
@pytest.mark.parametrize("eval_function, result", [
85
    (OvalResult(**BAD_RESULT_COUNTS).eval_operator_and, None),
86
    (OvalResult(**BAD_RESULT_COUNTS).eval_operator_one, None),
87
    (OvalResult(**BAD_RESULT_COUNTS).eval_operator_or, None),
88
    (OvalResult(**BAD_RESULT_COUNTS).eval_operator_xor, None),
89
    (OvalResult(**RESULT_COUNTS_1).eval_operator_and, 'false'),
90
    (OvalResult(**RESULT_COUNTS_1).eval_operator_one, None),
91
    (OvalResult(**RESULT_COUNTS_1).eval_operator_or, 'true'),
92
    (OvalResult(**RESULT_COUNTS_1).eval_operator_xor, 'error'),
93
])
94
def test_evaluate_oval_result(eval_function, result):
95
    assert eval_function() is result
96