4
4
from graphql .type import (
5
5
GraphQLArgument ,
6
6
GraphQLDirective ,
7
+ GraphQLInt ,
7
8
GraphQLString ,
8
9
GraphQLSkipDirective ,
9
10
is_directive ,
10
11
is_specified_directive ,
11
12
)
12
13
13
14
14
- def describe_graphql_directive ():
15
+ def describe_type_system_directive ():
15
16
def can_create_instance ():
16
17
arg = GraphQLArgument (GraphQLString , description = "arg description" )
17
18
node = DirectiveDefinitionNode ()
@@ -29,100 +30,128 @@ def can_create_instance():
29
30
assert directive .description == "test description"
30
31
assert directive .ast_node is node
31
32
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 )
35
36
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
39
61
40
- def accepts_strings_as_locations ():
62
+ def directive_accepts_strings_as_locations ():
41
63
# noinspection PyTypeChecker
42
64
directive = GraphQLDirective (
43
- name = "test " , locations = ["SCHEMA" , "OBJECT" ]
65
+ name = "Foo " , locations = ["SCHEMA" , "OBJECT" ]
44
66
) # type: ignore
45
67
assert directive .locations == [
46
68
DirectiveLocation .SCHEMA ,
47
69
DirectiveLocation .OBJECT ,
48
70
]
49
71
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)>"
58
79
59
- def does_not_accept_a_bad_name ():
80
+ def reject_an_unnamed_directivce ():
60
81
with raises (TypeError ) as exc_info :
61
82
# noinspection PyTypeChecker
62
83
GraphQLDirective (None , locations = []) # type: ignore
63
84
assert str (exc_info .value ) == "Directive must be named."
85
+
86
+ def reject_directive_with_incorrectly_typed_name ():
64
87
with raises (TypeError ) as exc_info :
65
88
# noinspection PyTypeChecker
66
89
GraphQLDirective ({"bad" : True }, locations = []) # type: ignore
67
90
assert str (exc_info .value ) == "The directive name must be a string."
68
91
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 ():
74
93
with raises (TypeError ) as exc_info :
75
94
# noinspection PyTypeChecker
76
- GraphQLDirective ("test " , locations = ["bad " ]) # type: ignore
95
+ GraphQLDirective ("Foo " , locations = [], args = [ "arg " ]) # type: ignore
77
96
assert str (exc_info .value ) == (
78
- "test locations must be DirectiveLocation objects ."
97
+ "Foo args must be a dict with argument names as keys ."
79
98
)
80
-
81
- def does_not_accept_bad_args ():
82
99
with raises (TypeError ) as exc_info :
83
100
# noinspection PyTypeChecker
84
- GraphQLDirective ("test" , locations = [], args = ["arg" ]) # type: ignore
101
+ GraphQLDirective (
102
+ "Foo" , locations = [], args = {1 : GraphQLArgument (GraphQLString )}
103
+ ) # type: ignore
85
104
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."
87
106
)
88
107
with raises (TypeError ) as exc_info :
89
108
# noinspection PyTypeChecker
90
109
GraphQLDirective (
91
- "test " , locations = [], args = {1 : GraphQLArgument ( GraphQLString )}
110
+ "Foo " , locations = [], args = {"arg" : GraphQLDirective ( "Bar" , [] )}
92
111
) # type: ignore
93
112
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 ."
95
114
)
115
+
116
+ def reject_directive_with_undefined_locations ():
96
117
with raises (TypeError ) as exc_info :
97
118
# 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
101
130
assert str (exc_info .value ) == (
102
- "test args must be GraphQLArgument or input type objects."
131
+ "Foo locations must be DirectiveLocation objects."
103
132
)
104
133
105
- def does_not_accept_a_bad_description ():
134
+ def reject_directive_with_incorrectly_typed_description ():
106
135
with raises (TypeError ) as exc_info :
107
136
# noinspection PyTypeChecker
108
137
GraphQLDirective (
109
- "test " , locations = [], description = {"bad" : True }
138
+ "Foo " , locations = [], description = {"bad" : True }
110
139
) # 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."
112
141
113
- def does_not_accept_a_bad_ast_node ():
142
+ def reject_directive_with_incorrectly_typed_ast_node ():
114
143
with raises (TypeError ) as exc_info :
115
144
# noinspection PyTypeChecker
116
- GraphQLDirective ("test " , locations = [], ast_node = Node ()) # type: ignore
145
+ GraphQLDirective ("Foo " , locations = [], ast_node = Node ()) # type: ignore
117
146
assert str (exc_info .value ) == (
118
- "test AST node must be a DirectiveDefinitionNode."
147
+ "Foo AST node must be a DirectiveDefinitionNode."
119
148
)
120
149
121
150
122
151
def describe_directive_predicates ():
123
152
def describe_is_directive ():
124
153
def returns_true_for_directive ():
125
- directive = GraphQLDirective ("test " , [])
154
+ directive = GraphQLDirective ("Foo " , [])
126
155
assert is_directive (directive ) is True
127
156
128
157
def returns_false_for_type_class_rather_than_instance ():
@@ -140,5 +169,5 @@ def returns_true_for_specified_directive():
140
169
assert is_specified_directive (GraphQLSkipDirective ) is True
141
170
142
171
def returns_false_for_unspecified_directive ():
143
- directive = GraphQLDirective ("test " , [])
172
+ directive = GraphQLDirective ("Foo " , [])
144
173
assert is_specified_directive (directive ) is False
0 commit comments