mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-02-07 15:10:35 +03:00
Test invalid forms
This commit is contained in:
parent
463ce68b16
commit
bf7ad7eeda
|
@ -16,21 +16,6 @@ except AttributeError:
|
||||||
singledispatch = import_single_dispatch()
|
singledispatch = import_single_dispatch()
|
||||||
|
|
||||||
|
|
||||||
def convert_form_to_input_type(form_class):
|
|
||||||
form = form_class()
|
|
||||||
|
|
||||||
items = {
|
|
||||||
name: convert_form_field(field)
|
|
||||||
for name, field in form.fields.items()
|
|
||||||
}
|
|
||||||
|
|
||||||
return type(
|
|
||||||
'{}Input'.format(form.__class__.__name__),
|
|
||||||
(graphene.InputObjectType, ),
|
|
||||||
items
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@singledispatch
|
@singledispatch
|
||||||
def convert_form_field(field):
|
def convert_form_field(field):
|
||||||
raise ImproperlyConfigured(
|
raise ImproperlyConfigured(
|
||||||
|
|
|
@ -83,7 +83,7 @@ class FormMutation(BaseFormMutation):
|
||||||
@classmethod
|
@classmethod
|
||||||
def perform_mutate(cls, form, info):
|
def perform_mutate(cls, form, info):
|
||||||
form.save()
|
form.save()
|
||||||
return cls(errors=None)
|
return cls(errors=[])
|
||||||
|
|
||||||
|
|
||||||
class ModelFormMutationOptions(FormMutationOptions):
|
class ModelFormMutationOptions(FormMutationOptions):
|
||||||
|
@ -138,4 +138,4 @@ class ModelFormMutation(BaseFormMutation):
|
||||||
def perform_mutate(cls, form, info):
|
def perform_mutate(cls, form, info):
|
||||||
obj = form.save()
|
obj = form.save()
|
||||||
kwargs = {cls._meta.return_field_name: obj}
|
kwargs = {cls._meta.return_field_name: obj}
|
||||||
return cls(errors=None, **kwargs)
|
return cls(errors=[], **kwargs)
|
||||||
|
|
|
@ -68,8 +68,23 @@ class ModelFormMutationTests(TestCase):
|
||||||
class Meta:
|
class Meta:
|
||||||
form_class = PetForm
|
form_class = PetForm
|
||||||
|
|
||||||
PetMutation.mutate_and_get_payload(None, None, name='Fluffy')
|
result = PetMutation.mutate_and_get_payload(None, None, name='Fluffy')
|
||||||
|
|
||||||
self.assertEqual(Pet.objects.count(), 1)
|
self.assertEqual(Pet.objects.count(), 1)
|
||||||
pet = Pet.objects.get()
|
pet = Pet.objects.get()
|
||||||
self.assertEqual(pet.name, 'Fluffy')
|
self.assertEqual(pet.name, 'Fluffy')
|
||||||
|
self.assertEqual(result.errors, [])
|
||||||
|
|
||||||
|
def test_model_form_mutation_mutate_invalid_form(self):
|
||||||
|
class PetMutation(ModelFormMutation):
|
||||||
|
class Meta:
|
||||||
|
form_class = PetForm
|
||||||
|
|
||||||
|
result = PetMutation.mutate_and_get_payload(None, None)
|
||||||
|
|
||||||
|
# A pet was not created
|
||||||
|
self.assertEqual(Pet.objects.count(), 0)
|
||||||
|
|
||||||
|
self.assertEqual(len(result.errors), 1)
|
||||||
|
self.assertEqual(result.errors[0].field, 'name')
|
||||||
|
self.assertEqual(result.errors[0].messages, ['This field is required.'])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user