From 47ecdb37cefaa0c31f582d0a65d96e51bb1961b5 Mon Sep 17 00:00:00 2001 From: Christoph Zwerschke Date: Thu, 3 Nov 2022 20:59:58 +0100 Subject: [PATCH] subscribe-test: extract subscribe_with_bad_fn function Replicates graphql/graphql-js@2deb27214ee4b74c09b19c55d934d72b0a7fd355 --- tests/execution/test_subscribe.py | 61 ++++++++++++++----------------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/tests/execution/test_subscribe.py b/tests/execution/test_subscribe.py index faf10f64..24655915 100644 --- a/tests/execution/test_subscribe.py +++ b/tests/execution/test_subscribe.py @@ -3,7 +3,12 @@ from pytest import mark, raises -from graphql.execution import MapAsyncIterator, create_source_event_stream, subscribe +from graphql.execution import ( + ExecutionResult, + MapAsyncIterator, + create_source_event_stream, + subscribe, +) from graphql.language import parse from graphql.pyutils import SimplePubSub from graphql.type import ( @@ -132,6 +137,22 @@ def transform(new_email): DummyQueryType = GraphQLObjectType("Query", {"dummy": GraphQLField(GraphQLString)}) +async def subscribe_with_bad_fn(subscribe_fn: Callable) -> ExecutionResult: + schema = GraphQLSchema( + query=DummyQueryType, + subscription=GraphQLObjectType( + "Subscription", + {"foo": GraphQLField(GraphQLString, subscribe=subscribe_fn)}, + ), + ) + document = parse("subscription { foo }") + result = await subscribe(schema, document) + + assert isinstance(result, ExecutionResult) + assert await create_source_event_stream(schema, document) == result + return result + + # Check all error cases when initializing the subscription. def describe_subscription_initialization_phase(): @mark.asyncio @@ -333,22 +354,8 @@ async def should_pass_through_unexpected_errors_thrown_in_subscribe(): @mark.asyncio @mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning") async def throws_an_error_if_subscribe_does_not_return_an_iterator(): - schema = GraphQLSchema( - query=DummyQueryType, - subscription=GraphQLObjectType( - "Subscription", - { - "foo": GraphQLField( - GraphQLString, subscribe=lambda _obj, _info: "test" - ) - }, - ), - ) - - document = parse("subscription { foo }") - with raises(TypeError) as exc_info: - await subscribe(schema, document) + await subscribe_with_bad_fn(lambda _obj, _info: "test") assert str(exc_info.value) == ( "Subscription field must return AsyncIterable. Received: 'test'." @@ -356,20 +363,6 @@ async def throws_an_error_if_subscribe_does_not_return_an_iterator(): @mark.asyncio async def resolves_to_an_error_for_subscription_resolver_errors(): - async def subscribe_with_fn(subscribe_fn: Callable): - schema = GraphQLSchema( - query=DummyQueryType, - subscription=GraphQLObjectType( - "Subscription", - {"foo": GraphQLField(GraphQLString, subscribe=subscribe_fn)}, - ), - ) - document = parse("subscription { foo }") - result = await subscribe(schema, document) - - assert await create_source_event_stream(schema, document) == result - return result - expected_result = ( None, [ @@ -385,25 +378,25 @@ async def subscribe_with_fn(subscribe_fn: Callable): def return_error(_obj, _info): return TypeError("test error") - assert await subscribe_with_fn(return_error) == expected_result + assert await subscribe_with_bad_fn(return_error) == expected_result # Throwing an error def throw_error(*_args): raise TypeError("test error") - assert await subscribe_with_fn(throw_error) == expected_result + assert await subscribe_with_bad_fn(throw_error) == expected_result # Resolving to an error async def resolve_error(*_args): return TypeError("test error") - assert await subscribe_with_fn(resolve_error) == expected_result + assert await subscribe_with_bad_fn(resolve_error) == expected_result # Rejecting with an error async def reject_error(*_args): return TypeError("test error") - assert await subscribe_with_fn(reject_error) == expected_result + assert await subscribe_with_bad_fn(reject_error) == expected_result @mark.asyncio async def resolves_to_an_error_if_variables_were_wrong_type():