-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_simple_estimator.py
153 lines (102 loc) · 3.97 KB
/
test_simple_estimator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import pytest
from graphql import build_schema
from graphql_complexity import SimpleEstimator, get_complexity
from tests.ut_utils import schema
def _evaluate_complexity_with_simple_estimator(query: str, field_complexity=1):
estimator = SimpleEstimator(field_complexity)
return get_complexity(query, build_schema(schema), estimator)
def test_root_fields_do_not_multiply():
query = """
query Something {
version
}
"""
complexity = _evaluate_complexity_with_simple_estimator(query, 3)
assert complexity == 3
def test_root_fields_complexity_is_added():
query = """
query Something {
version
anotherVersion: version
}
"""
complexity = _evaluate_complexity_with_simple_estimator(query, 5)
assert complexity == 10
def test_complexity_should_not_count_fields_with_include_directive_false_given_by_variable():
query = """query Foo ($shouldSkip: Boolean = false) {
version @include(if: $shouldSkip)
}"""
complexity = _evaluate_complexity_with_simple_estimator(query, 1)
assert complexity == 0
def test_complexity_should_not_count_fields_with_include_directive_false_given_by_boolean_value():
query = """query {
version @include(if: false)
}"""
complexity = _evaluate_complexity_with_simple_estimator(query, 1)
assert complexity == 0
def test_complexity_should_not_count_objects_with_include_directive_false_given_by_boolean_value():
query = """query {
droid @include(if: false) {
id
friends {
name
}
}
version
}"""
complexity = _evaluate_complexity_with_simple_estimator(query, 1)
assert complexity == 1
def test_complexity_should_not_count_inner_directive_of_object_with_include_directive_false_given_by_boolean_value():
query = """query {
droid @include(if: false) {
name @include(if: true)
friends @include(if: false) {
name @include(if: true)
}
}
version
}"""
complexity = _evaluate_complexity_with_simple_estimator(query, 1)
assert complexity == 1
def test_complexity_should_count_fields_with_include_directive_true_given_by_boolean_value():
query = """query {
version @include(if: true)
}"""
complexity = _evaluate_complexity_with_simple_estimator(query, 1)
assert complexity == 1
def test_complexity_should_not_count_fields_with_skip_directive_true_given_by_boolean_value():
query = """query {
version(count: 10) @skip(if: true)
}"""
complexity = _evaluate_complexity_with_simple_estimator(query, 1)
assert complexity == 0
def test_complexity_should_count_fields_with_skip_directive_false_given_by_boolean_value():
query = """query {
version(count: 10) @skip(if: false)
}"""
complexity = _evaluate_complexity_with_simple_estimator(query, 1)
assert complexity == 1
def test_complexity_should_count_fields_with_skip_directive_followed_by_include():
query = """query {
version(count: 10) @skip(if: false) @include(if: true)
}"""
complexity = _evaluate_complexity_with_simple_estimator(query, 1)
assert complexity == 1
def test_complexity_with_other_directives_not_affecting_evaluation():
query = """query {
version(count: 10) @deprecated(reason: "Test")
}"""
complexity = _evaluate_complexity_with_simple_estimator(query, 1)
assert complexity == 1
def test_simple_estimator_should_not_allow_negative_cost():
"""It should now allow negative cost"""
with pytest.raises(ValueError, match=r"^'complexity' must be a positive integer \(greater or equal than 0\)$"):
SimpleEstimator(-1)
def test_9():
# should ignore unknown fragments
query = """query {
...UnknownFragment
version(count: 100)
}""" # Simple Estimator(10): 10
complexity = _evaluate_complexity_with_simple_estimator(query, 10)
assert complexity == 10