Debian Codegraph @

tests/validation/test_no_unused_fragments.py

raw
1 from functools import partial
2
3 from graphql.validation import NoUnusedFragmentsRule
4
5 from .harness import assert_validation_errors
6
7 assert_errors = partial(assert_validation_errors, NoUnusedFragmentsRule)
8
9 assert_valid = partial(assert_errors, errors=[])
10
11
12 def describe_validate_no_unused_fragments():
13 def all_fragment_names_are_used():
14 assert_valid("""
15 {
16 human(id: 4) {
17 ...HumanFields1
18 ... on Human {
19 ...HumanFields2
20 }
21 }
22 }
23 fragment HumanFields1 on Human {
24 name
25 ...HumanFields3
26 }
27 fragment HumanFields2 on Human {
28 name
29 }
30 fragment HumanFields3 on Human {
31 name
32 }
33 """)
34
35 def all_fragment_names_are_used_by_multiple_operations():
36 assert_valid("""
37 query Foo {
38 human(id: 4) {
39 ...HumanFields1
40 }
41 }
42 query Bar {
43 human(id: 4) {
44 ...HumanFields2
45 }
46 }
47 fragment HumanFields1 on Human {
48 name
49 ...HumanFields3
50 }
51 fragment HumanFields2 on Human {
52 name
53 }
54 fragment HumanFields3 on Human {
55 name
56 }
57 """)
58
59 def contains_unknown_fragments():
60 assert_errors(
61 """
62 query Foo {
63 human(id: 4) {
64 ...HumanFields1
65 }
66 }
67 query Bar {
68 human(id: 4) {
69 ...HumanFields2
70 }
71 }
72 fragment HumanFields1 on Human {
73 name
74 ...HumanFields3
75 }
76 fragment HumanFields2 on Human {
77 name
78 }
79 fragment HumanFields3 on Human {
80 name
81 }
82 fragment Unused1 on Human {
83 name
84 }
85 fragment Unused2 on Human {
86 name
87 }
88 """,
89 [
90 {
91 "message": "Fragment 'Unused1' is never used.",
92 "locations": [(22, 13)],
93 },
94 {
95 "message": "Fragment 'Unused2' is never used.",
96 "locations": [(25, 13)],
97 },
98 ],
99 )
100
101 def contains_unknown_fragments_with_ref_cycle():
102 assert_errors(
103 """
104 query Foo {
105 human(id: 4) {
106 ...HumanFields1
107 }
108 }
109 query Bar {
110 human(id: 4) {
111 ...HumanFields2
112 }
113 }
114 fragment HumanFields1 on Human {
115 name
116 ...HumanFields3
117 }
118 fragment HumanFields2 on Human {
119 name
120 }
121 fragment HumanFields3 on Human {
122 name
123 }
124 fragment Unused1 on Human {
125 name
126 ...Unused2
127 }
128 fragment Unused2 on Human {
129 name
130 ...Unused1
131 }
132 """,
133 [
134 {
135 "message": "Fragment 'Unused1' is never used.",
136 "locations": [(22, 13)],
137 },
138 {
139 "message": "Fragment 'Unused2' is never used.",
140 "locations": [(26, 13)],
141 },
142 ],
143 )
144
145 def contains_unknown_and_undefined_fragments():
146 assert_errors(
147 """
148 query Foo {
149 human(id: 4) {
150 ...bar
151 }
152 }
153 fragment foo on Human {
154 name
155 }
156 """,
157 [{"message": "Fragment 'foo' is never used.", "locations": [(7, 13)]}],
158 )