Debian Codegraph @

tests/validation/test_unique_directive_names.py

raw
1 from functools import partial
2
3 from graphql.utilities import build_schema
4 from graphql.validation.rules.unique_directive_names import UniqueDirectiveNamesRule
5
6 from .harness import assert_sdl_validation_errors
7
8 assert_errors = partial(assert_sdl_validation_errors, UniqueDirectiveNamesRule)
9
10 assert_valid = partial(assert_errors, errors=[])
11
12
13 def describe_validate_unique_directive_names():
14 def no_directive():
15 assert_valid("""
16 type Foo
17 """)
18
19 def one_directive():
20 assert_valid("""
21 directive @foo on SCHEMA
22 """)
23
24 def many_directives():
25 assert_valid("""
26 directive @foo on SCHEMA
27 directive @bar on SCHEMA
28 directive @baz on SCHEMA
29 """)
30
31 def directive_and_non_directive_definitions_named_the_same():
32 assert_valid("""
33 query foo { __typename }
34 fragment foo on foo { __typename }
35 type foo
36
37 directive @foo on SCHEMA
38 """)
39
40 def directives_named_the_same():
41 assert_errors(
42 """
43 directive @foo on SCHEMA
44
45 directive @foo on SCHEMA
46 """,
47 [
48 {
49 "message": "There can be only one directive named '@foo'.",
50 "locations": [(2, 24), (4, 24)],
51 }
52 ],
53 )
54
55 def adding_new_directive_to_existing_schema():
56 schema = build_schema("directive @foo on SCHEMA")
57
58 assert_valid("directive @bar on SCHEMA", schema=schema)
59
60 def adding_new_directive_with_standard_name_to_existing_schema():
61 schema = build_schema("type foo")
62
63 assert_errors(
64 "directive @skip on SCHEMA",
65 [
66 {
67 "message": "Directive '@skip' already exists in the schema."
68 " It cannot be redefined.",
69 "locations": [(1, 12)],
70 }
71 ],
72 schema,
73 )
74
75 def adding_new_directive_to_existing_schema_with_same_named_type():
76 schema = build_schema("type foo")
77
78 assert_valid("directive @foo on SCHEMA", schema=schema)
79
80 def adding_conflicting_directives_to_existing_schema():
81 schema = build_schema("directive @foo on SCHEMA")
82
83 assert_errors(
84 "directive @foo on SCHEMA",
85 [
86 {
87 "message": "Directive '@foo' already exists in the schema."
88 " It cannot be redefined.",
89 "locations": [(1, 12)],
90 }
91 ],
92 schema,
93 )