From 235096362f78133058663025060e1019c1ea26cc Mon Sep 17 00:00:00 2001 From: Sergey Protasov Date: Sun, 23 Feb 2020 14:49:39 +0500 Subject: [PATCH] Update mutation.py (#811) * Update mutation.py * Add tests Co-authored-by: Jonathan Kim --- graphene_django/forms/mutation.py | 4 +- graphene_django/forms/tests/test_mutation.py | 68 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/graphene_django/forms/mutation.py b/graphene_django/forms/mutation.py index 1eeeb97..b7bf049 100644 --- a/graphene_django/forms/mutation.py +++ b/graphene_django/forms/mutation.py @@ -47,7 +47,7 @@ class BaseDjangoFormMutation(ClientIDMutation): else: errors = ErrorType.from_errors(form.errors) - return cls(errors=errors) + return cls(errors=errors, **form.data) @classmethod def get_form(cls, root, info, **input): @@ -100,7 +100,7 @@ class DjangoFormMutation(BaseDjangoFormMutation): @classmethod def perform_mutate(cls, form, info): form.save() - return cls(errors=[]) + return cls(errors=[], **form.cleaned_data) class DjangoModelDjangoFormMutationOptions(DjangoFormMutationOptions): diff --git a/graphene_django/forms/tests/test_mutation.py b/graphene_django/forms/tests/test_mutation.py index 494c77c..cafec90 100644 --- a/graphene_django/forms/tests/test_mutation.py +++ b/graphene_django/forms/tests/test_mutation.py @@ -1,5 +1,6 @@ from django import forms from django.test import TestCase +from django.core.exceptions import ValidationError from py.test import raises from graphene import ObjectType, Schema, String, Field @@ -13,6 +14,15 @@ from ..mutation import DjangoFormMutation, DjangoModelFormMutation class MyForm(forms.Form): text = forms.CharField() + def clean_text(self): + text = self.cleaned_data["text"] + if text == "INVALID_INPUT": + raise ValidationError("Invalid input") + return text + + def save(self): + pass + class PetForm(forms.ModelForm): class Meta: @@ -83,6 +93,64 @@ class MockQuery(ObjectType): a = String() +class FormMutationTests(TestCase): + def test_form_invalid_form(self): + class MyMutation(DjangoFormMutation): + class Meta: + form_class = MyForm + + class Mutation(ObjectType): + my_mutation = MyMutation.Field() + + schema = Schema(query=MockQuery, mutation=Mutation) + + result = schema.execute( + """ mutation MyMutation { + myMutation(input: { text: "INVALID_INPUT" }) { + errors { + field + messages + } + text + } + } + """ + ) + + self.assertIs(result.errors, None) + self.assertEqual( + result.data["myMutation"]["errors"], + [{"field": "text", "messages": ["Invalid input"]}], + ) + + def test_form_valid_input(self): + class MyMutation(DjangoFormMutation): + class Meta: + form_class = MyForm + + class Mutation(ObjectType): + my_mutation = MyMutation.Field() + + schema = Schema(query=MockQuery, mutation=Mutation) + + result = schema.execute( + """ mutation MyMutation { + myMutation(input: { text: "VALID_INPUT" }) { + errors { + field + messages + } + text + } + } + """ + ) + + self.assertIs(result.errors, None) + self.assertEqual(result.data["myMutation"]["errors"], []) + self.assertEqual(result.data["myMutation"]["text"], "VALID_INPUT") + + class ModelFormMutationTests(TestCase): def test_default_meta_fields(self): class PetMutation(DjangoModelFormMutation):