-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathtest_star_wars_validation.py
113 lines (96 loc) · 3.11 KB
/
test_star_wars_validation.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
from __future__ import annotations
from typing import TYPE_CHECKING
from graphql.language import Source, parse
from graphql.validation import validate
from .star_wars_schema import star_wars_schema
if TYPE_CHECKING:
from graphql.error import GraphQLError
def validation_errors(query: str) -> list[GraphQLError]:
"""Helper function to test a query and the expected response."""
source = Source(query, "StarWars.graphql")
ast = parse(source)
return validate(star_wars_schema, ast)
def describe_star_wars_validation_tests():
def describe_basic_queries():
def validates_a_complex_but_valid_query():
query = """
query NestedQueryWithFragment {
hero {
...NameAndAppearances
friends {
...NameAndAppearances
friends {
...NameAndAppearances
}
}
}
}
fragment NameAndAppearances on Character {
name
appearsIn
}
"""
assert not validation_errors(query)
def notes_that_non_existent_fields_are_invalid():
query = """
query HeroSpaceshipQuery {
hero {
favoriteSpaceship
}
}
"""
assert validation_errors(query)
def requires_fields_on_object():
query = """
query HeroNoFieldsQuery {
hero
}
"""
assert validation_errors(query)
def disallows_fields_on_scalars():
query = """
query HeroFieldsOnScalarQuery {
hero {
name {
firstCharacterOfName
}
}
}
"""
assert validation_errors(query)
def disallows_object_fields_on_interfaces():
query = """
query DroidFieldOnCharacter {
hero {
name
primaryFunction
}
}
"""
assert validation_errors(query)
def allows_object_fields_in_fragments():
query = """
query DroidFieldInFragment {
hero {
name
...DroidFields
}
}
fragment DroidFields on Droid {
primaryFunction
}
"""
assert not validation_errors(query)
def allows_object_fields_in_inline_fragments():
query = """
query DroidFieldInFragment {
hero {
name
...DroidFields
}
}
fragment DroidFields on Droid {
primaryFunction
}
"""
assert not validation_errors(query)