33from django .core .exceptions import ValidationError
44from py .test import raises
55
6- from graphene import ObjectType , Schema , String , Field
6+ from graphene import Int , ObjectType , Schema , String , Field
77from graphene_django import DjangoObjectType
88from graphene_django .tests .models import Film , Pet
99
1313
1414class 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+
8081def 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
8889def 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