2017-07-18 18:15:25 +03:00
|
|
|
from django import forms
|
2017-07-18 18:48:09 +03:00
|
|
|
from django.test import TestCase
|
2020-02-23 12:49:39 +03:00
|
|
|
from django.core.exceptions import ValidationError
|
2017-07-18 18:15:25 +03:00
|
|
|
from py.test import raises
|
|
|
|
|
2020-01-11 16:49:44 +03:00
|
|
|
from graphene import ObjectType, Schema, String, Field
|
|
|
|
from graphene_django import DjangoObjectType
|
2020-04-12 18:18:41 +03:00
|
|
|
from graphene_django.tests.models import Film, Pet
|
2019-06-25 11:40:29 +03:00
|
|
|
|
|
|
|
from ...settings import graphene_settings
|
2017-11-29 23:12:02 +03:00
|
|
|
from ..mutation import DjangoFormMutation, DjangoModelFormMutation
|
2017-07-18 18:15:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
class MyForm(forms.Form):
|
|
|
|
text = forms.CharField()
|
|
|
|
|
2020-02-23 12:49:39 +03:00
|
|
|
def clean_text(self):
|
|
|
|
text = self.cleaned_data["text"]
|
|
|
|
if text == "INVALID_INPUT":
|
|
|
|
raise ValidationError("Invalid input")
|
|
|
|
return text
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
pass
|
|
|
|
|
2017-07-18 18:15:25 +03:00
|
|
|
|
|
|
|
class PetForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Pet
|
2019-04-30 12:02:23 +03:00
|
|
|
fields = "__all__"
|
2017-07-18 18:15:25 +03:00
|
|
|
|
|
|
|
|
2020-01-11 16:49:44 +03:00
|
|
|
class PetType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Pet
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
|
|
|
|
class FilmType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Film
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
|
2017-07-18 18:15:25 +03:00
|
|
|
def test_needs_form_class():
|
|
|
|
with raises(Exception) as exc:
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2017-11-29 23:12:02 +03:00
|
|
|
class MyMutation(DjangoFormMutation):
|
2017-07-18 18:15:25 +03:00
|
|
|
pass
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
assert exc.value.args[0] == "form_class is required for DjangoFormMutation"
|
2017-07-18 18:15:25 +03:00
|
|
|
|
|
|
|
|
2017-10-02 21:03:20 +03:00
|
|
|
def test_has_output_fields():
|
2017-11-29 23:12:02 +03:00
|
|
|
class MyMutation(DjangoFormMutation):
|
2017-07-18 18:15:25 +03:00
|
|
|
class Meta:
|
|
|
|
form_class = MyForm
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
assert "errors" in MyMutation._meta.fields
|
2017-07-18 18:15:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_has_input_fields():
|
2017-11-29 23:12:02 +03:00
|
|
|
class MyMutation(DjangoFormMutation):
|
2017-07-18 18:15:25 +03:00
|
|
|
class Meta:
|
|
|
|
form_class = MyForm
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
assert "text" in MyMutation.Input._meta.fields
|
2017-07-18 18:15:25 +03:00
|
|
|
|
|
|
|
|
2019-06-25 11:40:29 +03:00
|
|
|
def test_mutation_error_camelcased():
|
|
|
|
class ExtraPetForm(PetForm):
|
|
|
|
test_field = forms.CharField(required=True)
|
|
|
|
|
|
|
|
class PetMutation(DjangoModelFormMutation):
|
|
|
|
class Meta:
|
|
|
|
form_class = ExtraPetForm
|
|
|
|
|
|
|
|
result = PetMutation.mutate_and_get_payload(None, None)
|
|
|
|
assert {f.field for f in result.errors} == {"name", "age", "test_field"}
|
2019-07-09 00:22:08 +03:00
|
|
|
graphene_settings.CAMELCASE_ERRORS = True
|
2019-06-25 11:40:29 +03:00
|
|
|
result = PetMutation.mutate_and_get_payload(None, None)
|
|
|
|
assert {f.field for f in result.errors} == {"name", "age", "testField"}
|
2019-07-09 00:22:08 +03:00
|
|
|
graphene_settings.CAMELCASE_ERRORS = False
|
2019-06-25 11:40:29 +03:00
|
|
|
|
|
|
|
|
2020-01-11 16:49:44 +03:00
|
|
|
class MockQuery(ObjectType):
|
|
|
|
a = String()
|
|
|
|
|
|
|
|
|
2020-02-23 12:49:39 +03:00
|
|
|
class FormMutationTests(TestCase):
|
|
|
|
def test_form_invalid_form(self):
|
|
|
|
class MyMutation(DjangoFormMutation):
|
|
|
|
class Meta:
|
|
|
|
form_class = MyForm
|
|
|
|
|
|
|
|
class Mutation(ObjectType):
|
|
|
|
my_mutation = MyMutation.Field()
|
|
|
|
|
|
|
|
schema = Schema(query=MockQuery, mutation=Mutation)
|
|
|
|
|
|
|
|
result = schema.execute(
|
|
|
|
""" mutation MyMutation {
|
|
|
|
myMutation(input: { text: "INVALID_INPUT" }) {
|
|
|
|
errors {
|
|
|
|
field
|
|
|
|
messages
|
|
|
|
}
|
|
|
|
text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertIs(result.errors, None)
|
|
|
|
self.assertEqual(
|
|
|
|
result.data["myMutation"]["errors"],
|
|
|
|
[{"field": "text", "messages": ["Invalid input"]}],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_form_valid_input(self):
|
|
|
|
class MyMutation(DjangoFormMutation):
|
|
|
|
class Meta:
|
|
|
|
form_class = MyForm
|
|
|
|
|
|
|
|
class Mutation(ObjectType):
|
|
|
|
my_mutation = MyMutation.Field()
|
|
|
|
|
|
|
|
schema = Schema(query=MockQuery, mutation=Mutation)
|
|
|
|
|
|
|
|
result = schema.execute(
|
|
|
|
""" mutation MyMutation {
|
|
|
|
myMutation(input: { text: "VALID_INPUT" }) {
|
|
|
|
errors {
|
|
|
|
field
|
|
|
|
messages
|
|
|
|
}
|
|
|
|
text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertIs(result.errors, None)
|
|
|
|
self.assertEqual(result.data["myMutation"]["errors"], [])
|
|
|
|
self.assertEqual(result.data["myMutation"]["text"], "VALID_INPUT")
|
|
|
|
|
|
|
|
|
2017-07-18 18:48:09 +03:00
|
|
|
class ModelFormMutationTests(TestCase):
|
2017-10-02 21:03:20 +03:00
|
|
|
def test_default_meta_fields(self):
|
2017-11-29 23:12:02 +03:00
|
|
|
class PetMutation(DjangoModelFormMutation):
|
2017-07-18 18:48:09 +03:00
|
|
|
class Meta:
|
|
|
|
form_class = PetForm
|
|
|
|
|
2017-10-02 21:03:20 +03:00
|
|
|
self.assertEqual(PetMutation._meta.model, Pet)
|
2018-07-20 02:51:33 +03:00
|
|
|
self.assertEqual(PetMutation._meta.return_field_name, "pet")
|
|
|
|
self.assertIn("pet", PetMutation._meta.fields)
|
2017-10-02 21:03:20 +03:00
|
|
|
|
2018-07-23 01:34:01 +03:00
|
|
|
def test_default_input_meta_fields(self):
|
|
|
|
class PetMutation(DjangoModelFormMutation):
|
|
|
|
class Meta:
|
|
|
|
form_class = PetForm
|
|
|
|
|
|
|
|
self.assertEqual(PetMutation._meta.model, Pet)
|
|
|
|
self.assertEqual(PetMutation._meta.return_field_name, "pet")
|
|
|
|
self.assertIn("name", PetMutation.Input._meta.fields)
|
|
|
|
self.assertIn("client_mutation_id", PetMutation.Input._meta.fields)
|
|
|
|
self.assertIn("id", PetMutation.Input._meta.fields)
|
|
|
|
|
|
|
|
def test_exclude_fields_input_meta_fields(self):
|
|
|
|
class PetMutation(DjangoModelFormMutation):
|
|
|
|
class Meta:
|
|
|
|
form_class = PetForm
|
2019-04-30 12:02:23 +03:00
|
|
|
exclude_fields = ["id"]
|
2018-07-23 01:34:01 +03:00
|
|
|
|
|
|
|
self.assertEqual(PetMutation._meta.model, Pet)
|
|
|
|
self.assertEqual(PetMutation._meta.return_field_name, "pet")
|
|
|
|
self.assertIn("name", PetMutation.Input._meta.fields)
|
|
|
|
self.assertIn("age", PetMutation.Input._meta.fields)
|
|
|
|
self.assertIn("client_mutation_id", PetMutation.Input._meta.fields)
|
|
|
|
self.assertNotIn("id", PetMutation.Input._meta.fields)
|
|
|
|
|
2017-10-02 21:03:20 +03:00
|
|
|
def test_custom_return_field_name(self):
|
2017-11-29 23:12:02 +03:00
|
|
|
class PetMutation(DjangoModelFormMutation):
|
2017-10-02 21:03:20 +03:00
|
|
|
class Meta:
|
|
|
|
form_class = PetForm
|
2020-04-12 18:18:41 +03:00
|
|
|
model = Pet
|
2018-07-20 02:51:33 +03:00
|
|
|
return_field_name = "animal"
|
2017-10-02 21:03:20 +03:00
|
|
|
|
2020-04-12 18:18:41 +03:00
|
|
|
self.assertEqual(PetMutation._meta.model, Pet)
|
2018-07-20 02:51:33 +03:00
|
|
|
self.assertEqual(PetMutation._meta.return_field_name, "animal")
|
|
|
|
self.assertIn("animal", PetMutation._meta.fields)
|
2017-07-18 18:48:09 +03:00
|
|
|
|
2020-01-11 16:49:44 +03:00
|
|
|
def test_model_form_mutation_mutate_existing(self):
|
2017-11-29 23:12:02 +03:00
|
|
|
class PetMutation(DjangoModelFormMutation):
|
2020-01-11 16:49:44 +03:00
|
|
|
pet = Field(PetType)
|
|
|
|
|
2017-07-18 18:48:09 +03:00
|
|
|
class Meta:
|
|
|
|
form_class = PetForm
|
|
|
|
|
2020-01-11 16:49:44 +03:00
|
|
|
class Mutation(ObjectType):
|
|
|
|
pet_mutation = PetMutation.Field()
|
|
|
|
|
|
|
|
schema = Schema(query=MockQuery, mutation=Mutation)
|
|
|
|
|
2018-07-23 01:34:01 +03:00
|
|
|
pet = Pet.objects.create(name="Axel", age=10)
|
2017-11-29 23:12:02 +03:00
|
|
|
|
2020-01-11 16:49:44 +03:00
|
|
|
result = schema.execute(
|
|
|
|
""" mutation PetMutation($pk: ID!) {
|
|
|
|
petMutation(input: { id: $pk, name: "Mia", age: 10 }) {
|
|
|
|
pet {
|
|
|
|
name
|
|
|
|
age
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""",
|
2020-02-23 12:50:40 +03:00
|
|
|
variable_values={"pk": pet.pk},
|
2019-04-30 12:02:23 +03:00
|
|
|
)
|
2017-11-29 23:12:02 +03:00
|
|
|
|
2020-01-11 16:49:44 +03:00
|
|
|
self.assertIs(result.errors, None)
|
|
|
|
self.assertEqual(result.data["petMutation"]["pet"], {"name": "Mia", "age": 10})
|
|
|
|
|
2017-11-29 23:12:02 +03:00
|
|
|
self.assertEqual(Pet.objects.count(), 1)
|
|
|
|
pet.refresh_from_db()
|
2018-07-20 02:51:33 +03:00
|
|
|
self.assertEqual(pet.name, "Mia")
|
2017-11-29 23:12:02 +03:00
|
|
|
|
2020-01-11 16:49:44 +03:00
|
|
|
def test_model_form_mutation_creates_new(self):
|
2017-11-29 23:12:02 +03:00
|
|
|
class PetMutation(DjangoModelFormMutation):
|
2020-01-11 16:49:44 +03:00
|
|
|
pet = Field(PetType)
|
|
|
|
|
2017-11-29 23:12:02 +03:00
|
|
|
class Meta:
|
|
|
|
form_class = PetForm
|
|
|
|
|
2020-01-11 16:49:44 +03:00
|
|
|
class Mutation(ObjectType):
|
|
|
|
pet_mutation = PetMutation.Field()
|
|
|
|
|
|
|
|
schema = Schema(query=MockQuery, mutation=Mutation)
|
|
|
|
|
|
|
|
result = schema.execute(
|
|
|
|
""" mutation PetMutation {
|
|
|
|
petMutation(input: { name: "Mia", age: 10 }) {
|
|
|
|
pet {
|
|
|
|
name
|
|
|
|
age
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
self.assertIs(result.errors, None)
|
|
|
|
self.assertEqual(result.data["petMutation"]["pet"], {"name": "Mia", "age": 10})
|
2017-07-18 18:15:25 +03:00
|
|
|
|
2017-07-18 18:48:09 +03:00
|
|
|
self.assertEqual(Pet.objects.count(), 1)
|
|
|
|
pet = Pet.objects.get()
|
2018-07-20 02:51:33 +03:00
|
|
|
self.assertEqual(pet.name, "Mia")
|
2018-07-23 01:34:01 +03:00
|
|
|
self.assertEqual(pet.age, 10)
|
2017-10-02 21:15:29 +03:00
|
|
|
|
|
|
|
def test_model_form_mutation_mutate_invalid_form(self):
|
2017-11-29 23:12:02 +03:00
|
|
|
class PetMutation(DjangoModelFormMutation):
|
2017-10-02 21:15:29 +03:00
|
|
|
class Meta:
|
|
|
|
form_class = PetForm
|
|
|
|
|
|
|
|
result = PetMutation.mutate_and_get_payload(None, None)
|
|
|
|
|
|
|
|
# A pet was not created
|
|
|
|
self.assertEqual(Pet.objects.count(), 0)
|
|
|
|
|
2018-07-23 01:53:58 +03:00
|
|
|
fields_w_error = [e.field for e in result.errors]
|
2018-07-23 01:34:01 +03:00
|
|
|
self.assertEqual(len(result.errors), 2)
|
2018-07-23 01:53:58 +03:00
|
|
|
self.assertIn("name", fields_w_error)
|
2018-07-20 02:51:33 +03:00
|
|
|
self.assertEqual(result.errors[0].messages, ["This field is required."])
|
2018-07-23 01:53:58 +03:00
|
|
|
self.assertIn("age", fields_w_error)
|
2018-07-23 01:34:01 +03:00
|
|
|
self.assertEqual(result.errors[1].messages, ["This field is required."])
|