From e7391937dd39f0a7f105e5814476bec4c91e0716 Mon Sep 17 00:00:00 2001 From: Paul Bailey Date: Fri, 10 Nov 2017 21:49:48 +0000 Subject: [PATCH 1/6] fixes #322, fixed incorrect serializer instance usage --- graphene_django/rest_framework/mutation.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/graphene_django/rest_framework/mutation.py b/graphene_django/rest_framework/mutation.py index 94d1e4b..58c6b9c 100644 --- a/graphene_django/rest_framework/mutation.py +++ b/graphene_django/rest_framework/mutation.py @@ -84,4 +84,9 @@ class SerializerMutation(ClientIDMutation): @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) From e05f41af405283a32052b2f04335488d3e42c0c3 Mon Sep 17 00:00:00 2001 From: Paul Bailey Date: Sun, 12 Nov 2017 23:10:29 +0000 Subject: [PATCH 2/6] fixed indentation --- graphene_django/rest_framework/mutation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphene_django/rest_framework/mutation.py b/graphene_django/rest_framework/mutation.py index 58c6b9c..a776eab 100644 --- a/graphene_django/rest_framework/mutation.py +++ b/graphene_django/rest_framework/mutation.py @@ -87,6 +87,6 @@ class SerializerMutation(ClientIDMutation): kwargs = {} for f, field in serializer.fields.items(): - kwargs[f] = field.get_attribute(obj) + kwargs[f] = field.get_attribute(obj) return cls(errors=None, **kwargs) From 75e11a8195daf42b507c3d1fd8608f4c3269fa50 Mon Sep 17 00:00:00 2001 From: Paul Bailey Date: Mon, 13 Nov 2017 16:06:07 +0000 Subject: [PATCH 3/6] add created field to test --- graphene_django/rest_framework/tests/test_mutation.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/graphene_django/rest_framework/tests/test_mutation.py b/graphene_django/rest_framework/tests/test_mutation.py index 852265d..bb3ba34 100644 --- a/graphene_django/rest_framework/tests/test_mutation.py +++ b/graphene_django/rest_framework/tests/test_mutation.py @@ -10,6 +10,7 @@ from ..mutation import SerializerMutation class MyFakeModel(models.Model): cool_name = models.CharField(max_length=50) + created = models.DateTimeField(auto_now_add=True) class MyModelSerializer(serializers.ModelSerializer): @@ -71,6 +72,7 @@ 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 + assert 'created' in model_input_type._meta.fields def test_mutate_and_get_payload_success(): From c72e7e55ebe4ff7fb3e4063c7fdc71deb406dd32 Mon Sep 17 00:00:00 2001 From: Paul Bailey Date: Mon, 13 Nov 2017 20:33:12 +0000 Subject: [PATCH 4/6] added ModelSerializer tests --- django_test_settings.py | 1 + .../rest_framework/tests/test_mutation.py | 35 +++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/django_test_settings.py b/django_test_settings.py index 2e08272..9279a73 100644 --- a/django_test_settings.py +++ b/django_test_settings.py @@ -8,6 +8,7 @@ SECRET_KEY = 1 INSTALLED_APPS = [ 'graphene_django', + 'graphene_django.rest_framework', 'graphene_django.tests', 'starwars', ] diff --git a/graphene_django/rest_framework/tests/test_mutation.py b/graphene_django/rest_framework/tests/test_mutation.py index bb3ba34..c34a971 100644 --- a/graphene_django/rest_framework/tests/test_mutation.py +++ b/graphene_django/rest_framework/tests/test_mutation.py @@ -1,18 +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) - created = models.DateTimeField(auto_now_add=True) - - class MyModelSerializer(serializers.ModelSerializer): class Meta: model = MyFakeModel @@ -90,6 +88,19 @@ def test_mutate_and_get_payload_success(): 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): @@ -98,4 +109,14 @@ def test_mutate_and_get_payload_error(): # missing required fields result = MyMutation.mutate_and_get_payload(None, None, **{}) - assert len(result.errors) > 0 \ No newline at end of file + 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 From 345fe877c4c73e650a5fb9dd7e7aa3c918bca760 Mon Sep 17 00:00:00 2001 From: Paul Bailey Date: Mon, 13 Nov 2017 20:35:00 +0000 Subject: [PATCH 5/6] added ModelSerializer tests --- graphene_django/rest_framework/models.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 graphene_django/rest_framework/models.py diff --git a/graphene_django/rest_framework/models.py b/graphene_django/rest_framework/models.py new file mode 100644 index 0000000..27ac2f4 --- /dev/null +++ b/graphene_django/rest_framework/models.py @@ -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) \ No newline at end of file From 6cfd5b28540dbffcf96e6968ebe251cc3ecd6792 Mon Sep 17 00:00:00 2001 From: Paul Bailey Date: Tue, 14 Nov 2017 22:10:26 +0000 Subject: [PATCH 6/6] added line ending --- graphene_django/rest_framework/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphene_django/rest_framework/models.py b/graphene_django/rest_framework/models.py index 27ac2f4..848837b 100644 --- a/graphene_django/rest_framework/models.py +++ b/graphene_django/rest_framework/models.py @@ -3,4 +3,4 @@ from django.db import models class MyFakeModel(models.Model): cool_name = models.CharField(max_length=50) - created = models.DateTimeField(auto_now_add=True) \ No newline at end of file + created = models.DateTimeField(auto_now_add=True)