Skip to content

Commit 8845f38

Browse files
committed
test_no_deprecated: simplify test cases
Replicates graphql/graphql-js@19f2c52
1 parent ce61c88 commit 8845f38

File tree

1 file changed

+129
-93
lines changed

1 file changed

+129
-93
lines changed
Lines changed: 129 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,155 @@
11
from functools import partial
2+
from typing import Callable, List, Tuple
23

34
from graphql.utilities import build_schema
45
from graphql.validation import NoDeprecatedCustomRule
56

67
from .harness import assert_validation_errors
78

8-
schema = build_schema(
9-
"""
10-
enum EnumType {
11-
NORMAL_VALUE
12-
DEPRECATED_VALUE @deprecated(reason: "Some enum reason.")
13-
DEPRECATED_VALUE_WITH_NO_REASON @deprecated
14-
}
159

16-
type Query {
17-
normalField(enumArg: [EnumType]): String
18-
deprecatedField: String @deprecated(reason: "Some field reason.")
19-
deprecatedFieldWithNoReason: String @deprecated
20-
}
21-
"""
22-
)
23-
24-
assert_errors = partial(assert_validation_errors, NoDeprecatedCustomRule, schema=schema)
25-
26-
assert_valid = partial(assert_errors, errors=[])
10+
def build_assertions(
11+
sdl_str: str,
12+
) -> Tuple[Callable[[str], None], Callable[[str, List], None]]:
13+
schema = build_schema(sdl_str)
14+
assert_errors = partial(
15+
assert_validation_errors, NoDeprecatedCustomRule, schema=schema
16+
)
17+
assert_valid = partial(assert_errors, errors=[])
18+
return (
19+
assert_valid,
20+
assert_errors,
21+
) # type: ignore
2722

2823

2924
def describe_validate_no_deprecated():
30-
def ignores_fields_and_enum_values_that_are_not_deprecated():
31-
assert_valid(
25+
def describe_no_deprecated_fields():
26+
_assert_valid, _assert_errors = build_assertions(
3227
"""
33-
{
34-
normalField(enumArg: [NORMAL_VALUE])
28+
type Query {
29+
normalField: String
30+
deprecatedField: String @deprecated(reason: "Some field reason.")
3531
}
3632
"""
3733
)
3834

39-
def ignores_unknown_fields_and_enum_values():
40-
assert_valid(
35+
def ignores_fields_and_enum_values_that_are_not_deprecated():
36+
_assert_valid(
37+
"""
38+
{
39+
normalField
40+
}
41+
"""
42+
)
43+
44+
def ignores_unknown_fields():
45+
_assert_valid(
46+
"""
47+
{
48+
unknownField
49+
}
50+
51+
fragment UnknownFragment on UnknownType {
52+
unknownField
53+
}
54+
"""
55+
)
56+
57+
def reports_error_when_a_deprecated_field_is_selected():
58+
message = (
59+
"The field Query.deprecatedField is deprecated. Some field reason."
60+
)
61+
_assert_errors(
62+
"""
63+
{
64+
deprecatedField
65+
}
66+
67+
fragment QueryFragment on Query {
68+
deprecatedField
69+
}
70+
""",
71+
[
72+
{
73+
"message": message,
74+
"locations": [(3, 19)],
75+
},
76+
{
77+
"message": message,
78+
"locations": [(7, 19)],
79+
},
80+
],
81+
)
82+
83+
def describe_no_deprecated_enum_values():
84+
_assert_valid, _assert_errors = build_assertions(
4185
"""
42-
fragment UnknownFragment on UnknownType {
43-
unknownField(unknownArg: UNKNOWN_VALUE)
86+
enum EnumType {
87+
NORMAL_VALUE
88+
DEPRECATED_VALUE @deprecated(reason: "Some enum reason.")
4489
}
4590
46-
fragment QueryFragment on Query {
47-
unknownField(unknownArg: UNKNOWN_VALUE)
48-
normalField(enumArg: UNKNOWN_VALUE)
91+
type Query {
92+
someField(enumArg: EnumType): String
4993
}
5094
"""
5195
)
5296

53-
def reports_error_when_a_deprecated_field_is_selected():
54-
assert_errors(
55-
"""
56-
{
57-
normalField
58-
deprecatedField
59-
deprecatedFieldWithNoReason
60-
}
61-
""",
62-
[
63-
{
64-
"message": "The field Query.deprecatedField is deprecated."
65-
" Some field reason.",
66-
"locations": [(4, 15)],
67-
},
97+
def ignores_enum_values_that_are_not_deprecated():
98+
_assert_valid(
99+
"""
68100
{
69-
"message": "The field Query.deprecatedFieldWithNoReason"
70-
" is deprecated. No longer supported",
71-
"locations": [(5, 15)],
72-
},
73-
],
74-
)
101+
normalField(enumArg: NORMAL_VALUE)
102+
}
103+
"""
104+
)
75105

76-
def reports_error_when_a_deprecated_enum_value_is_used():
77-
assert_errors(
78-
"""
79-
{
80-
normalField(enumArg: [NORMAL_VALUE, DEPRECATED_VALUE])
81-
normalField(enumArg: [DEPRECATED_VALUE_WITH_NO_REASON])
82-
}
83-
""",
84-
[
85-
{
86-
"message": "The enum value 'EnumType.DEPRECATED_VALUE'"
87-
" is deprecated. Some enum reason.",
88-
"locations": [(3, 51)],
89-
},
90-
{
91-
"message": "The enum value"
92-
" 'EnumType.DEPRECATED_VALUE_WITH_NO_REASON'"
93-
" is deprecated. No longer supported",
94-
"locations": [(4, 37)],
95-
},
96-
],
97-
)
106+
def ignores_unknown_enum_values():
107+
_assert_valid(
108+
"""
109+
query (
110+
$unknownValue: EnumType = UNKNOWN_VALUE
111+
$unknownType: UnknownType = UNKNOWN_VALUE
112+
) {
113+
someField(enumArg: UNKNOWN_VALUE)
114+
someField(unknownArg: UNKNOWN_VALUE)
115+
unknownField(unknownArg: UNKNOWN_VALUE)
116+
}
98117
99-
def reports_error_when_deprecated_field_or_enum_value_is_used_inside_a_fragment():
100-
assert_errors(
101-
"""
102-
fragment QueryFragment on Query {
103-
deprecatedField
104-
normalField(enumArg: [NORMAL_VALUE, DEPRECATED_VALUE])
105-
}
106-
""",
107-
[
108-
{
109-
"message": "The field Query.deprecatedField is deprecated."
110-
" Some field reason.",
111-
"locations": [(3, 15)],
112-
},
113-
{
114-
"message": "The enum value 'EnumType.DEPRECATED_VALUE'"
115-
" is deprecated. Some enum reason.",
116-
"locations": [(4, 51)],
117-
},
118-
],
119-
)
118+
fragment SomeFragment on Query {
119+
someField(enumArg: UNKNOWN_VALUE)
120+
}
121+
"""
122+
)
123+
124+
def reports_error_when_a_deprecated_enum_value_is_used():
125+
message = (
126+
"The enum value 'EnumType.DEPRECATED_VALUE' is deprecated."
127+
" Some enum reason."
128+
)
129+
_assert_errors(
130+
"""
131+
query (
132+
$variable: EnumType = DEPRECATED_VALUE
133+
) {
134+
someField(enumArg: DEPRECATED_VALUE)
135+
}
136+
137+
fragment QueryFragment on Query {
138+
someField(enumArg: DEPRECATED_VALUE)
139+
}
140+
""",
141+
[
142+
{
143+
"message": message,
144+
"locations": [(3, 41)],
145+
},
146+
{
147+
"message": message,
148+
"locations": [(5, 38)],
149+
},
150+
{
151+
"message": message,
152+
"locations": [(9, 38)],
153+
},
154+
],
155+
)

0 commit comments

Comments
 (0)