Debian Codegraph @

tests/validation/test_lone_schema_definition.py

raw
1 from functools import partial
2
3 from graphql.utilities import build_schema
4 from graphql.validation.rules.lone_schema_definition import LoneSchemaDefinitionRule
5
6 from .harness import assert_sdl_validation_errors
7
8 assert_sdl_errors = partial(assert_sdl_validation_errors, LoneSchemaDefinitionRule)
9
10 assert_sdl_valid = partial(assert_sdl_errors, errors=[])
11
12
13 def describe_validate_schema_definition_should_be_alone():
14 def no_schema():
15 assert_sdl_valid("""
16 type Query {
17 foo: String
18 }
19 """)
20
21 def one_schema_definition():
22 assert_sdl_valid("""
23 schema {
24 query: Foo
25 }
26
27 type Foo {
28 foo: String
29 }
30 """)
31
32 def multiple_schema_definitions():
33 assert_sdl_errors(
34 """
35 schema {
36 query: Foo
37 }
38
39 type Foo {
40 foo: String
41 }
42
43 schema {
44 mutation: Foo
45 }
46
47 schema {
48 subscription: Foo
49 }
50 """,
51 [
52 {
53 "message": "Must provide only one schema definition.",
54 "locations": [(10, 13)],
55 },
56 {
57 "message": "Must provide only one schema definition.",
58 "locations": [(14, 13)],
59 },
60 ],
61 )
62
63 def define_schema_in_schema_extension():
64 schema = build_schema("""
65 type Foo {
66 foo: String
67 }
68 """)
69
70 assert_sdl_valid(
71 """
72 schema {
73 query: Foo
74 }
75 """,
76 schema=schema,
77 )
78
79 def redefine_schema_in_schema_extension():
80 schema = build_schema("""
81 schema {
82 query: Foo
83 }
84
85 type Foo {
86 foo: String
87 }
88 """)
89
90 assert_sdl_errors(
91 """
92 schema {
93 mutation: Foo
94 }
95 """,
96 [
97 {
98 "message": "Cannot define a new schema within a schema extension.",
99 "locations": [(2, 13)],
100 }
101 ],
102 schema,
103 )
104
105 def redefine_implicit_schema_in_schema_extension():
106 schema = build_schema("""
107 type Query {
108 fooField: Foo
109 }
110
111 type Foo {
112 foo: String
113 }
114 """)
115
116 assert_sdl_errors(
117 """
118 schema {
119 mutation: Foo
120 }
121 """,
122 [
123 {
124 "message": "Cannot define a new schema within a schema extension.",
125 "locations": [(2, 13)],
126 },
127 ],
128 schema,
129 )
130
131 def extend_schema_in_schema_extension():
132 schema = build_schema("""
133 type Query {
134 fooField: Foo
135 }
136
137 type Foo {
138 foo: String
139 }
140 """)
141
142 assert_sdl_valid(
143 """
144 extend schema {
145 mutation: Foo
146 }
147 """,
148 schema=schema,
149 )