Skip to content

Commit 04166d6

Browse files
committed
Add tests for GraphQLDirective
Replicates graphql/graphql-js@6741ac2 We actually had already most of the tests in Python.
1 parent 3fbb46a commit 04166d6

File tree

2 files changed

+73
-44
lines changed

2 files changed

+73
-44
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.1 of GraphQL-core-next is up-to-date with GraphQL.js version
16-
14.0.2. All parts of the API are covered by an extensive test suite of currently 1690
16+
14.0.2. All parts of the API are covered by an extensive test suite of currently 1692
1717
unit tests.
1818

1919

tests/type/test_directives.py

Lines changed: 72 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
from graphql.type import (
55
GraphQLArgument,
66
GraphQLDirective,
7+
GraphQLInt,
78
GraphQLString,
89
GraphQLSkipDirective,
910
is_directive,
1011
is_specified_directive,
1112
)
1213

1314

14-
def describe_graphql_directive():
15+
def describe_type_system_directive():
1516
def can_create_instance():
1617
arg = GraphQLArgument(GraphQLString, description="arg description")
1718
node = DirectiveDefinitionNode()
@@ -29,100 +30,128 @@ def can_create_instance():
2930
assert directive.description == "test description"
3031
assert directive.ast_node is node
3132

32-
def has_str():
33-
directive = GraphQLDirective("test", [])
34-
assert str(directive) == "@test"
33+
def defines_a_directive_with_no_args():
34+
locations = [DirectiveLocation.QUERY]
35+
directive = GraphQLDirective("Foo", locations=locations)
3536

36-
def has_repr():
37-
directive = GraphQLDirective("test", [])
38-
assert repr(directive) == "<GraphQLDirective(@test)>"
37+
assert directive.name == "Foo"
38+
assert directive.args == {}
39+
assert directive.locations == locations
40+
41+
def defines_a_directive_with_multiple_args():
42+
args = {
43+
"foo": GraphQLArgument(GraphQLString),
44+
"bar": GraphQLArgument(GraphQLInt),
45+
}
46+
locations = [DirectiveLocation.QUERY]
47+
directive = GraphQLDirective("Foo", locations=locations, args=args)
48+
49+
assert directive.name == "Foo"
50+
assert directive.args == args
51+
assert directive.locations == locations
52+
53+
def directive_accepts_input_types_as_arguments():
54+
# noinspection PyTypeChecker
55+
directive = GraphQLDirective(
56+
name="Foo", locations=[], args={"arg": GraphQLString}
57+
) # type: ignore
58+
arg = directive.args["arg"]
59+
assert isinstance(arg, GraphQLArgument)
60+
assert arg.type is GraphQLString
3961

40-
def accepts_strings_as_locations():
62+
def directive_accepts_strings_as_locations():
4163
# noinspection PyTypeChecker
4264
directive = GraphQLDirective(
43-
name="test", locations=["SCHEMA", "OBJECT"]
65+
name="Foo", locations=["SCHEMA", "OBJECT"]
4466
) # type: ignore
4567
assert directive.locations == [
4668
DirectiveLocation.SCHEMA,
4769
DirectiveLocation.OBJECT,
4870
]
4971

50-
def accepts_input_types_as_arguments():
51-
# noinspection PyTypeChecker
52-
directive = GraphQLDirective(
53-
name="test", locations=[], args={"arg": GraphQLString}
54-
) # type: ignore
55-
arg = directive.args["arg"]
56-
assert isinstance(arg, GraphQLArgument)
57-
assert arg.type is GraphQLString
72+
def directive_has_str():
73+
directive = GraphQLDirective("foo", [])
74+
assert str(directive) == "@foo"
75+
76+
def directive_has_repr():
77+
directive = GraphQLDirective("foo", [])
78+
assert repr(directive) == "<GraphQLDirective(@foo)>"
5879

59-
def does_not_accept_a_bad_name():
80+
def reject_an_unnamed_directivce():
6081
with raises(TypeError) as exc_info:
6182
# noinspection PyTypeChecker
6283
GraphQLDirective(None, locations=[]) # type: ignore
6384
assert str(exc_info.value) == "Directive must be named."
85+
86+
def reject_directive_with_incorrectly_typed_name():
6487
with raises(TypeError) as exc_info:
6588
# noinspection PyTypeChecker
6689
GraphQLDirective({"bad": True}, locations=[]) # type: ignore
6790
assert str(exc_info.value) == "The directive name must be a string."
6891

69-
def does_not_accept_bad_locations():
70-
with raises(TypeError) as exc_info:
71-
# noinspection PyTypeChecker
72-
GraphQLDirective("test", locations="bad") # type: ignore
73-
assert str(exc_info.value) == "test locations must be a list/tuple."
92+
def reject_directive_with_incorrectly_typed_args():
7493
with raises(TypeError) as exc_info:
7594
# noinspection PyTypeChecker
76-
GraphQLDirective("test", locations=["bad"]) # type: ignore
95+
GraphQLDirective("Foo", locations=[], args=["arg"]) # type: ignore
7796
assert str(exc_info.value) == (
78-
"test locations must be DirectiveLocation objects."
97+
"Foo args must be a dict with argument names as keys."
7998
)
80-
81-
def does_not_accept_bad_args():
8299
with raises(TypeError) as exc_info:
83100
# noinspection PyTypeChecker
84-
GraphQLDirective("test", locations=[], args=["arg"]) # type: ignore
101+
GraphQLDirective(
102+
"Foo", locations=[], args={1: GraphQLArgument(GraphQLString)}
103+
) # type: ignore
85104
assert str(exc_info.value) == (
86-
"test args must be a dict with argument names as keys."
105+
"Foo args must be a dict with argument names as keys."
87106
)
88107
with raises(TypeError) as exc_info:
89108
# noinspection PyTypeChecker
90109
GraphQLDirective(
91-
"test", locations=[], args={1: GraphQLArgument(GraphQLString)}
110+
"Foo", locations=[], args={"arg": GraphQLDirective("Bar", [])}
92111
) # type: ignore
93112
assert str(exc_info.value) == (
94-
"test args must be a dict with argument names as keys."
113+
"Foo args must be GraphQLArgument or input type objects."
95114
)
115+
116+
def reject_directive_with_undefined_locations():
96117
with raises(TypeError) as exc_info:
97118
# noinspection PyTypeChecker
98-
GraphQLDirective(
99-
"test", locations=[], args={"arg": GraphQLDirective("test", [])}
100-
) # type: ignore
119+
GraphQLDirective("Foo", locations=None) # type: ignore
120+
assert str(exc_info.value) == "Foo locations must be a list/tuple."
121+
122+
def recect_directive_with_incorrectly_typed_locations():
123+
with raises(TypeError) as exc_info:
124+
# noinspection PyTypeChecker
125+
GraphQLDirective("Foo", locations="bad") # type: ignore
126+
assert str(exc_info.value) == "Foo locations must be a list/tuple."
127+
with raises(TypeError) as exc_info:
128+
# noinspection PyTypeChecker
129+
GraphQLDirective("Foo", locations=["bad"]) # type: ignore
101130
assert str(exc_info.value) == (
102-
"test args must be GraphQLArgument or input type objects."
131+
"Foo locations must be DirectiveLocation objects."
103132
)
104133

105-
def does_not_accept_a_bad_description():
134+
def reject_directive_with_incorrectly_typed_description():
106135
with raises(TypeError) as exc_info:
107136
# noinspection PyTypeChecker
108137
GraphQLDirective(
109-
"test", locations=[], description={"bad": True}
138+
"Foo", locations=[], description={"bad": True}
110139
) # type: ignore
111-
assert str(exc_info.value) == "test description must be a string."
140+
assert str(exc_info.value) == "Foo description must be a string."
112141

113-
def does_not_accept_a_bad_ast_node():
142+
def reject_directive_with_incorrectly_typed_ast_node():
114143
with raises(TypeError) as exc_info:
115144
# noinspection PyTypeChecker
116-
GraphQLDirective("test", locations=[], ast_node=Node()) # type: ignore
145+
GraphQLDirective("Foo", locations=[], ast_node=Node()) # type: ignore
117146
assert str(exc_info.value) == (
118-
"test AST node must be a DirectiveDefinitionNode."
147+
"Foo AST node must be a DirectiveDefinitionNode."
119148
)
120149

121150

122151
def describe_directive_predicates():
123152
def describe_is_directive():
124153
def returns_true_for_directive():
125-
directive = GraphQLDirective("test", [])
154+
directive = GraphQLDirective("Foo", [])
126155
assert is_directive(directive) is True
127156

128157
def returns_false_for_type_class_rather_than_instance():
@@ -140,5 +169,5 @@ def returns_true_for_specified_directive():
140169
assert is_specified_directive(GraphQLSkipDirective) is True
141170

142171
def returns_false_for_unspecified_directive():
143-
directive = GraphQLDirective("test", [])
172+
directive = GraphQLDirective("Foo", [])
144173
assert is_specified_directive(directive) is False

0 commit comments

Comments
 (0)