Skip to content

fixes #322, fixed incorrect serializer instance usage #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions django_test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

INSTALLED_APPS = [
'graphene_django',
'graphene_django.rest_framework',
'graphene_django.tests',
'starwars',
]
Expand Down
6 changes: 6 additions & 0 deletions graphene_django/rest_framework/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.db import models


class MyFakeModel(models.Model):
cool_name = models.CharField(max_length=50)
created = models.DateTimeField(auto_now_add=True)
7 changes: 6 additions & 1 deletion graphene_django/rest_framework/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ def mutate_and_get_payload(cls, root, info, **input):
@classmethod
def perform_mutate(cls, serializer, info):
obj = serializer.save()
return cls(errors=None, **obj)

kwargs = {}
for f, field in serializer.fields.items():
kwargs[f] = field.get_attribute(obj)

return cls(errors=None, **kwargs)
35 changes: 29 additions & 6 deletions graphene_django/rest_framework/tests/test_mutation.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
from django.db import models
import datetime

from graphene import Field
from graphene.types.inputobjecttype import InputObjectType
from py.test import raises
from py.test import mark
from rest_framework import serializers

from ...types import DjangoObjectType
from ..models import MyFakeModel
from ..mutation import SerializerMutation


class MyFakeModel(models.Model):
cool_name = models.CharField(max_length=50)


class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyFakeModel
Expand Down Expand Up @@ -71,6 +70,7 @@ class Meta:
model_input_type = model_input._type.of_type
assert issubclass(model_input_type, InputObjectType)
assert 'cool_name' in model_input_type._meta.fields
assert 'created' in model_input_type._meta.fields


def test_mutate_and_get_payload_success():
Expand All @@ -88,6 +88,19 @@ class Meta:
assert result.errors is None


@mark.django_db
def test_model_mutate_and_get_payload_success():
class MyMutation(SerializerMutation):
class Meta:
serializer_class = MyModelSerializer

result = MyMutation.mutate_and_get_payload(None, None, **{
'cool_name': 'Narf',
})
assert result.errors is None
assert result.cool_name == 'Narf'
assert isinstance(result.created, datetime.datetime)

def test_mutate_and_get_payload_error():

class MyMutation(SerializerMutation):
Expand All @@ -96,4 +109,14 @@ class Meta:

# missing required fields
result = MyMutation.mutate_and_get_payload(None, None, **{})
assert len(result.errors) > 0
assert len(result.errors) > 0

def test_model_mutate_and_get_payload_error():

class MyMutation(SerializerMutation):
class Meta:
serializer_class = MyModelSerializer

# missing required fields
result = MyMutation.mutate_and_get_payload(None, None, **{})
assert len(result.errors) > 0