Debian Codegraph @

tests/validation/test_unique_enum_value_names.py

raw
1 from functools import partial
2
3 from graphql.utilities import build_schema
4 from graphql.validation.rules.unique_enum_value_names import UniqueEnumValueNamesRule
5
6 from .harness import assert_sdl_validation_errors
7
8 assert_errors = partial(assert_sdl_validation_errors, UniqueEnumValueNamesRule)
9
10 assert_valid = partial(assert_errors, errors=[])
11
12
13 def describe_validate_unique_field_definition_names():
14 def no_values():
15 assert_valid("""
16 enum SomeEnum
17 """)
18
19 def one_value():
20 assert_valid("""
21 enum SomeEnum {
22 FOO
23 }
24 """)
25
26 def multiple_values():
27 assert_valid("""
28 enum SomeEnum {
29 FOO
30 BAR
31 }
32 """)
33
34 def duplicate_values_inside_the_same_enum_definition():
35 assert_errors(
36 """
37 enum SomeEnum {
38 FOO
39 BAR
40 FOO
41 }
42 """,
43 [
44 {
45 "message": "Enum value 'SomeEnum.FOO' can only be defined once.",
46 "locations": [(3, 15), (5, 15)],
47 },
48 ],
49 )
50
51 def extend_enum_with_new_value():
52 assert_valid("""
53 enum SomeEnum {
54 FOO
55 }
56 extend enum SomeEnum {
57 BAR
58 }
59 extend enum SomeEnum {
60 BAZ
61 }
62 """)
63
64 def extend_enum_with_duplicate_value():
65 assert_errors(
66 """
67 extend enum SomeEnum {
68 FOO
69 }
70 enum SomeEnum {
71 FOO
72 }
73 """,
74 [
75 {
76 "message": "Enum value 'SomeEnum.FOO' can only be defined once.",
77 "locations": [(3, 15), (6, 15)],
78 },
79 ],
80 )
81
82 def duplicate_value_inside_extension():
83 assert_errors(
84 """
85 enum SomeEnum
86 extend enum SomeEnum {
87 FOO
88 BAR
89 FOO
90 }
91 """,
92 [
93 {
94 "message": "Enum value 'SomeEnum.FOO' can only be defined once.",
95 "locations": [(4, 15), (6, 15)],
96 },
97 ],
98 )
99
100 def duplicate_value_inside_different_extension():
101 assert_errors(
102 """
103 enum SomeEnum
104 extend enum SomeEnum {
105 FOO
106 }
107 extend enum SomeEnum {
108 FOO
109 }
110 """,
111 [
112 {
113 "message": "Enum value 'SomeEnum.FOO' can only be defined once.",
114 "locations": [(4, 15), (7, 15)],
115 },
116 ],
117 )
118
119 def adding_new_value_to_the_enum_inside_existing_schema():
120 schema = build_schema("enum SomeEnum")
121 sdl = """
122 extend enum SomeEnum {
123 FOO
124 }
125 """
126
127 assert_valid(sdl, schema=schema)
128
129 def adding_conflicting_value_to_existing_schema_twice():
130 schema = build_schema("""
131 enum SomeEnum {
132 FOO
133 }
134 """)
135 sdl = """
136 extend enum SomeEnum {
137 FOO
138 }
139 extend enum SomeEnum {
140 FOO
141 }
142 """
143
144 assert_errors(
145 sdl,
146 [
147 {
148 "message": "Enum value 'SomeEnum.FOO' already exists in the schema."
149 " It cannot also be defined in this type extension.",
150 "locations": [(3, 15)],
151 },
152 {
153 "message": "Enum value 'SomeEnum.FOO' already exists in the schema."
154 " It cannot also be defined in this type extension.",
155 "locations": [(6, 15)],
156 },
157 ],
158 schema,
159 )
160
161 def adding_enum_values_to_existing_schema_twice():
162 schema = build_schema("enum SomeEnum")
163 sdl = """
164 extend enum SomeEnum {
165 FOO
166 }
167 extend enum SomeEnum {
168 FOO
169 }
170 """
171
172 assert_errors(
173 sdl,
174 [
175 {
176 "message": "Enum value 'SomeEnum.FOO' can only be defined once.",
177 "locations": [(3, 15), (6, 15)],
178 },
179 ],
180 schema,
181 )