Skip to content

Commit 36bb3ee

Browse files
committed
Add tests for directives
1 parent 0ecb1eb commit 36bb3ee

File tree

3 files changed

+140
-3
lines changed

3 files changed

+140
-3
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

1414
The current version 1.0.0rc2 of GraphQL-core-next is up-to-date with GraphQL.js
1515
version 14.0.0rc2. All parts of the API are covered by an extensive test
16-
suite of currently 1569 unit tests.
16+
suite of currently 1585 unit tests.
1717

1818

1919
## Documentation

graphql/type/directives.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, name: str,
3434
elif not isinstance(name, str):
3535
raise TypeError('The directive name must be a string.')
3636
if not isinstance(locations, (list, tuple)):
37-
raise TypeError('{name} locations must be a list/tuple.')
37+
raise TypeError(f'{name} locations must be a list/tuple.')
3838
if not all(isinstance(value, DirectiveLocation)
3939
for value in locations):
4040
try:
@@ -60,7 +60,7 @@ def __init__(self, name: str,
6060
else GraphQLArgument(cast(GraphQLInputType, value))
6161
for name, value in args.items()}
6262
if description is not None and not isinstance(description, str):
63-
raise TypeError('f{name} description must be a string.')
63+
raise TypeError(f'{name} description must be a string.')
6464
if ast_node and not isinstance(ast_node, ast.DirectiveDefinitionNode):
6565
raise TypeError(
6666
f'{name} AST node must be a DirectiveDefinitionNode.')

tests/type/test_directives.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
from pytest import raises
2+
3+
from graphql.language import DirectiveLocation, DirectiveDefinitionNode, Node
4+
from graphql.type import (
5+
GraphQLArgument, GraphQLDirective, GraphQLString, GraphQLSkipDirective,
6+
is_directive, is_specified_directive)
7+
8+
9+
def describe_graphql_directive():
10+
11+
def can_create_instance():
12+
arg = GraphQLArgument(GraphQLString, description='arg description')
13+
node = DirectiveDefinitionNode()
14+
locations = [DirectiveLocation.SCHEMA, DirectiveLocation.OBJECT]
15+
directive = GraphQLDirective(
16+
name='test',
17+
locations=[DirectiveLocation.SCHEMA, DirectiveLocation.OBJECT],
18+
args={'arg': arg},
19+
description='test description',
20+
ast_node=node)
21+
assert directive.name == 'test'
22+
assert directive.locations == locations
23+
assert directive.args == {'arg': arg}
24+
assert directive.description == 'test description'
25+
assert directive.ast_node is node
26+
27+
def has_str():
28+
directive = GraphQLDirective('test', [])
29+
assert str(directive) == '@test'
30+
31+
def has_repr():
32+
directive = GraphQLDirective('test', [])
33+
assert repr(directive) == '<GraphQLDirective(@test)>'
34+
35+
def accepts_strings_as_locations():
36+
# noinspection PyTypeChecker
37+
directive = GraphQLDirective(
38+
name='test', locations=['SCHEMA', 'OBJECT']) # type: ignore
39+
assert directive.locations == [
40+
DirectiveLocation.SCHEMA, DirectiveLocation.OBJECT]
41+
42+
def accepts_input_types_as_arguments():
43+
# noinspection PyTypeChecker
44+
directive = GraphQLDirective(
45+
name='test', locations=[],
46+
args={'arg': GraphQLString}) # type: ignore
47+
arg = directive.args['arg']
48+
assert isinstance(arg, GraphQLArgument)
49+
assert arg.type is GraphQLString
50+
51+
def does_not_accept_a_bad_name():
52+
with raises(TypeError) as exc_info:
53+
# noinspection PyTypeChecker
54+
GraphQLDirective(None, locations=[]) # type: ignore
55+
assert str(exc_info.value) == 'Directive must be named.'
56+
with raises(TypeError) as exc_info:
57+
# noinspection PyTypeChecker
58+
GraphQLDirective({'bad': True}, locations=[]) # type: ignore
59+
assert str(exc_info.value) == 'The directive name must be a string.'
60+
61+
def does_not_accept_bad_locations():
62+
with raises(TypeError) as exc_info:
63+
# noinspection PyTypeChecker
64+
GraphQLDirective('test', locations='bad') # type: ignore
65+
assert str(exc_info.value) == 'test locations must be a list/tuple.'
66+
with raises(TypeError) as exc_info:
67+
# noinspection PyTypeChecker
68+
GraphQLDirective('test', locations=['bad']) # type: ignore
69+
assert str(exc_info.value) == (
70+
'test locations must be DirectiveLocation objects.')
71+
72+
def does_not_accept_bad_args():
73+
with raises(TypeError) as exc_info:
74+
# noinspection PyTypeChecker
75+
GraphQLDirective(
76+
'test', locations=[], args=['arg']) # type: ignore
77+
assert str(exc_info.value) == (
78+
'test args must be a dict with argument names as keys.')
79+
with raises(TypeError) as exc_info:
80+
# noinspection PyTypeChecker
81+
GraphQLDirective(
82+
'test', locations=[],
83+
args={1: GraphQLArgument(GraphQLString)}) # type: ignore
84+
assert str(exc_info.value) == (
85+
'test args must be a dict with argument names as keys.')
86+
with raises(TypeError) as exc_info:
87+
# noinspection PyTypeChecker
88+
GraphQLDirective(
89+
'test', locations=[],
90+
args={'arg': GraphQLDirective('test', [])}) # type: ignore
91+
assert str(exc_info.value) == (
92+
'test args must be GraphQLArgument or input type objects.')
93+
94+
def does_not_accept_a_bad_description():
95+
with raises(TypeError) as exc_info:
96+
# noinspection PyTypeChecker
97+
GraphQLDirective(
98+
'test', locations=[],
99+
description={'bad': True}) # type: ignore
100+
assert str(exc_info.value) == 'test description must be a string.'
101+
102+
def does_not_accept_a_bad_ast_node():
103+
with raises(TypeError) as exc_info:
104+
# noinspection PyTypeChecker
105+
GraphQLDirective(
106+
'test', locations=[],
107+
ast_node=Node()) # type: ignore
108+
assert str(exc_info.value) == (
109+
'test AST node must be a DirectiveDefinitionNode.')
110+
111+
112+
def describe_directive_predicates():
113+
114+
def describe_is_directive():
115+
116+
def returns_true_for_directive():
117+
directive = GraphQLDirective('test', [])
118+
assert is_directive(directive) is True
119+
120+
def returns_false_for_type_class_rather_than_instance():
121+
assert is_directive(GraphQLDirective) is False
122+
123+
def returns_false_for_other_instances():
124+
assert is_directive(GraphQLString) is False
125+
126+
def returns_false_for_random_garbage():
127+
assert is_directive(None) is False
128+
assert is_directive({'what': 'is this'}) is False
129+
130+
def describe_is_specified_directive():
131+
132+
def returns_true_for_specified_directive():
133+
assert is_specified_directive(GraphQLSkipDirective) is True
134+
135+
def returns_false_for_unspecified_directive():
136+
directive = GraphQLDirective('test', [])
137+
assert is_specified_directive(directive) is False

0 commit comments

Comments
 (0)