Passed
Push — master ( 5b3fdd...dd1c2d )
by Guibert
02:21
created

tests.test_parser.test_compile_expression_has()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
import pytest
2
3
from networkx_query import ParserException
4
from networkx_query.parser import compile_ast, explain, parse
5
6
node_0 = {'application': 'test'}
7
node_1 = {'application': 'test', 'weight': 3, 'group': 'my-group'}
8
node_2 = {'_link': {'provider': "aws", 'resource_type': "test", 'other': {'weight': 2}}}
9
10
11
def test_explain():
12
    result = explain(parse({'not': {'has': ['group']}, 'has': 'application', 'eq': [('_link', 'other', 'weight'), 2]}))
13
    assert result == {
14
        'and': [{'not': [{'has': ['group']}]}, {'has': ['application']}, {'eq': [('_link', 'other', 'weight'), 2]}]
15
    }
16
17
18
def test_compile_expression_has():
19
    func = compile_ast(parse({'has': 'application'}))
20
    assert func(node_0)
21
    assert func(node_1)
22
23
24
def test_compile_expression_not_has():
25
    func = compile_ast(parse({'not': {'has': 'group'}}))
26
    assert func(node_0)
27
    assert not func(node_1)
28
29
30
def test_compile_expression_contains():
31
    func = compile_ast(parse({'contains': ['group', 'my']}))
32
    assert not func(node_0)
33
    assert func(node_1)
34
35
36
def test_compile_expression_has_dict():
37
    func = compile_ast(parse({'has': '_link'}))
38
    assert not func(node_0)
39
    assert func(node_2)
40
41
42
def test_compile_expression_has_path():
43
    func = compile_ast(parse({'has': [('_link', 'other', 'weight')]}))
44
    assert not func(node_0)
45
    assert func(node_2)
46
47
48
def test_compile_expression_eq_path():
49
    func = compile_ast(parse({'eq': [('_link', 'other', 'weight'), 2]}))
50
    assert func(node_2)
51
52
53
def test_compile_expression_neq_path():
54
    func = compile_ast(parse({'neq': [('_link', 'other', 'weight'), 2]}))
55
    assert not func(node_2)
56
    func = compile_ast(parse({'neq': [('_link', 'other', 'weight'), 8]}))
57
    assert func(node_2)
58
59
60
def test_compile_expression_gt_path():
61
    func = compile_ast(parse({'gt': [('_link', 'other', 'weight'), 1]}))
62
    assert func(node_2)
63
    func = compile_ast(parse({'gt': [('_link', 'other', 'weight'), 2]}))
64
    assert not func(node_2)
65
66
67
def test_compile_expression_gte_path():
68
    func = compile_ast(parse({'gte': [('_link', 'other', 'weight'), 2]}))
69
    assert func(node_2)
70
    func = compile_ast(parse({'gte': [('_link', 'other', 'weight'), 1]}))
71
    assert func(node_2)
72
73
74
def test_compile_expression_lte_path():
75
    func = compile_ast(parse({'lte': [('_link', 'other', 'weight'), 2]}))
76
    assert func(node_2)
77
    func = compile_ast(parse({'lte': [('_link', 'other', 'weight'), 3]}))
78
    assert func(node_2)
79
80
81
def test_compile_expression_lt_path():
82
    func = compile_ast(parse({'lt': [('_link', 'other', 'weight'), 3]}))
83
    assert func(node_2)
84
    func = compile_ast(parse({'lt': [('_link', 'other', 'weight'), 2]}))
85
    assert not func(node_2)
86
87
88
def test_compile_expression_in_path():
89
    func = compile_ast(parse({'in': [('_link', 'other', 'weight'), [1, 2, 3]]}))
90
    assert func(node_2)
91
92
93
def test_compile_expression_and():
94
    func = compile_ast(parse({'and': [{'has': 'application'}, {'in': [('weight',), [1, 2, 3]]}]}))
95
    assert not func(node_0)
96
    assert func(node_1)
97
    assert not func(node_2)
98
99
100
def test_compile_expression_or():
101
    func = compile_ast(parse({'or': [{'has': 'application'}, {'in': [('weight',), [1, 2, 3]]}]}))
102
    assert func(node_0)
103
    assert func(node_1)
104
    assert not func(node_2)
105
106
107
def test_compile_expression_xor():
108
    func = compile_ast(parse({'xor': [{'has': 'application'}, {'in': [('weight',), [1, 2, 3]]}]}))
109
    assert func(node_0)
110
    assert not func(node_1)
111
    assert not func(node_2)
112
113
114
def test_compile_expression_nxor():
115
    func = compile_ast(parse({'nxor': [{'has': 'application'}, {'in': [('weight',), [1, 2, 3]]}]}))
116
    assert not func(node_0)
117
    assert func(node_1)
118
    assert func(node_2)
119
120
121
def test_parse_exception():
122
    with pytest.raises(ParserException):
123
        parse({'has': ['_link', 'other', 'weight']})
124
125
126
def test_parse_raise_unknown_operator():
127
    with pytest.raises(ParserException):
128
        parse({'idontexistatall': ['_link', 'other', 'weight']})
129