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

177 lines
4.9 KiB
Python
Raw Normal View History

2017-11-13 23:33:12 +03:00
import datetime
2017-11-15 01:02:28 +03:00
from graphene import Field, ResolveInfo
2017-07-18 22:01:43 +03:00
from graphene.types.inputobjecttype import InputObjectType
2017-05-31 01:30:03 +03:00
from py.test import raises
2017-11-13 23:33:12 +03:00
from py.test import mark
2017-05-31 01:30:03 +03:00
from rest_framework import serializers
2017-07-18 22:01:43 +03:00
from ...types import DjangoObjectType
2017-11-13 23:33:12 +03:00
from ..models import MyFakeModel
2017-05-31 01:30:03 +03:00
from ..mutation import SerializerMutation
2017-11-15 01:02:28 +03:00
def mock_info():
return ResolveInfo(
None,
None,
None,
None,
schema=None,
fragments=None,
root_value=None,
operation=None,
variable_values=None,
context=None
)
2017-05-31 01:30:03 +03:00
2017-07-18 22:01:43 +03:00
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyFakeModel
fields = '__all__'
2017-11-15 01:02:28 +03:00
class MyModelMutation(SerializerMutation):
class Meta:
serializer_class = MyModelSerializer
2017-07-18 22:01:43 +03:00
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 create(self, validated_data):
return validated_data
2017-05-31 01:30:03 +03:00
def test_needs_serializer_class():
with raises(Exception) as exc:
class MyMutation(SerializerMutation):
pass
2017-07-25 09:42:40 +03:00
assert str(exc.value) == 'serializer_class is required for the SerializerMutation'
2017-05-31 01:30:03 +03:00
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
2017-12-12 05:23:00 +03:00
def test_exclude_fields():
class MyMutation(SerializerMutation):
class Meta:
serializer_class = MyModelSerializer
exclude_fields = ['created']
assert 'cool_name' in MyMutation._meta.fields
assert 'created' not in MyMutation._meta.fields
assert 'errors' in MyMutation._meta.fields
assert 'cool_name' in MyMutation.Input._meta.fields
assert 'created' not in MyMutation.Input._meta.fields
2017-07-18 22:01:43 +03:00
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
2017-11-13 19:06:07 +03:00
assert 'created' in model_input_type._meta.fields
def test_mutate_and_get_payload_success():
class MyMutation(SerializerMutation):
class Meta:
serializer_class = MySerializer
2017-11-15 01:02:28 +03:00
result = MyMutation.mutate_and_get_payload(None, mock_info(), **{
'text': 'value',
'model': {
'cool_name': 'other_value'
}
})
assert result.errors is None
2017-11-13 23:33:12 +03:00
@mark.django_db
2017-11-15 01:02:28 +03:00
def test_model_add_mutate_and_get_payload_success():
result = MyModelMutation.mutate_and_get_payload(None, mock_info(), **{
2017-11-13 23:33:12 +03:00
'cool_name': 'Narf',
})
assert result.errors is None
assert result.cool_name == 'Narf'
assert isinstance(result.created, datetime.datetime)
2017-11-15 01:02:28 +03:00
@mark.django_db
def test_model_update_mutate_and_get_payload_success():
instance = MyFakeModel.objects.create(cool_name="Narf")
result = MyModelMutation.mutate_and_get_payload(None, mock_info(), **{
'id': instance.id,
'cool_name': 'New Narf',
})
assert result.errors is None
assert result.cool_name == 'New Narf'
@mark.django_db
def test_model_invalid_update_mutate_and_get_payload_success():
class InvalidModelMutation(SerializerMutation):
class Meta:
serializer_class = MyModelSerializer
model_operations = ['update']
with raises(Exception) as exc:
result = InvalidModelMutation.mutate_and_get_payload(None, mock_info(), **{
'cool_name': 'Narf',
})
assert '"id" required' in str(exc.value)
def test_mutate_and_get_payload_error():
class MyMutation(SerializerMutation):
class Meta:
serializer_class = MySerializer
# missing required fields
2017-11-15 01:02:28 +03:00
result = MyMutation.mutate_and_get_payload(None, mock_info(), **{})
2017-11-13 23:33:12 +03:00
assert len(result.errors) > 0
def test_model_mutate_and_get_payload_error():
# missing required fields
2017-11-15 01:02:28 +03:00
result = MyModelMutation.mutate_and_get_payload(None, mock_info(), **{})
2017-11-13 23:33:12 +03:00
assert len(result.errors) > 0
2017-11-15 01:02:28 +03:00
def test_invalid_serializer_operations():
with raises(Exception) as exc:
class MyModelMutation(SerializerMutation):
class Meta:
serializer_class = MyModelSerializer
model_operations = ['Add']
assert 'model_operations' in str(exc.value)