Passed
Push — master ( 55ba43...476f93 )
by Jan
13:33 queued 09:06
created

test_graph.test_add_to_tree()   A

Complexity

Conditions 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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