mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-06-13 10:03:12 +03:00
Add tests
This commit is contained in:
parent
bc7d96b2be
commit
c5dd913f53
|
@ -1,5 +1,6 @@
|
|||
from django.db import models
|
||||
from graphene import Field
|
||||
|
||||
from graphene import Field, Schema, String, Mutation, ObjectType, AbstractType
|
||||
from graphene.types.inputobjecttype import InputObjectType
|
||||
from py.test import raises
|
||||
from rest_framework import serializers
|
||||
|
@ -68,3 +69,47 @@ def test_nested_model():
|
|||
model_input_type = model_input._type.of_type
|
||||
assert issubclass(model_input_type, InputObjectType)
|
||||
assert 'cool_name' in model_input_type._meta.fields
|
||||
|
||||
|
||||
def test_custom_serializer(capfd):
|
||||
|
||||
class MyBasicSerializer(serializers.Serializer):
|
||||
whatever = serializers.CharField(required=False)
|
||||
|
||||
def create(self, data):
|
||||
return {'user': self.context['request']['user']}
|
||||
|
||||
class MyBasicGrapheneType(ObjectType):
|
||||
text = String()
|
||||
|
||||
class MyAwesomeMutation(SerializerMutation):
|
||||
|
||||
class Meta:
|
||||
serializer_class = MyBasicSerializer
|
||||
|
||||
user = String()
|
||||
|
||||
@classmethod
|
||||
def get_serializer(cls, instance, args, request, info):
|
||||
input = args.get('input')
|
||||
|
||||
return cls._meta.serializer_class(data=dict(input),
|
||||
context={'request': request})
|
||||
|
||||
class MyAbstractMutation(AbstractType, ObjectType):
|
||||
myAwesomeMutation = MyAwesomeMutation.Field()
|
||||
|
||||
schema = Schema(query=MyBasicGrapheneType, mutation=MyAbstractMutation)
|
||||
mutation = '''
|
||||
mutation MutationTest($input: MyBasicSerializerInput!) {
|
||||
myAwesomeMutation(input: $input) {
|
||||
user
|
||||
}
|
||||
}
|
||||
'''
|
||||
|
||||
result = schema.execute(mutation, variable_values={'input': {}},
|
||||
context_value={'user': 1})
|
||||
assert result.data['myAwesomeMutation']['user'] == '1'
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user