Skip to content

Commit a2da4ab

Browse files
committed
Nearly working tests
1 parent db8b38b commit a2da4ab

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

graphene_django/forms/mutation.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# from django import forms
2+
import warnings
23
from collections import OrderedDict
34

45
import graphene
@@ -112,24 +113,27 @@ def __init_subclass_with_meta__(
112113
raise Exception("form_class is required for DjangoFormMutation")
113114

114115
form = form_class()
115-
if (any(only, exclude, input_fields)
116+
if (any([fields, exclude, input_fields])
116117
and (only_fields or exclude_fields)):
117118
raise Exception("Cannot specify legacy `only_fields` or `exclude_fields` params with"
118119
" `only`, `exclude`, or `input_fields` params")
119120
if only_fields or exclude_fields:
120121
warnings.warn(
121-
DeprecationWarning,
122122
"only_fields/exclude_fields have been deprecated, use "
123123
"input_fields or only/exclude (for output fields)"
124-
"instead"
124+
"instead",
125+
DeprecationWarning
125126
)
126127
if not fields or exclude:
127128
warnings.warn(
128-
DeprecationWarning,
129129
"a future version of graphene-django will require fields or exclude."
130-
" Set fields='__all__' to allow all fields through."
130+
" Set fields='__all__' to allow all fields through.",
131+
DeprecationWarning
131132
)
132-
input_fields = fields_for_form(form, only_fields or input_fields, exclude_fields)
133+
if not input_fields and input_fields is not None:
134+
input_fields = {}
135+
else:
136+
input_fields = fields_for_form(form, only_fields or input_fields, exclude_fields)
133137
output_fields = fields_for_form(form, only_fields or fields, exclude_fields or exclude)
134138

135139
_meta = DjangoFormMutationOptions(cls)

graphene_django/forms/tests/test_mutation.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.core.exceptions import ValidationError
44
from py.test import raises
55

6-
from graphene import ObjectType, Schema, String, Field
6+
from graphene import Int, ObjectType, Schema, String, Field
77
from graphene_django import DjangoObjectType
88
from graphene_django.tests.models import Film, Pet
99

@@ -13,16 +13,16 @@
1313

1414
class MyForm(forms.Form):
1515
text = forms.CharField()
16-
another = forms.CharField(required=False, default="defaultvalue")
16+
another = forms.CharField(required=False)
1717

1818
def clean_text(self):
1919
text = self.cleaned_data["text"]
2020
if text == "INVALID_INPUT":
2121
raise ValidationError("Invalid input")
2222
return text
2323

24-
def clean(self):
25-
self.cleaned_data["some_integer"] = 5
24+
def clean_another(self):
25+
self.cleaned_data["another"] = self.cleaned_data["another"] or "defaultvalue"
2626

2727
def save(self):
2828
pass
@@ -77,12 +77,13 @@ class Meta:
7777
assert "text" in MyMutation.Input._meta.fields
7878
assert "another" in MyMutation.Input._meta.fields
7979

80+
8081
def test_no_input_fields():
8182
class MyMutation(DjangoFormMutation):
8283
class Meta:
8384
form_class = MyForm
8485
input_fields = []
85-
assert not MyMutation.Input._meta.fields
86+
assert set(MyMutation.Input._meta.fields.keys()) == set(["client_mutation_id"])
8687

8788

8889
def test_filtering_input_fields():
@@ -100,8 +101,8 @@ class MyMutation(DjangoFormMutation):
100101
class Meta:
101102
form_class = MyForm
102103
fields = ["text"]
103-
assert "text" in MyMutation.Output._meta.fields
104-
assert "another" not in MyMutation.Output._meta.fields
104+
assert "text" in MyMutation._meta.fields
105+
assert "another" not in MyMutation._meta.fields
105106

106107

107108

@@ -183,16 +184,28 @@ class Mutation(ObjectType):
183184
self.assertEqual(result.data["myMutation"]["text"], "VALID_INPUT")
184185

185186
def test_filtering_output_fields_exclude(self):
187+
class FormWithWeirdOutput(MyForm):
188+
"""Weird form that has extra cleaned_data we want to expose"""
189+
text = forms.CharField()
190+
another = forms.CharField(required=False)
191+
def clean(self):
192+
super(FormWithWeirdOutput, self).clean()
193+
self.cleaned_data["some_integer"] = 5
194+
return self.cleaned_data
195+
186196
class MyMutation(DjangoFormMutation):
187197
class Meta:
188-
form_class = MyForm
198+
form_class = FormWithWeirdOutput
189199
exclude = ["text"]
190200

191-
some_integer = graphene.Int()
201+
some_integer = Int()
192202

193-
assert "text" not in MyMutation.Output._meta.fields
194-
assert "another" in MyMutation.Output._meta.fields
195-
assert "some_integer" in MyMutation.Output._meta.fields
203+
assert "text" in MyMutation.Input._meta.fields
204+
assert "another" in MyMutation.Input._meta.fields
205+
206+
assert "text" not in MyMutation._meta.fields
207+
assert "another" in MyMutation._meta.fields
208+
assert "some_integer" in MyMutation._meta.fields
196209

197210
class Mutation(ObjectType):
198211
my_mutation = MyMutation.Field()
@@ -207,16 +220,15 @@ class Mutation(ObjectType):
207220
messages
208221
}
209222
another
210-
some_integer
223+
someInteger
211224
}
212225
}
213226
"""
214227
)
215228

216229
self.assertIs(result.errors, None)
217230
self.assertEqual(result.data["myMutation"]["errors"], [])
218-
self.assertEqual(result.data["myMutation"]["text"], "VALID_INPUT")
219-
self.assertEqual(result.data["myMutation"]["some_integer"], 5)
231+
self.assertEqual(result.data["myMutation"]["someInteger"], 5)
220232
self.assertEqual(result.data["myMutation"]["another"], "defaultvalue")
221233

222234

0 commit comments

Comments
 (0)