Fix mutation tests with promises

This commit is contained in:
Christoph Zwerschke 2019-06-30 22:27:33 +02:00 committed by Mel van Londen
parent c8adab675a
commit f1ae6e4cd7
3 changed files with 21 additions and 26 deletions

View File

@ -1,5 +1,4 @@
import pytest
from promise import Promise
from pytest import mark, raises
from ...types import (
ID,
@ -55,15 +54,15 @@ class SaySomethingFixed(ClientIDMutation):
return FixedSaySomething(phrase=str(what))
class SaySomethingPromise(ClientIDMutation):
class SaySomethingAsync(ClientIDMutation):
class Input:
what = String()
phrase = String()
@staticmethod
def mutate_and_get_payload(self, info, what, client_mutation_id=None):
return Promise.resolve(SaySomething(phrase=str(what)))
async def mutate_and_get_payload(self, info, what, client_mutation_id=None):
return SaySomething(phrase=str(what))
# MyEdge = MyNode.Connection.Edge
@ -97,7 +96,7 @@ class RootQuery(ObjectType):
class Mutation(ObjectType):
say = SaySomething.Field()
say_fixed = SaySomethingFixed.Field()
say_promise = SaySomethingPromise.Field()
say_async = SaySomethingAsync.Field()
other = OtherMutation.Field()
@ -105,7 +104,7 @@ schema = Schema(query=RootQuery, mutation=Mutation)
def test_no_mutate_and_get_payload():
with pytest.raises(AssertionError) as excinfo:
with raises(AssertionError) as excinfo:
class MyMutation(ClientIDMutation):
pass
@ -185,12 +184,13 @@ def test_node_query_fixed():
)
def test_node_query_promise():
executed = schema.execute(
'mutation a { sayPromise(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
@mark.asyncio
async def test_node_query_async():
executed = await schema.execute_async(
'mutation a { sayAsync(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
)
assert not executed.errors
assert executed.data == {"sayPromise": {"phrase": "hello"}}
assert executed.data == {"sayAsync": {"phrase": "hello"}}
def test_edge_query():

View File

@ -454,13 +454,13 @@ class Schema:
def lazy(self, _type):
return lambda: self.get_type(_type)
async def async_execute(self, *args, **kwargs):
return graphql(self.graphql_schema, *args, **normalize_execute_kwargs(kwargs))
async def execute_async(self, *args, **kwargs):
kwargs = normalize_execute_kwargs(kwargs)
return await graphql(self.graphql_schema, *args, **kwargs)
def execute(self, *args, **kwargs):
return graphql_sync(
self.graphql_schema, *args, **normalize_execute_kwargs(kwargs)
)
kwargs = normalize_execute_kwargs(kwargs)
return graphql_sync(self.graphql_schema, *args, **kwargs)
def introspect(self):
introspection = self.execute(introspection_query)

View File

@ -1,5 +1,4 @@
import pytest
from graphql.execution.executors.asyncio import AsyncioExecutor
from pytest import mark
from graphene.types import ID, Field, ObjectType, Schema
from graphene.types.scalars import String
@ -64,23 +63,19 @@ class Mutation(ObjectType):
schema = Schema(query=RootQuery, mutation=Mutation)
@pytest.mark.asyncio
@mark.asyncio
async def test_node_query_promise():
executed = await schema.execute(
executed = await schema.execute_async(
'mutation a { sayPromise(input: {what:"hello", clientMutationId:"1"}) { phrase } }',
executor=AsyncioExecutor(),
return_promise=True,
)
assert not executed.errors
assert executed.data == {"sayPromise": {"phrase": "hello"}}
@pytest.mark.asyncio
@mark.asyncio
async def test_edge_query():
executed = await schema.execute(
executed = await schema.execute_async(
'mutation a { other(input: {clientMutationId:"1"}) { clientMutationId, myNodeEdge { cursor node { name }} } }',
executor=AsyncioExecutor(),
return_promise=True,
)
assert not executed.errors
assert dict(executed.data) == {