11import pytest
22from django import forms
3+ from django .test import TestCase
34from django .core .exceptions import ValidationError
45from py .test import raises
56
6- from graphene import Field , ObjectType , Schema , String
7+ from graphene import Field , Int , ObjectType , Schema , String
78from graphene_django import DjangoObjectType
89from graphene_django .tests .models import Pet
910
@@ -22,13 +23,17 @@ class Meta:
2223
2324class MyForm (forms .Form ):
2425 text = forms .CharField ()
26+ another = forms .CharField (required = False )
2527
2628 def clean_text (self ):
2729 text = self .cleaned_data ["text" ]
2830 if text == "INVALID_INPUT" :
2931 raise ValidationError ("Invalid input" )
3032 return text
3133
34+ def clean_another (self ):
35+ self .cleaned_data ["another" ] = self .cleaned_data ["another" ] or "defaultvalue"
36+
3237 def save (self ):
3338 pass
3439
@@ -68,6 +73,83 @@ class Meta:
6873 form_class = MyForm
6974
7075 assert "text" in MyMutation .Input ._meta .fields
76+ assert "another" in MyMutation .Input ._meta .fields
77+
78+
79+ def test_no_input_fields ():
80+ class MyMutation (DjangoFormMutation ):
81+ class Meta :
82+ form_class = MyForm
83+ input_fields = []
84+ assert set (MyMutation .Input ._meta .fields .keys ()) == set (["client_mutation_id" ])
85+
86+
87+ def test_filtering_input_fields ():
88+ class MyMutation (DjangoFormMutation ):
89+ class Meta :
90+ form_class = MyForm
91+ input_fields = ["text" ]
92+
93+ assert "text" in MyMutation .Input ._meta .fields
94+ assert "another" not in MyMutation .Input ._meta .fields
95+
96+
97+ def test_select_output_fields ():
98+ class MyMutation (DjangoFormMutation ):
99+ class Meta :
100+ form_class = MyForm
101+ fields = ["text" ]
102+ assert "text" in MyMutation ._meta .fields
103+ assert "another" not in MyMutation ._meta .fields
104+
105+
106+ def test_filtering_output_fields_exclude ():
107+ class FormWithWeirdOutput (MyForm ):
108+ """Weird form that has extra cleaned_data we want to expose"""
109+ text = forms .CharField ()
110+ another = forms .CharField (required = False )
111+ def clean (self ):
112+ super (FormWithWeirdOutput , self ).clean ()
113+ self .cleaned_data ["some_integer" ] = 5
114+ return self .cleaned_data
115+
116+ class MyMutation (DjangoFormMutation ):
117+ class Meta :
118+ form_class = FormWithWeirdOutput
119+ exclude = ["text" ]
120+
121+ some_integer = Int ()
122+
123+ assert "text" in MyMutation .Input ._meta .fields
124+ assert "another" in MyMutation .Input ._meta .fields
125+
126+ assert "text" not in MyMutation ._meta .fields
127+ assert "another" in MyMutation ._meta .fields
128+ assert "some_integer" in MyMutation ._meta .fields
129+
130+ class Mutation (ObjectType ):
131+ my_mutation = MyMutation .Field ()
132+
133+ schema = Schema (query = MockQuery , mutation = Mutation )
134+
135+ result = schema .execute (
136+ """ mutation MyMutation {
137+ myMutation(input: { text: "VALID_INPUT" }) {
138+ errors {
139+ field
140+ messages
141+ }
142+ another
143+ someInteger
144+ }
145+ }
146+ """
147+ )
148+
149+ assert result .errors is None
150+ assert result .data ["myMutation" ]["errors" ] == []
151+ assert result .data ["myMutation" ]["someInteger" ] == 5
152+ assert result .data ["myMutation" ]["another" ] == "defaultvalue"
71153
72154
73155def test_mutation_error_camelcased (pet_type , graphene_settings ):
0 commit comments