graphene/tests/core/test_mutations.py

67 lines
1.4 KiB
Python
Raw Normal View History

2015-10-26 07:39:54 +03:00
import graphene
from py.test import raises
from graphene.core.schema import Schema
my_id = 0
class Query(graphene.ObjectType):
base = graphene.StringField()
class ChangeNumber(graphene.Mutation):
'''Result mutation'''
class Input:
2015-10-26 08:30:35 +03:00
to = graphene.IntField()
2015-10-26 07:39:54 +03:00
result = graphene.StringField()
@classmethod
def mutate(cls, instance, args, info):
global my_id
2015-10-26 08:30:35 +03:00
my_id = args.get('to', my_id + 1)
2015-10-26 07:39:54 +03:00
return ChangeNumber(result=my_id)
class MyResultMutation(graphene.ObjectType):
change_number = graphene.Field(ChangeNumber)
schema = Schema(query=Query, mutation=MyResultMutation)
2015-10-26 08:30:35 +03:00
def test_mutation_input():
assert ChangeNumber.input_type
assert ChangeNumber.input_type._meta.type_name == 'ChangeNumberInput'
assert list(ChangeNumber.input_type._meta.fields_map.keys()) == ['to']
def test_execute_mutations():
2015-10-26 07:39:54 +03:00
query = '''
mutation M{
first: changeNumber {
result
},
second: changeNumber {
result
}
2015-10-26 08:30:35 +03:00
third: changeNumber(to: 5) {
result
}
2015-10-26 07:39:54 +03:00
}
'''
expected = {
'first': {
'result': '1',
},
'second': {
'result': '2',
2015-10-26 08:30:35 +03:00
},
'third': {
'result': '5',
2015-10-26 07:39:54 +03:00
}
}
result = schema.execute(query, root=object())
assert not result.errors
assert result.data == expected