Skip to content

Commit

Permalink
Test invalid forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant McConnaughey committed Oct 2, 2017
1 parent 463ce68 commit bf7ad7e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
15 changes: 0 additions & 15 deletions graphene_django/forms/converter.py
Expand Up @@ -16,21 +16,6 @@ class UUIDField(object):
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(
Expand Down
4 changes: 2 additions & 2 deletions graphene_django/forms/mutation.py
Expand Up @@ -83,7 +83,7 @@ def __init_subclass_with_meta__(cls, form_class=None,
@classmethod
def perform_mutate(cls, form, info):
form.save()
return cls(errors=None)
return cls(errors=[])


class ModelFormMutationOptions(FormMutationOptions):
Expand Down Expand Up @@ -138,4 +138,4 @@ def __init_subclass_with_meta__(cls, form_class=None, model=None, return_field_n
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)
17 changes: 16 additions & 1 deletion graphene_django/forms/tests/test_mutation.py
Expand Up @@ -68,8 +68,23 @@ class PetMutation(ModelFormMutation):
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.'])

0 comments on commit bf7ad7e

Please sign in to comment.