-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_examples.py
71 lines (54 loc) · 1.45 KB
/
test_examples.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
from graphql import build_schema
from graphql_complexity import (
DirectivesEstimator,
SimpleEstimator,
get_complexity
)
def _evaluate_complexity_with_simple_estimator(query: str, schema: str, field_complexity=1):
estimator = SimpleEstimator(field_complexity)
return get_complexity(query, build_schema(schema), estimator)
def test_root_fields_complexity_is_added():
schema = """
type Obj {
name: String
email: String
}
type Query {
user: Obj
}
"""
query = """
query {
user {
name
email
}
}
"""
complexity = _evaluate_complexity_with_simple_estimator(query, schema, 2)
assert complexity == 6
def _evaluate_complexity_with_directives_estimator(
query: str,
schema: str,
):
estimator = DirectivesEstimator(schema)
return get_complexity(query, build_schema(schema), estimator)
def test_simple_query_with_directive_estimator():
schema = """directive @complexity(
value: Int!
) on FIELD_DEFINITION
type Query {
oneField: String @complexity(value: 5)
otherField: String @complexity(value: 1)
withoutDirective: String
}
"""
query = """
query Something {
oneField
otherField
withoutDirective
}
"""
complexity = _evaluate_complexity_with_directives_estimator(query, schema)
assert complexity == 7