Skip to content

Commit 6c4e73b

Browse files
committed
Fixed subscription tests
1 parent 4803d6c commit 6c4e73b

File tree

2 files changed

+66
-29
lines changed

2 files changed

+66
-29
lines changed

graphql/execution/tests/test_executor.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ def deeper(self):
112112

113113
schema = GraphQLSchema(query=DataType)
114114

115-
result = execute(schema, ast, Data(), operation_name='Example', variable_values={'size': 100})
115+
result = execute(schema, ast, Data(),
116+
operation_name='Example', variable_values={'size': 100})
116117
assert not result.errors
117118
assert result.data == expected
118119

@@ -178,7 +179,8 @@ def resolver(root_value, *_):
178179
'a': GraphQLField(GraphQLString, resolver=resolver)
179180
})
180181

181-
result = execute(GraphQLSchema(Type), ast, Data(), operation_name='Example')
182+
result = execute(GraphQLSchema(Type), ast,
183+
Data(), operation_name='Example')
182184
assert not result.errors
183185
assert resolver.got_here
184186

@@ -209,7 +211,8 @@ def resolver(source, info, numArg, stringArg):
209211
resolver=resolver),
210212
})
211213

212-
result = execute(GraphQLSchema(Type), doc_ast, None, operation_name='Example')
214+
result = execute(GraphQLSchema(Type), doc_ast,
215+
None, operation_name='Example')
213216
assert not result.errors
214217
assert resolver.got_here
215218

@@ -282,7 +285,8 @@ class Data(object):
282285
Type = GraphQLObjectType('Type', {
283286
'a': GraphQLField(GraphQLString)
284287
})
285-
result = execute(GraphQLSchema(Type), ast, Data(), operation_name='OtherExample')
288+
result = execute(GraphQLSchema(Type), ast, Data(),
289+
operation_name='OtherExample')
286290
assert not result.errors
287291
assert result.data == {'second': 'b'}
288292

@@ -313,7 +317,8 @@ class Data(object):
313317
'a': GraphQLField(GraphQLString)
314318
})
315319
with raises(GraphQLError) as excinfo:
316-
execute(GraphQLSchema(Type), ast, Data(), operation_name="UnknownExample")
320+
execute(GraphQLSchema(Type), ast, Data(),
321+
operation_name="UnknownExample")
317322
assert 'Unknown operation named "UnknownExample".' == str(excinfo.value)
318323

319324

@@ -329,7 +334,8 @@ class Data(object):
329334
})
330335
with raises(GraphQLError) as excinfo:
331336
execute(GraphQLSchema(Type), ast, Data())
332-
assert 'Must provide operation name if query contains multiple operations.' == str(excinfo.value)
337+
assert 'Must provide operation name if query contains multiple operations.' == str(
338+
excinfo.value)
333339

334340

335341
def test_uses_the_query_schema_for_queries():
@@ -374,6 +380,7 @@ class Data(object):
374380

375381

376382
def test_uses_the_subscription_schema_for_subscriptions():
383+
from rx import Observable
377384
doc = 'query Q { a } subscription S { a }'
378385

379386
class Data(object):
@@ -385,9 +392,14 @@ class Data(object):
385392
'a': GraphQLField(GraphQLString)
386393
})
387394
S = GraphQLObjectType('S', {
388-
'a': GraphQLField(GraphQLString)
395+
'a': GraphQLField(GraphQLString, resolver=lambda root, info: Observable.from_(['b']))
389396
})
390-
result = execute(GraphQLSchema(Q, subscription=S), ast, Data(), operation_name='S')
397+
result = execute(GraphQLSchema(Q, subscription=S),
398+
ast, Data(), operation_name='S')
399+
assert isinstance(result, Observable)
400+
l = []
401+
result.subscribe(l.append)
402+
result = l[0]
391403
assert not result.errors
392404
assert result.data == {'a': 'b'}
393405

@@ -437,7 +449,8 @@ def test_does_not_include_arguments_that_were_not_set():
437449
{
438450
'field': GraphQLField(
439451
GraphQLString,
440-
resolver=lambda source, info, **args: args and json.dumps(args, sort_keys=True, separators=(',', ':')),
452+
resolver=lambda source, info, **args: args and json.dumps(
453+
args, sort_keys=True, separators=(',', ':')),
441454
args={
442455
'a': GraphQLArgument(GraphQLBoolean),
443456
'b': GraphQLArgument(GraphQLBoolean),
@@ -501,7 +514,8 @@ def __init__(self, value):
501514
]
502515
}
503516

504-
assert 'Expected value of type "SpecialType" but got: NotSpecial.' in [str(e) for e in result.errors]
517+
assert 'Expected value of type "SpecialType" but got: NotSpecial.' in [
518+
str(e) for e in result.errors]
505519

506520

507521
def test_fails_to_execute_a_query_containing_a_type_definition():
@@ -547,7 +561,8 @@ def resolver(*_):
547561
)
548562

549563
execute(schema, query)
550-
logger.exception.assert_called_with("An error occurred while resolving field Query.foo")
564+
logger.exception.assert_called_with(
565+
"An error occurred while resolving field Query.foo")
551566

552567

553568
def test_middleware():
@@ -576,7 +591,8 @@ def reversed_middleware(next, *args, **kwargs):
576591
return p.then(lambda x: x[::-1])
577592

578593
middlewares = MiddlewareManager(reversed_middleware)
579-
result = execute(GraphQLSchema(Type), doc_ast, Data(), middleware=middlewares)
594+
result = execute(GraphQLSchema(Type), doc_ast,
595+
Data(), middleware=middlewares)
580596
assert result.data == {'ok': 'ko', 'not_ok': 'ko_ton'}
581597

582598

@@ -607,7 +623,8 @@ def resolve(self, next, *args, **kwargs):
607623
return p.then(lambda x: x[::-1])
608624

609625
middlewares = MiddlewareManager(MyMiddleware())
610-
result = execute(GraphQLSchema(Type), doc_ast, Data(), middleware=middlewares)
626+
result = execute(GraphQLSchema(Type), doc_ast,
627+
Data(), middleware=middlewares)
611628
assert result.data == {'ok': 'ko', 'not_ok': 'ko_ton'}
612629

613630

@@ -640,9 +657,14 @@ class MyEmptyMiddleware(object):
640657
def resolve(self, next, *args, **kwargs):
641658
return next(*args, **kwargs)
642659

643-
middlewares_with_promise = MiddlewareManager(MyPromiseMiddleware(), wrap_in_promise=False)
644-
middlewares_without_promise = MiddlewareManager(MyEmptyMiddleware(), wrap_in_promise=False)
645-
646-
result1 = execute(GraphQLSchema(Type), doc_ast, Data(), middleware=middlewares_with_promise)
647-
result2 = execute(GraphQLSchema(Type), doc_ast, Data(), middleware=middlewares_without_promise)
648-
assert result1.data == result2.data and result1.data == {'ok': 'ok', 'not_ok': 'not_ok'}
660+
middlewares_with_promise = MiddlewareManager(
661+
MyPromiseMiddleware(), wrap_in_promise=False)
662+
middlewares_without_promise = MiddlewareManager(
663+
MyEmptyMiddleware(), wrap_in_promise=False)
664+
665+
result1 = execute(GraphQLSchema(Type), doc_ast, Data(),
666+
middleware=middlewares_with_promise)
667+
result2 = execute(GraphQLSchema(Type), doc_ast, Data(),
668+
middleware=middlewares_without_promise)
669+
assert result1.data == result2.data and result1.data == {
670+
'ok': 'ok', 'not_ok': 'not_ok'}

graphql/type/tests/test_enum_type.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import OrderedDict
22

3+
from rx import Observable
34
from pytest import raises
45

56
from graphql import graphql
@@ -35,15 +36,17 @@ def get_first(args, *keys):
3536
'fromInt': GraphQLArgument(GraphQLInt),
3637
'fromString': GraphQLArgument(GraphQLString)
3738
},
38-
resolver=lambda value, info, **args: get_first(args, 'fromInt', 'fromString', 'fromEnum')
39+
resolver=lambda value, info, **args: get_first(
40+
args, 'fromInt', 'fromString', 'fromEnum')
3941
),
4042
'colorInt': GraphQLField(
4143
type=GraphQLInt,
4244
args={
4345
'fromEnum': GraphQLArgument(ColorType),
4446
'fromInt': GraphQLArgument(GraphQLInt),
4547
},
46-
resolver=lambda value, info, **args: get_first(args, 'fromInt', 'fromEnum')
48+
resolver=lambda value, info, **args: get_first(
49+
args, 'fromInt', 'fromEnum')
4750
)
4851
}
4952
)
@@ -69,12 +72,14 @@ def get_first(args, *keys):
6972
args={
7073
'color': GraphQLArgument(ColorType)
7174
},
72-
resolver=lambda value, info, **args: args.get('color')
75+
resolver=lambda value, info, **args: Observable.from_(
76+
[args.get('color')])
7377
)
7478
}
7579
)
7680

77-
Schema = GraphQLSchema(query=QueryType, mutation=MutationType, subscription=SubscriptionType)
81+
Schema = GraphQLSchema(
82+
query=QueryType, mutation=MutationType, subscription=SubscriptionType)
7883

7984

8085
def test_accepts_enum_literals_as_input():
@@ -130,13 +135,15 @@ def test_does_not_accept_enum_literal_in_place_of_int():
130135

131136

132137
def test_accepts_json_string_as_enum_variable():
133-
result = graphql(Schema, 'query test($color: Color!) { colorEnum(fromEnum: $color) }', variable_values={'color': 'BLUE'})
138+
result = graphql(Schema, 'query test($color: Color!) { colorEnum(fromEnum: $color) }', variable_values={
139+
'color': 'BLUE'})
134140
assert not result.errors
135141
assert result.data == {'colorEnum': 'BLUE'}
136142

137143

138144
def test_accepts_enum_literals_as_input_arguments_to_mutations():
139-
result = graphql(Schema, 'mutation x($color: Color!) { favoriteEnum(color: $color) }', variable_values={'color': 'GREEN'})
145+
result = graphql(Schema, 'mutation x($color: Color!) { favoriteEnum(color: $color) }', variable_values={
146+
'color': 'GREEN'})
140147
assert not result.errors
141148
assert result.data == {'favoriteEnum': 'GREEN'}
142149

@@ -145,31 +152,39 @@ def test_accepts_enum_literals_as_input_arguments_to_subscriptions():
145152
result = graphql(
146153
Schema, 'subscription x($color: Color!) { subscribeToEnum(color: $color) }', variable_values={
147154
'color': 'GREEN'})
155+
assert isinstance(result, Observable)
156+
l = []
157+
result.subscribe(l.append)
158+
result = l[0]
148159
assert not result.errors
149160
assert result.data == {'subscribeToEnum': 'GREEN'}
150161

151162

152163
def test_does_not_accept_internal_value_as_enum_variable():
153-
result = graphql(Schema, 'query test($color: Color!) { colorEnum(fromEnum: $color) }', variable_values={'color': 2})
164+
result = graphql(
165+
Schema, 'query test($color: Color!) { colorEnum(fromEnum: $color) }', variable_values={'color': 2})
154166
assert not result.data
155167
assert result.errors[0].message == 'Variable "$color" got invalid value 2.\n' \
156168
'Expected type "Color", found 2.'
157169

158170

159171
def test_does_not_accept_string_variables_as_enum_input():
160-
result = graphql(Schema, 'query test($color: String!) { colorEnum(fromEnum: $color) }', variable_values={'color': 'BLUE'})
172+
result = graphql(Schema, 'query test($color: String!) { colorEnum(fromEnum: $color) }', variable_values={
173+
'color': 'BLUE'})
161174
assert not result.data
162175
assert result.errors[0].message == 'Variable "color" of type "String!" used in position expecting type "Color".'
163176

164177

165178
def test_does_not_accept_internal_value_as_enum_input():
166-
result = graphql(Schema, 'query test($color: Int!) { colorEnum(fromEnum: $color) }', variable_values={'color': 2})
179+
result = graphql(
180+
Schema, 'query test($color: Int!) { colorEnum(fromEnum: $color) }', variable_values={'color': 2})
167181
assert not result.data
168182
assert result.errors[0].message == 'Variable "color" of type "Int!" used in position expecting type "Color".'
169183

170184

171185
def test_enum_value_may_have_an_internal_value_of_0():
172-
result = graphql(Schema, '{ colorEnum(fromEnum: RED) colorInt(fromEnum: RED) }')
186+
result = graphql(
187+
Schema, '{ colorEnum(fromEnum: RED) colorInt(fromEnum: RED) }')
173188
assert not result.errors
174189
assert result.data == {'colorEnum': 'RED', 'colorInt': 0}
175190

0 commit comments

Comments
 (0)