Skip to content

Commit 96c38b4

Browse files
authored
Update Django model form tests (#839)
* Clean up code and raise an exception if the model type is not found * Update tests * Fix tests
1 parent de87573 commit 96c38b4

File tree

2 files changed

+70
-30
lines changed

2 files changed

+70
-30
lines changed

graphene_django/forms/mutation.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,6 @@ def get_form_kwargs(cls, root, info, **input):
6666
return kwargs
6767

6868

69-
# class DjangoFormInputObjectTypeOptions(InputObjectTypeOptions):
70-
# form_class = None
71-
72-
73-
# class DjangoFormInputObjectType(InputObjectType):
74-
# class Meta:
75-
# abstract = True
76-
77-
# @classmethod
78-
# def __init_subclass_with_meta__(cls, form_class=None,
79-
# only_fields=(), exclude_fields=(), _meta=None, **options):
80-
# if not _meta:
81-
# _meta = DjangoFormInputObjectTypeOptions(cls)
82-
# assert isinstance(form_class, forms.Form), (
83-
# 'form_class must be an instance of django.forms.Form'
84-
# )
85-
# _meta.form_class = form_class
86-
# form = form_class()
87-
# fields = fields_for_form(form, only_fields, exclude_fields)
88-
# super(DjangoFormInputObjectType, cls).__init_subclass_with_meta__(_meta=_meta, fields=fields, **options)
89-
90-
9169
class DjangoFormMutationOptions(MutationOptions):
9270
form_class = None
9371

@@ -163,7 +141,9 @@ def __init_subclass_with_meta__(
163141

164142
registry = get_global_registry()
165143
model_type = registry.get_type_for_model(model)
166-
return_field_name = return_field_name
144+
if not model_type:
145+
raise Exception("No type registered for model: {}".format(model.__name__))
146+
167147
if not return_field_name:
168148
model_name = model.__name__
169149
return_field_name = model_name[:1].lower() + model_name[1:]

graphene_django/forms/tests/test_mutation.py

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from django.test import TestCase
33
from py.test import raises
44

5+
from graphene import ObjectType, Schema, String, Field
6+
from graphene_django import DjangoObjectType
57
from graphene_django.tests.models import Film, FilmDetails, Pet
68

79
from ...settings import graphene_settings
@@ -18,6 +20,24 @@ class Meta:
1820
fields = "__all__"
1921

2022

23+
class PetType(DjangoObjectType):
24+
class Meta:
25+
model = Pet
26+
fields = "__all__"
27+
28+
29+
class FilmType(DjangoObjectType):
30+
class Meta:
31+
model = Film
32+
fields = "__all__"
33+
34+
35+
class FilmDetailsType(DjangoObjectType):
36+
class Meta:
37+
model = FilmDetails
38+
fields = "__all__"
39+
40+
2141
def test_needs_form_class():
2242
with raises(Exception) as exc:
2343

@@ -59,6 +79,10 @@ class Meta:
5979
graphene_settings.CAMELCASE_ERRORS = False
6080

6181

82+
class MockQuery(ObjectType):
83+
a = String()
84+
85+
6286
class ModelFormMutationTests(TestCase):
6387
def test_default_meta_fields(self):
6488
class PetMutation(DjangoModelFormMutation):
@@ -113,34 +137,70 @@ class Meta:
113137
self.assertEqual(PetMutation._meta.return_field_name, "animal")
114138
self.assertIn("animal", PetMutation._meta.fields)
115139

116-
def test_model_form_mutation_mutate(self):
140+
def test_model_form_mutation_mutate_existing(self):
117141
class PetMutation(DjangoModelFormMutation):
142+
pet = Field(PetType)
143+
118144
class Meta:
119145
form_class = PetForm
120146

147+
class Mutation(ObjectType):
148+
pet_mutation = PetMutation.Field()
149+
150+
schema = Schema(query=MockQuery, mutation=Mutation)
151+
121152
pet = Pet.objects.create(name="Axel", age=10)
122153

123-
result = PetMutation.mutate_and_get_payload(
124-
None, None, id=pet.pk, name="Mia", age=10
154+
result = schema.execute(
155+
""" mutation PetMutation($pk: ID!) {
156+
petMutation(input: { id: $pk, name: "Mia", age: 10 }) {
157+
pet {
158+
name
159+
age
160+
}
161+
}
162+
}
163+
""",
164+
variables={"pk": pet.pk},
125165
)
126166

167+
self.assertIs(result.errors, None)
168+
self.assertEqual(result.data["petMutation"]["pet"], {"name": "Mia", "age": 10})
169+
127170
self.assertEqual(Pet.objects.count(), 1)
128171
pet.refresh_from_db()
129172
self.assertEqual(pet.name, "Mia")
130-
self.assertEqual(result.errors, [])
131173

132-
def test_model_form_mutation_updates_existing_(self):
174+
def test_model_form_mutation_creates_new(self):
133175
class PetMutation(DjangoModelFormMutation):
176+
pet = Field(PetType)
177+
134178
class Meta:
135179
form_class = PetForm
136180

137-
result = PetMutation.mutate_and_get_payload(None, None, name="Mia", age=10)
181+
class Mutation(ObjectType):
182+
pet_mutation = PetMutation.Field()
183+
184+
schema = Schema(query=MockQuery, mutation=Mutation)
185+
186+
result = schema.execute(
187+
""" mutation PetMutation {
188+
petMutation(input: { name: "Mia", age: 10 }) {
189+
pet {
190+
name
191+
age
192+
}
193+
}
194+
}
195+
"""
196+
)
197+
self.assertIs(result.errors, None)
198+
self.assertEqual(result.data["petMutation"]["pet"], {"name": "Mia", "age": 10})
138199

139200
self.assertEqual(Pet.objects.count(), 1)
140201
pet = Pet.objects.get()
141202
self.assertEqual(pet.name, "Mia")
142203
self.assertEqual(pet.age, 10)
143-
self.assertEqual(result.errors, [])
144204

145205
def test_model_form_mutation_mutate_invalid_form(self):
146206
class PetMutation(DjangoModelFormMutation):

0 commit comments

Comments
 (0)