Skip to content

Commit b000a8b

Browse files
committed
Move directive predicates tests and fix test names
Replicates graphql/graphql-js@36d13ae
1 parent 3c562f6 commit b000a8b

File tree

4 files changed

+68
-43
lines changed

4 files changed

+68
-43
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ a query language for APIs created by Facebook.
1313
[![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
1414

1515
The current version 1.0.2 of GraphQL-core-next is up-to-date with GraphQL.js version
16-
14.1.1. All parts of the API are covered by an extensive test suite of currently 1724
16+
14.1.1. All parts of the API are covered by an extensive test suite of currently 1730
1717
unit tests.
1818

1919

graphql/type/directives.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def assert_directive(directive: Any) -> GraphQLDirective:
175175

176176
def is_specified_directive(directive: GraphQLDirective):
177177
"""Check whether the given directive is one of the specified directives."""
178-
return any(
178+
return isinstance(directive, GraphQLDirective) and any(
179179
specified_directive.name == directive.name
180180
for specified_directive in specified_directives
181181
)

tests/type/test_directives.py

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
from pytest import raises
22

33
from graphql.language import DirectiveLocation, DirectiveDefinitionNode, Node
4-
from graphql.type import (
5-
GraphQLArgument,
6-
GraphQLDirective,
7-
GraphQLInt,
8-
GraphQLString,
9-
GraphQLSkipDirective,
10-
is_directive,
11-
is_specified_directive,
12-
)
4+
from graphql.type import GraphQLArgument, GraphQLDirective, GraphQLInt, GraphQLString
135

146

157
def describe_type_system_directive():
@@ -77,19 +69,19 @@ def directive_has_repr():
7769
directive = GraphQLDirective("foo", [])
7870
assert repr(directive) == "<GraphQLDirective(@foo)>"
7971

80-
def reject_an_unnamed_directivce():
72+
def rejects_an_unnamed_directive():
8173
with raises(TypeError) as exc_info:
8274
# noinspection PyTypeChecker
8375
GraphQLDirective(None, locations=[]) # type: ignore
8476
assert str(exc_info.value) == "Directive must be named."
8577

86-
def reject_directive_with_incorrectly_typed_name():
78+
def rejects_a_directive_with_incorrectly_typed_name():
8779
with raises(TypeError) as exc_info:
8880
# noinspection PyTypeChecker
8981
GraphQLDirective({"bad": True}, locations=[]) # type: ignore
9082
assert str(exc_info.value) == "The directive name must be a string."
9183

92-
def reject_directive_with_incorrectly_typed_args():
84+
def rejects_a_directive_with_incorrectly_typed_args():
9385
with raises(TypeError) as exc_info:
9486
# noinspection PyTypeChecker
9587
GraphQLDirective("Foo", locations=[], args=["arg"]) # type: ignore
@@ -113,13 +105,13 @@ def reject_directive_with_incorrectly_typed_args():
113105
"Foo args must be GraphQLArgument or input type objects."
114106
)
115107

116-
def reject_directive_with_undefined_locations():
108+
def rejects_a_directive_with_undefined_locations():
117109
with raises(TypeError) as exc_info:
118110
# noinspection PyTypeChecker
119111
GraphQLDirective("Foo", locations=None) # type: ignore
120112
assert str(exc_info.value) == "Foo locations must be a list/tuple."
121113

122-
def recect_directive_with_incorrectly_typed_locations():
114+
def recects_a_directive_with_incorrectly_typed_locations():
123115
with raises(TypeError) as exc_info:
124116
# noinspection PyTypeChecker
125117
GraphQLDirective("Foo", locations="bad") # type: ignore
@@ -131,43 +123,18 @@ def recect_directive_with_incorrectly_typed_locations():
131123
"Foo locations must be DirectiveLocation objects."
132124
)
133125

134-
def reject_directive_with_incorrectly_typed_description():
126+
def rejects_a_directive_with_incorrectly_typed_description():
135127
with raises(TypeError) as exc_info:
136128
# noinspection PyTypeChecker
137129
GraphQLDirective(
138130
"Foo", locations=[], description={"bad": True}
139131
) # type: ignore
140132
assert str(exc_info.value) == "Foo description must be a string."
141133

142-
def reject_directive_with_incorrectly_typed_ast_node():
134+
def rejects_a_directive_with_incorrectly_typed_ast_node():
143135
with raises(TypeError) as exc_info:
144136
# noinspection PyTypeChecker
145137
GraphQLDirective("Foo", locations=[], ast_node=Node()) # type: ignore
146138
assert str(exc_info.value) == (
147139
"Foo AST node must be a DirectiveDefinitionNode."
148140
)
149-
150-
151-
def describe_directive_predicates():
152-
def describe_is_directive():
153-
def returns_true_for_directive():
154-
directive = GraphQLDirective("Foo", [])
155-
assert is_directive(directive) is True
156-
157-
def returns_false_for_type_class_rather_than_instance():
158-
assert is_directive(GraphQLDirective) is False
159-
160-
def returns_false_for_other_instances():
161-
assert is_directive(GraphQLString) is False
162-
163-
def returns_false_for_random_garbage():
164-
assert is_directive(None) is False
165-
assert is_directive({"what": "is this"}) is False
166-
167-
def describe_is_specified_directive():
168-
def returns_true_for_specified_directive():
169-
assert is_specified_directive(GraphQLSkipDirective) is True
170-
171-
def returns_false_for_unspecified_directive():
172-
directive = GraphQLDirective("Foo", [])
173-
assert is_specified_directive(directive) is False

tests/type/test_predicate.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
from pytest import raises
22

3+
from graphql.language import DirectiveLocation
34
from graphql.type import (
45
GraphQLArgument,
6+
GraphQLDeprecatedDirective,
7+
GraphQLDirective,
58
GraphQLEnumType,
9+
GraphQLIncludeDirective,
610
GraphQLInputField,
711
GraphQLInputObjectType,
812
GraphQLInterfaceType,
913
GraphQLList,
1014
GraphQLNonNull,
1115
GraphQLObjectType,
1216
GraphQLScalarType,
17+
GraphQLSkipDirective,
1318
GraphQLString,
1419
GraphQLUnionType,
1520
assert_abstract_type,
@@ -33,6 +38,7 @@
3338
get_nullable_type,
3439
is_abstract_type,
3540
is_composite_type,
41+
is_directive,
3642
is_enum_type,
3743
is_input_object_type,
3844
is_input_type,
@@ -47,6 +53,7 @@
4753
is_object_type,
4854
is_output_type,
4955
is_scalar_type,
56+
is_specified_directive,
5057
is_type,
5158
is_union_type,
5259
is_wrapping_type,
@@ -453,3 +460,54 @@ def returns_false_for_optional_input_field():
453460
GraphQLNonNull(GraphQLString), default_value="default"
454461
)
455462
assert is_required_input_field(opt_field4) is False
463+
464+
def describe_directive_predicates():
465+
def describe_is_directive():
466+
def returns_true_for_directives():
467+
directive = GraphQLDirective("Foo", [DirectiveLocation.QUERY])
468+
assert is_directive(directive) is True
469+
assert is_directive(GraphQLSkipDirective) is True
470+
471+
def returns_false_for_directive_class_rather_than_instance():
472+
assert is_directive(GraphQLDirective) is False
473+
474+
def returns_false_for_object_type():
475+
assert is_directive(ObjectType) is False
476+
477+
def returns_false_for_scalar_type():
478+
assert is_directive(GraphQLString) is False
479+
480+
def returns_false_for_random_garbage():
481+
assert is_directive(None) is False
482+
assert is_directive({"what": "is this"}) is False
483+
484+
def describe_is_specified_directive():
485+
def returns_true_for_specified_directives():
486+
assert is_specified_directive(GraphQLIncludeDirective) is True
487+
assert is_specified_directive(GraphQLSkipDirective) is True
488+
assert is_specified_directive(GraphQLDeprecatedDirective) is True
489+
490+
def returns_false_for_custom_directive():
491+
directive = GraphQLDirective("Foo", [DirectiveLocation.QUERY])
492+
assert is_specified_directive(directive) is False
493+
494+
def returns_false_for_directive_class_rather_than_instance():
495+
assert is_specified_directive(GraphQLDirective) is False
496+
497+
def returns_false_for_object_type():
498+
assert is_specified_directive(ObjectType) is False
499+
500+
def returns_false_for_spec_defined_scalar_type():
501+
assert is_specified_directive(GraphQLString) is False
502+
503+
def returns_false_for_scalar_type_with_name_of_specified_directive():
504+
assert (
505+
is_specified_directive(
506+
GraphQLScalarType("deprecated", lambda: None)
507+
)
508+
is False
509+
)
510+
511+
def returns_false_for_random_garbage():
512+
assert is_specified_directive(None) is False
513+
assert is_specified_directive({"what": "is this"}) is False

0 commit comments

Comments
 (0)