Skip to content

Commit

Permalink
Improved ClientIDMutation tests for thenables
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Jun 24, 2017
1 parent 645bfdd commit 6c2b940
Showing 1 changed file with 52 additions and 15 deletions.
67 changes: 52 additions & 15 deletions graphene/relay/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
from ...types.scalars import String
from ..mutation import ClientIDMutation
from ..node import Node
from promise import Promise


class SharedFields(AbstractType):
shared = String()


class MyNode(ObjectType):

class Meta:
interfaces = (Node, )

name = String()


class SaySomething(ClientIDMutation):

class Input:
what = String()

phrase = String()

@staticmethod
Expand All @@ -31,8 +31,19 @@ def mutate_and_get_payload(args, context, info):
return SaySomething(phrase=str(what))


class OtherMutation(ClientIDMutation):
class SaySomethingPromise(ClientIDMutation):
class Input:
what = String()

phrase = String()

@staticmethod
def mutate_and_get_payload(args, context, info):
what = args.get('what')
return Promise.resolve(SaySomething(phrase=str(what)))


class OtherMutation(ClientIDMutation):
class Input(SharedFields):
additional_field = String()

Expand All @@ -44,9 +55,9 @@ def mutate_and_get_payload(cls, args, context, info):
shared = args.get('shared', '')
additionalField = args.get('additionalField', '')
edge_type = MyNode.Connection.Edge
return OtherMutation(name=shared + additionalField,
my_node_edge=edge_type(
cursor='1', node=MyNode(name='name')))
return OtherMutation(
name=shared + additionalField,
my_node_edge=edge_type(cursor='1', node=MyNode(name='name')))


class RootQuery(ObjectType):
Expand All @@ -55,13 +66,16 @@ class RootQuery(ObjectType):

class Mutation(ObjectType):
say = SaySomething.Field()
say_promise = SaySomethingPromise.Field()
other = OtherMutation.Field()


schema = Schema(query=RootQuery, mutation=Mutation)


def test_no_mutate_and_get_payload():
with pytest.raises(AssertionError) as excinfo:

class MyMutation(ClientIDMutation):
pass

Expand Down Expand Up @@ -97,7 +111,9 @@ def test_mutation_input():

def test_subclassed_mutation():
fields = OtherMutation._meta.fields
assert list(fields.keys()) == ['name', 'my_node_edge', 'client_mutation_id']
assert list(fields.keys()) == [
'name', 'my_node_edge', 'client_mutation_id'
]
assert isinstance(fields['name'], Field)
field = OtherMutation.Field()
assert field.type == OtherMutation
Expand All @@ -111,7 +127,9 @@ def test_subclassed_mutation_input():
Input = OtherMutation.Input
assert issubclass(Input, InputObjectType)
fields = Input._meta.fields
assert list(fields.keys()) == ['shared', 'additional_field', 'client_mutation_id']
assert list(fields.keys()) == [
'shared', 'additional_field', 'client_mutation_id'
]
assert isinstance(fields['shared'], InputField)
assert fields['shared'].type == String
assert isinstance(fields['additional_field'], InputField)
Expand All @@ -120,16 +138,35 @@ def test_subclassed_mutation_input():
assert fields['client_mutation_id'].type == String


# def test_node_query():
# executed = schema.execute(
# 'mutation a { say(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
# )
# assert not executed.errors
# assert executed.data == {'say': {'phrase': 'hello'}}
def test_node_query():
executed = schema.execute(
'mutation a { say(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
)
assert not executed.errors
assert executed.data == {'say': {'phrase': 'hello'}}


def test_node_query():
executed = schema.execute(
'mutation a { sayPromise(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
)
assert not executed.errors
assert executed.data == {'sayPromise': {'phrase': 'hello'}}


def test_edge_query():
executed = schema.execute(
'mutation a { other(input: {clientMutationId:"1"}) { clientMutationId, myNodeEdge { cursor node { name }} } }'
)
assert not executed.errors
assert dict(executed.data) == {'other': {'clientMutationId': '1', 'myNodeEdge': {'cursor': '1', 'node': {'name': 'name'}}}}
assert dict(executed.data) == {
'other': {
'clientMutationId': '1',
'myNodeEdge': {
'cursor': '1',
'node': {
'name': 'name'
}
}
}
}

0 comments on commit 6c2b940

Please sign in to comment.