3
3
from django .core .exceptions import ValidationError
4
4
from py .test import raises
5
5
6
- from graphene import ObjectType , Schema , String , Field
6
+ from graphene import Int , ObjectType , Schema , String , Field
7
7
from graphene_django import DjangoObjectType
8
8
from graphene_django .tests .models import Film , Pet
9
9
13
13
14
14
class MyForm (forms .Form ):
15
15
text = forms .CharField ()
16
- another = forms .CharField (required = False , default = "defaultvalue" )
16
+ another = forms .CharField (required = False )
17
17
18
18
def clean_text (self ):
19
19
text = self .cleaned_data ["text" ]
20
20
if text == "INVALID_INPUT" :
21
21
raise ValidationError ("Invalid input" )
22
22
return text
23
23
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"
26
26
27
27
def save (self ):
28
28
pass
@@ -77,12 +77,13 @@ class Meta:
77
77
assert "text" in MyMutation .Input ._meta .fields
78
78
assert "another" in MyMutation .Input ._meta .fields
79
79
80
+
80
81
def test_no_input_fields ():
81
82
class MyMutation (DjangoFormMutation ):
82
83
class Meta :
83
84
form_class = MyForm
84
85
input_fields = []
85
- assert not MyMutation .Input ._meta .fields
86
+ assert set ( MyMutation .Input ._meta .fields . keys ()) == set ([ "client_mutation_id" ])
86
87
87
88
88
89
def test_filtering_input_fields ():
@@ -100,8 +101,8 @@ class MyMutation(DjangoFormMutation):
100
101
class Meta :
101
102
form_class = MyForm
102
103
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
105
106
106
107
107
108
@@ -183,16 +184,28 @@ class Mutation(ObjectType):
183
184
self .assertEqual (result .data ["myMutation" ]["text" ], "VALID_INPUT" )
184
185
185
186
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
+
186
196
class MyMutation (DjangoFormMutation ):
187
197
class Meta :
188
- form_class = MyForm
198
+ form_class = FormWithWeirdOutput
189
199
exclude = ["text" ]
190
200
191
- some_integer = graphene . Int ()
201
+ some_integer = Int ()
192
202
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
196
209
197
210
class Mutation (ObjectType ):
198
211
my_mutation = MyMutation .Field ()
@@ -207,16 +220,15 @@ class Mutation(ObjectType):
207
220
messages
208
221
}
209
222
another
210
- some_integer
223
+ someInteger
211
224
}
212
225
}
213
226
"""
214
227
)
215
228
216
229
self .assertIs (result .errors , None )
217
230
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 )
220
232
self .assertEqual (result .data ["myMutation" ]["another" ], "defaultvalue" )
221
233
222
234
0 commit comments