-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_directives_estimator.py
148 lines (111 loc) · 3.16 KB
/
test_directives_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
from graphql import build_schema
from graphql_complexity import DirectivesEstimator, get_complexity
def _evaluate_complexity_with_directives_estimator(
query: str,
schema: str,
**kwargs,
):
estimator = DirectivesEstimator(schema, **kwargs)
return get_complexity(query, build_schema(schema), estimator)
def test_simple_query_with_directive_estimator():
schema = """directive @complexity(
# The complexity value for the field
value: Int!
# Optional multipliers
multipliers: [String!]
) on FIELD_DEFINITION
type Query {
# Fixed complexity of 5
someField: String @complexity(value: 5)
}
"""
query = """
query Something {
someField
}
"""
complexity = _evaluate_complexity_with_directives_estimator(query, schema)
assert complexity == 5
def test_directive_estimator_accepts_to_set_directive_name():
schema = """directive @cost(
value: Int!
) on FIELD_DEFINITION
type Query {
oneField: String @cost(value: 3)
otherField: String @cost(value: 1)
withoutDirective: String
}
"""
query = """
query Something {
oneField
otherField
withoutDirective
}
"""
complexity = _evaluate_complexity_with_directives_estimator(
query, schema, directive_name="cost"
)
assert complexity == 5
def test_():
schema = """directive @complexity(
# The complexity value for the field
value: Int!
# Optional multipliers
multipliers: [String!]
) on FIELD_DEFINITION
type Query {
someField (limit: Int): String @complexity(value: 5, multipliers: ["limit"])
}
"""
query = """
query Something {
someField (limit: 10)
}
"""
complexity = _evaluate_complexity_with_directives_estimator(query, schema)
assert complexity == 5
# Add more unit tests regarding the directive estimator
def test_directive_estimator_accepts_to_set_missing_complexity():
schema = """directive @complexity(
# The complexity value for the field
value: Int!
) on FIELD_DEFINITION
type Query {
oneField: String @complexity(value: 3)
otherField: String @complexity(value: 1)
withoutDirective: String
}
"""
query = """
query Something {
oneField
otherField
withoutDirective
}
"""
complexity = _evaluate_complexity_with_directives_estimator(
query, schema, missing_complexity=10
)
assert complexity == 14
def test_directive_estimator_should_accept_field_with_directive_is_part_of_an_object_with_directive():
schema = """directive @complexity(
# The complexity value for the field
value: Int!
) on FIELD_DEFINITION
type Query {
oneField: Obj @complexity(value: 3)
}
type Obj {
otherField: String @complexity(value: 120)
}
"""
query = """
query Something {
oneField {
otherField
}
}
"""
complexity = _evaluate_complexity_with_directives_estimator(query, schema)
assert complexity == 123