mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-22 09:37:07 +03:00
Update mutation.py (#811)
* Update mutation.py * Add tests Co-authored-by: Jonathan Kim <jkimbo@gmail.com>
This commit is contained in:
parent
a12fc9299a
commit
235096362f
|
@ -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):
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user