mirror of
https://github.com/graphql-python/graphene.git
synced 2025-09-21 11:22:33 +03:00
Fix mutation tests with promises
This commit is contained in:
parent
c8adab675a
commit
f1ae6e4cd7
|
@ -1,5 +1,4 @@
|
||||||
import pytest
|
from pytest import mark, raises
|
||||||
from promise import Promise
|
|
||||||
|
|
||||||
from ...types import (
|
from ...types import (
|
||||||
ID,
|
ID,
|
||||||
|
@ -55,15 +54,15 @@ class SaySomethingFixed(ClientIDMutation):
|
||||||
return FixedSaySomething(phrase=str(what))
|
return FixedSaySomething(phrase=str(what))
|
||||||
|
|
||||||
|
|
||||||
class SaySomethingPromise(ClientIDMutation):
|
class SaySomethingAsync(ClientIDMutation):
|
||||||
class Input:
|
class Input:
|
||||||
what = String()
|
what = String()
|
||||||
|
|
||||||
phrase = String()
|
phrase = String()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def mutate_and_get_payload(self, info, what, client_mutation_id=None):
|
async def mutate_and_get_payload(self, info, what, client_mutation_id=None):
|
||||||
return Promise.resolve(SaySomething(phrase=str(what)))
|
return SaySomething(phrase=str(what))
|
||||||
|
|
||||||
|
|
||||||
# MyEdge = MyNode.Connection.Edge
|
# MyEdge = MyNode.Connection.Edge
|
||||||
|
@ -97,7 +96,7 @@ class RootQuery(ObjectType):
|
||||||
class Mutation(ObjectType):
|
class Mutation(ObjectType):
|
||||||
say = SaySomething.Field()
|
say = SaySomething.Field()
|
||||||
say_fixed = SaySomethingFixed.Field()
|
say_fixed = SaySomethingFixed.Field()
|
||||||
say_promise = SaySomethingPromise.Field()
|
say_async = SaySomethingAsync.Field()
|
||||||
other = OtherMutation.Field()
|
other = OtherMutation.Field()
|
||||||
|
|
||||||
|
|
||||||
|
@ -105,7 +104,7 @@ schema = Schema(query=RootQuery, mutation=Mutation)
|
||||||
|
|
||||||
|
|
||||||
def test_no_mutate_and_get_payload():
|
def test_no_mutate_and_get_payload():
|
||||||
with pytest.raises(AssertionError) as excinfo:
|
with raises(AssertionError) as excinfo:
|
||||||
|
|
||||||
class MyMutation(ClientIDMutation):
|
class MyMutation(ClientIDMutation):
|
||||||
pass
|
pass
|
||||||
|
@ -185,12 +184,13 @@ def test_node_query_fixed():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_node_query_promise():
|
@mark.asyncio
|
||||||
executed = schema.execute(
|
async def test_node_query_async():
|
||||||
'mutation a { sayPromise(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
|
executed = await schema.execute_async(
|
||||||
|
'mutation a { sayAsync(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
|
||||||
)
|
)
|
||||||
assert not executed.errors
|
assert not executed.errors
|
||||||
assert executed.data == {"sayPromise": {"phrase": "hello"}}
|
assert executed.data == {"sayAsync": {"phrase": "hello"}}
|
||||||
|
|
||||||
|
|
||||||
def test_edge_query():
|
def test_edge_query():
|
||||||
|
|
|
@ -454,13 +454,13 @@ class Schema:
|
||||||
def lazy(self, _type):
|
def lazy(self, _type):
|
||||||
return lambda: self.get_type(_type)
|
return lambda: self.get_type(_type)
|
||||||
|
|
||||||
async def async_execute(self, *args, **kwargs):
|
async def execute_async(self, *args, **kwargs):
|
||||||
return graphql(self.graphql_schema, *args, **normalize_execute_kwargs(kwargs))
|
kwargs = normalize_execute_kwargs(kwargs)
|
||||||
|
return await graphql(self.graphql_schema, *args, **kwargs)
|
||||||
|
|
||||||
def execute(self, *args, **kwargs):
|
def execute(self, *args, **kwargs):
|
||||||
return graphql_sync(
|
kwargs = normalize_execute_kwargs(kwargs)
|
||||||
self.graphql_schema, *args, **normalize_execute_kwargs(kwargs)
|
return graphql_sync(self.graphql_schema, *args, **kwargs)
|
||||||
)
|
|
||||||
|
|
||||||
def introspect(self):
|
def introspect(self):
|
||||||
introspection = self.execute(introspection_query)
|
introspection = self.execute(introspection_query)
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import pytest
|
from pytest import mark
|
||||||
from graphql.execution.executors.asyncio import AsyncioExecutor
|
|
||||||
|
|
||||||
from graphene.types import ID, Field, ObjectType, Schema
|
from graphene.types import ID, Field, ObjectType, Schema
|
||||||
from graphene.types.scalars import String
|
from graphene.types.scalars import String
|
||||||
|
@ -64,23 +63,19 @@ class Mutation(ObjectType):
|
||||||
schema = Schema(query=RootQuery, mutation=Mutation)
|
schema = Schema(query=RootQuery, mutation=Mutation)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@mark.asyncio
|
||||||
async def test_node_query_promise():
|
async def test_node_query_promise():
|
||||||
executed = await schema.execute(
|
executed = await schema.execute_async(
|
||||||
'mutation a { sayPromise(input: {what:"hello", clientMutationId:"1"}) { phrase } }',
|
'mutation a { sayPromise(input: {what:"hello", clientMutationId:"1"}) { phrase } }',
|
||||||
executor=AsyncioExecutor(),
|
|
||||||
return_promise=True,
|
|
||||||
)
|
)
|
||||||
assert not executed.errors
|
assert not executed.errors
|
||||||
assert executed.data == {"sayPromise": {"phrase": "hello"}}
|
assert executed.data == {"sayPromise": {"phrase": "hello"}}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@mark.asyncio
|
||||||
async def test_edge_query():
|
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 }} } }',
|
'mutation a { other(input: {clientMutationId:"1"}) { clientMutationId, myNodeEdge { cursor node { name }} } }',
|
||||||
executor=AsyncioExecutor(),
|
|
||||||
return_promise=True,
|
|
||||||
)
|
)
|
||||||
assert not executed.errors
|
assert not executed.errors
|
||||||
assert dict(executed.data) == {
|
assert dict(executed.data) == {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user