Skip to content

Commit ab6c1bf

Browse files
committed
Add tests for DjangoFormInputObjectType
1 parent fa3255e commit ab6c1bf

File tree

1 file changed

+333
-0
lines changed

1 file changed

+333
-0
lines changed
Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
import graphene
2+
3+
from django import forms
4+
from py.test import raises
5+
6+
from graphene_django import DjangoObjectType
7+
from ..types import DjangoFormInputObjectType
8+
from ...tests.models import Reporter, Film, CHOICES
9+
10+
# Reporter a_choice CHOICES = ((1, "this"), (2, _("that")))
11+
THIS = CHOICES[0][0]
12+
THIS_ON_CLIENT_CONVERTED = "A_1"
13+
14+
# Film genre choices=[("do", "Documentary"), ("ac", "Action"), ("ot", "Other")],
15+
DOCUMENTARY = "do"
16+
DOCUMENTARY_ON_CLIENT_CONVERTED = "DO"
17+
18+
19+
class FilmForm(forms.ModelForm):
20+
class Meta:
21+
model = Film
22+
exclude = ()
23+
24+
25+
class ReporterType(DjangoObjectType):
26+
class Meta:
27+
model = Reporter
28+
fields = "__all__"
29+
30+
31+
class ReporterForm(forms.ModelForm):
32+
class Meta:
33+
model = Reporter
34+
exclude = ("pets", "email")
35+
36+
37+
class MyForm(forms.Form):
38+
text_field = forms.CharField()
39+
int_field = forms.IntegerField()
40+
41+
42+
def test_needs_form_class():
43+
with raises(Exception) as exc:
44+
45+
class MyInputType(DjangoFormInputObjectType):
46+
pass
47+
48+
assert exc.value.args[0] == "form_class is required for DjangoFormInputObjectType"
49+
50+
51+
def test_type_from_modelform_has_input_fields():
52+
class ReporterInputType(DjangoFormInputObjectType):
53+
class Meta:
54+
form_class = ReporterForm
55+
only_fields = ("first_name", "last_name", "a_choice")
56+
57+
fields = ["first_name", "last_name", "a_choice", "id"]
58+
assert all(f in ReporterInputType._meta.fields for f in fields)
59+
60+
61+
def test_type_from_form_has_input_fields():
62+
class MyFormInputType(DjangoFormInputObjectType):
63+
class Meta:
64+
form_class = MyForm
65+
66+
fields = ["text_field", "int_field", "id"]
67+
assert all(f in MyFormInputType._meta.fields for f in fields)
68+
69+
70+
def test_type_custom_id_field():
71+
class MyFormInputType(DjangoFormInputObjectType):
72+
class Meta:
73+
form_class = MyForm
74+
add_id_field_name = "pk"
75+
76+
fields = ["text_field", "int_field", "pk"]
77+
assert all(f in MyFormInputType._meta.fields for f in fields)
78+
assert MyFormInputType._meta.fields["pk"].type is graphene.ID
79+
80+
81+
def test_type_custom_id_field_type():
82+
class MyFormInputType(DjangoFormInputObjectType):
83+
class Meta:
84+
form_class = MyForm
85+
add_id_field_name = "pk"
86+
add_id_field_type = graphene.String(required=False)
87+
88+
fields = ["text_field", "int_field", "pk"]
89+
assert all(f in MyFormInputType._meta.fields for f in fields)
90+
assert MyFormInputType._meta.fields["pk"].type is graphene.String
91+
92+
93+
class MockQuery(graphene.ObjectType):
94+
a = graphene.String()
95+
96+
97+
def test_mutation_with_form_djangoforminputtype():
98+
class MyFormInputType(DjangoFormInputObjectType):
99+
class Meta:
100+
form_class = MyForm
101+
102+
class MyFormMutation(graphene.Mutation):
103+
class Arguments:
104+
form_data = MyFormInputType(required=True)
105+
106+
result = graphene.Boolean()
107+
108+
def mutate(_root, _info, form_data):
109+
form = MyForm(data=form_data)
110+
if form.is_valid():
111+
result = form.cleaned_data == {
112+
"text_field": "text",
113+
"int_field": 777,
114+
}
115+
return MyFormMutation(result=result)
116+
return MyFormMutation(result=False)
117+
118+
class Mutation(graphene.ObjectType):
119+
myForm_mutation = MyFormMutation.Field()
120+
121+
schema = graphene.Schema(query=MockQuery, mutation=Mutation)
122+
123+
result = schema.execute(
124+
""" mutation MyFormMutation($formData: MyFormInputType!) {
125+
myFormMutation(formData: $formData) {
126+
result
127+
}
128+
}
129+
""",
130+
variable_values={"formData": {"textField": "text", "intField": 777}},
131+
)
132+
assert result.errors is None
133+
assert result.data == {"myFormMutation": {"result": True}}
134+
135+
136+
def test_mutation_with_modelform_djangoforminputtype():
137+
class ReporterInputType(DjangoFormInputObjectType):
138+
class Meta:
139+
form_class = ReporterForm
140+
object_type = ReporterType
141+
only_fields = ("first_name", "last_name", "a_choice")
142+
143+
class ReporterMutation(graphene.Mutation):
144+
class Arguments:
145+
reporter_data = ReporterInputType(required=True)
146+
147+
result = graphene.Field(ReporterType)
148+
149+
def mutate(_root, _info, reporter_data):
150+
reporter = Reporter.objects.get(pk=reporter_data.id)
151+
form = ReporterForm(data=reporter_data, instance=reporter)
152+
if form.is_valid():
153+
reporter = form.save()
154+
return ReporterMutation(result=reporter)
155+
156+
return ReporterMutation(result=None)
157+
158+
class Mutation(graphene.ObjectType):
159+
report_mutation = ReporterMutation.Field()
160+
161+
schema = graphene.Schema(query=MockQuery, mutation=Mutation)
162+
163+
reporter = Reporter.objects.create(
164+
first_name="Bob", last_name="Roberts", a_choice=THIS
165+
)
166+
167+
result = schema.execute(
168+
""" mutation ReportMutation($reporterData: ReporterInputType!) {
169+
reportMutation(reporterData: $reporterData) {
170+
result {
171+
id,
172+
firstName,
173+
lastName,
174+
aChoice
175+
}
176+
}
177+
}
178+
""",
179+
variable_values={
180+
"reporterData": {
181+
"id": reporter.pk,
182+
"firstName": "Dave",
183+
"lastName": "Smith",
184+
"aChoice": THIS_ON_CLIENT_CONVERTED,
185+
}
186+
},
187+
)
188+
assert result.errors is None
189+
assert result.data["reportMutation"]["result"] == {
190+
"id": "1",
191+
"firstName": "Dave",
192+
"lastName": "Smith",
193+
"aChoice": THIS_ON_CLIENT_CONVERTED,
194+
}
195+
assert Reporter.objects.count() == 1
196+
reporter.refresh_from_db()
197+
assert reporter.first_name == "Dave"
198+
199+
200+
def reporter_enum_convert_mutation_result(
201+
ReporterInputType, choice_val_on_client=THIS_ON_CLIENT_CONVERTED
202+
):
203+
class ReporterMutation(graphene.Mutation):
204+
class Arguments:
205+
reporter = ReporterInputType(required=True)
206+
207+
result_str = graphene.String()
208+
result_int = graphene.Int()
209+
210+
def mutate(_root, _info, reporter):
211+
if isinstance(reporter.a_choice, int) or reporter.a_choice.isdigit():
212+
return ReporterMutation(result_int=reporter.a_choice, result_str=None)
213+
return ReporterMutation(result_int=None, result_str=reporter.a_choice)
214+
215+
class Mutation(graphene.ObjectType):
216+
report_mutation = ReporterMutation.Field()
217+
218+
schema = graphene.Schema(query=MockQuery, mutation=Mutation)
219+
220+
return schema.execute(
221+
""" mutation ReportMutation($reporter: ReporterInputType!) {
222+
reportMutation(reporter: $reporter) {
223+
resultStr,
224+
resultInt
225+
}
226+
}
227+
""",
228+
variable_values={"reporter": {"aChoice": choice_val_on_client}},
229+
)
230+
231+
232+
def test_enum_not_converted():
233+
class ReporterInputType(DjangoFormInputObjectType):
234+
class Meta:
235+
form_class = ReporterForm
236+
only_fields = ("a_choice",)
237+
238+
result = reporter_enum_convert_mutation_result(ReporterInputType)
239+
assert result.errors is None
240+
assert result.data["reportMutation"]["resultStr"] == THIS_ON_CLIENT_CONVERTED
241+
assert result.data["reportMutation"]["resultInt"] is None
242+
assert ReporterInputType._meta.fields["a_choice"].type is graphene.String
243+
244+
245+
def test_enum_is_converted_to_original():
246+
class ReporterInputType(DjangoFormInputObjectType):
247+
class Meta:
248+
form_class = ReporterForm
249+
object_type = ReporterType
250+
only_fields = ("a_choice",)
251+
252+
result = reporter_enum_convert_mutation_result(ReporterInputType)
253+
assert result.errors is None
254+
assert result.data["reportMutation"]["resultInt"] == THIS
255+
assert result.data["reportMutation"]["resultStr"] is None
256+
assert (
257+
ReporterInputType._meta.fields["a_choice"].type.__name__
258+
== "AChoiceEnumBackConvString"
259+
)
260+
261+
262+
def test_convert_choices_to_enum_is_false_and_field_type_as_in_model():
263+
class ReporterTypeNotConvertChoices(DjangoObjectType):
264+
class Meta:
265+
model = Reporter
266+
convert_choices_to_enum = False
267+
fields = "__all__"
268+
269+
class ReporterInputType(DjangoFormInputObjectType):
270+
class Meta:
271+
form_class = ReporterForm
272+
object_type = ReporterTypeNotConvertChoices
273+
only_fields = ("a_choice",)
274+
275+
result = reporter_enum_convert_mutation_result(ReporterInputType, THIS)
276+
assert result.errors is None
277+
assert result.data["reportMutation"]["resultInt"] == THIS
278+
assert result.data["reportMutation"]["resultStr"] is None
279+
assert ReporterInputType._meta.fields["a_choice"].type is graphene.Int
280+
281+
282+
def enum_convert_mutation_result_film(FilmInputType):
283+
class FilmMutation(graphene.Mutation):
284+
class Arguments:
285+
film = FilmInputType(required=True)
286+
287+
result = graphene.String()
288+
289+
def mutate(_root, _info, film):
290+
return FilmMutation(result=film.genre)
291+
292+
class Mutation(graphene.ObjectType):
293+
film_mutation = FilmMutation.Field()
294+
295+
schema = graphene.Schema(query=MockQuery, mutation=Mutation)
296+
297+
return schema.execute(
298+
""" mutation FilmMutation($film: FilmInputType!) {
299+
filmMutation(film: $film) {
300+
result
301+
}
302+
}
303+
""",
304+
variable_values={"film": {"genre": DOCUMENTARY_ON_CLIENT_CONVERTED}},
305+
)
306+
307+
308+
def test_enum_not_converted_required_non_number():
309+
class FilmInputType(DjangoFormInputObjectType):
310+
class Meta:
311+
form_class = FilmForm
312+
only_fields = ("genre",)
313+
314+
result = enum_convert_mutation_result_film(FilmInputType)
315+
assert result.errors is None
316+
assert result.data["filmMutation"]["result"] == DOCUMENTARY_ON_CLIENT_CONVERTED
317+
318+
319+
def test_enum_is_converted_to_original_required_non_number():
320+
class FilmType(DjangoObjectType):
321+
class Meta:
322+
model = Film
323+
fields = "__all__"
324+
325+
class FilmInputType(DjangoFormInputObjectType):
326+
class Meta:
327+
form_class = FilmForm
328+
object_type = FilmType
329+
only_fields = ("genre",)
330+
331+
result = enum_convert_mutation_result_film(FilmInputType)
332+
assert result.errors is None
333+
assert result.data["filmMutation"]["result"] == DOCUMENTARY

0 commit comments

Comments
 (0)