Test invalid forms

This commit is contained in:
Grant McConnaughey 2017-10-02 13:15:29 -05:00
parent 463ce68b16
commit bf7ad7eeda
3 changed files with 18 additions and 18 deletions

View File

@ -16,21 +16,6 @@ except AttributeError:
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
def convert_form_field(field):
raise ImproperlyConfigured(

View File

@ -83,7 +83,7 @@ class FormMutation(BaseFormMutation):
@classmethod
def perform_mutate(cls, form, info):
form.save()
return cls(errors=None)
return cls(errors=[])
class ModelFormMutationOptions(FormMutationOptions):
@ -138,4 +138,4 @@ class ModelFormMutation(BaseFormMutation):
def perform_mutate(cls, form, info):
obj = form.save()
kwargs = {cls._meta.return_field_name: obj}
return cls(errors=None, **kwargs)
return cls(errors=[], **kwargs)

View File

@ -68,8 +68,23 @@ class ModelFormMutationTests(TestCase):
class Meta:
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)
pet = Pet.objects.get()
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.'])