From 96c38b4349f5c9680531c301e3f2baf2d05b7fc9 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Sat, 11 Jan 2020 14:49:44 +0100 Subject: [PATCH] Update Django model form tests (#839) * Clean up code and raise an exception if the model type is not found * Update tests * Fix tests --- graphene_django/forms/mutation.py | 26 +------ graphene_django/forms/tests/test_mutation.py | 74 ++++++++++++++++++-- 2 files changed, 70 insertions(+), 30 deletions(-) diff --git a/graphene_django/forms/mutation.py b/graphene_django/forms/mutation.py index f5921e8..1eeeb97 100644 --- a/graphene_django/forms/mutation.py +++ b/graphene_django/forms/mutation.py @@ -66,28 +66,6 @@ class BaseDjangoFormMutation(ClientIDMutation): return kwargs -# class DjangoFormInputObjectTypeOptions(InputObjectTypeOptions): -# form_class = None - - -# class DjangoFormInputObjectType(InputObjectType): -# class Meta: -# abstract = True - -# @classmethod -# def __init_subclass_with_meta__(cls, form_class=None, -# only_fields=(), exclude_fields=(), _meta=None, **options): -# if not _meta: -# _meta = DjangoFormInputObjectTypeOptions(cls) -# assert isinstance(form_class, forms.Form), ( -# 'form_class must be an instance of django.forms.Form' -# ) -# _meta.form_class = form_class -# form = form_class() -# fields = fields_for_form(form, only_fields, exclude_fields) -# super(DjangoFormInputObjectType, cls).__init_subclass_with_meta__(_meta=_meta, fields=fields, **options) - - class DjangoFormMutationOptions(MutationOptions): form_class = None @@ -163,7 +141,9 @@ class DjangoModelFormMutation(BaseDjangoFormMutation): registry = get_global_registry() model_type = registry.get_type_for_model(model) - return_field_name = return_field_name + if not model_type: + raise Exception("No type registered for model: {}".format(model.__name__)) + if not return_field_name: model_name = model.__name__ return_field_name = model_name[:1].lower() + model_name[1:] diff --git a/graphene_django/forms/tests/test_mutation.py b/graphene_django/forms/tests/test_mutation.py index 2de5113..494c77c 100644 --- a/graphene_django/forms/tests/test_mutation.py +++ b/graphene_django/forms/tests/test_mutation.py @@ -2,6 +2,8 @@ from django import forms from django.test import TestCase from py.test import raises +from graphene import ObjectType, Schema, String, Field +from graphene_django import DjangoObjectType from graphene_django.tests.models import Film, FilmDetails, Pet from ...settings import graphene_settings @@ -18,6 +20,24 @@ class PetForm(forms.ModelForm): fields = "__all__" +class PetType(DjangoObjectType): + class Meta: + model = Pet + fields = "__all__" + + +class FilmType(DjangoObjectType): + class Meta: + model = Film + fields = "__all__" + + +class FilmDetailsType(DjangoObjectType): + class Meta: + model = FilmDetails + fields = "__all__" + + def test_needs_form_class(): with raises(Exception) as exc: @@ -59,6 +79,10 @@ def test_mutation_error_camelcased(): graphene_settings.CAMELCASE_ERRORS = False +class MockQuery(ObjectType): + a = String() + + class ModelFormMutationTests(TestCase): def test_default_meta_fields(self): class PetMutation(DjangoModelFormMutation): @@ -113,34 +137,70 @@ class ModelFormMutationTests(TestCase): self.assertEqual(PetMutation._meta.return_field_name, "animal") self.assertIn("animal", PetMutation._meta.fields) - def test_model_form_mutation_mutate(self): + def test_model_form_mutation_mutate_existing(self): class PetMutation(DjangoModelFormMutation): + pet = Field(PetType) + class Meta: form_class = PetForm + class Mutation(ObjectType): + pet_mutation = PetMutation.Field() + + schema = Schema(query=MockQuery, mutation=Mutation) + pet = Pet.objects.create(name="Axel", age=10) - result = PetMutation.mutate_and_get_payload( - None, None, id=pet.pk, name="Mia", age=10 + result = schema.execute( + """ mutation PetMutation($pk: ID!) { + petMutation(input: { id: $pk, name: "Mia", age: 10 }) { + pet { + name + age + } + } + } + """, + variables={"pk": pet.pk}, ) + self.assertIs(result.errors, None) + self.assertEqual(result.data["petMutation"]["pet"], {"name": "Mia", "age": 10}) + self.assertEqual(Pet.objects.count(), 1) pet.refresh_from_db() self.assertEqual(pet.name, "Mia") - self.assertEqual(result.errors, []) - def test_model_form_mutation_updates_existing_(self): + def test_model_form_mutation_creates_new(self): class PetMutation(DjangoModelFormMutation): + pet = Field(PetType) + class Meta: form_class = PetForm - result = PetMutation.mutate_and_get_payload(None, None, name="Mia", age=10) + class Mutation(ObjectType): + pet_mutation = PetMutation.Field() + + schema = Schema(query=MockQuery, mutation=Mutation) + + result = schema.execute( + """ mutation PetMutation { + petMutation(input: { name: "Mia", age: 10 }) { + pet { + name + age + } + } + } + """ + ) + self.assertIs(result.errors, None) + self.assertEqual(result.data["petMutation"]["pet"], {"name": "Mia", "age": 10}) self.assertEqual(Pet.objects.count(), 1) pet = Pet.objects.get() self.assertEqual(pet.name, "Mia") self.assertEqual(pet.age, 10) - self.assertEqual(result.errors, []) def test_model_form_mutation_mutate_invalid_form(self): class PetMutation(DjangoModelFormMutation):