graphene-django/graphene_django/rest_framework/tests/test_mutation.py

71 lines
1.9 KiB
Python
Raw Normal View History

2017-07-18 22:01:43 +03:00
from django.db import models
from graphene import Field
from graphene.types.inputobjecttype import InputObjectType
2017-05-31 01:30:03 +03:00
from py.test import raises
from rest_framework import serializers
2017-07-18 22:01:43 +03:00
from ...types import DjangoObjectType
2017-05-31 01:30:03 +03:00
from ..mutation import SerializerMutation
2017-07-18 22:01:43 +03:00
class MyFakeModel(models.Model):
cool_name = models.CharField(max_length=50)
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyFakeModel
fields = '__all__'
2017-05-31 01:30:03 +03:00
class MySerializer(serializers.Serializer):
text = serializers.CharField()
2017-07-18 22:01:43 +03:00
model = MyModelSerializer()
2017-05-31 01:30:03 +03:00
def test_needs_serializer_class():
with raises(Exception) as exc:
class MyMutation(SerializerMutation):
pass
assert exc.value.args[0] == 'Missing serializer_class'
def test_has_fields():
class MyMutation(SerializerMutation):
class Meta:
serializer_class = MySerializer
assert 'text' in MyMutation._meta.fields
2017-07-18 22:01:43 +03:00
assert 'model' in MyMutation._meta.fields
2017-05-31 01:30:03 +03:00
assert 'errors' in MyMutation._meta.fields
def test_has_input_fields():
class MyMutation(SerializerMutation):
class Meta:
serializer_class = MySerializer
assert 'text' in MyMutation.Input._meta.fields
2017-07-18 22:01:43 +03:00
assert 'model' in MyMutation.Input._meta.fields
def test_nested_model():
class MyFakeModelGrapheneType(DjangoObjectType):
class Meta:
model = MyFakeModel
class MyMutation(SerializerMutation):
class Meta:
serializer_class = MySerializer
2017-05-31 01:30:03 +03:00
2017-07-18 22:01:43 +03:00
model_field = MyMutation._meta.fields['model']
assert isinstance(model_field, Field)
assert model_field.type == MyFakeModelGrapheneType
2017-05-31 01:30:03 +03:00
2017-07-18 22:01:43 +03:00
model_input = MyMutation.Input._meta.fields['model']
model_input_type = model_input._type.of_type
assert issubclass(model_input_type, InputObjectType)
assert 'cool_name' in model_input_type._meta.fields