graphene-django/graphene_django/forms/tests/test_mutation.py

113 lines
3.4 KiB
Python
Raw Normal View History

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
2017-07-18 18:15:25 +03:00
from py.test import raises
from graphene_django.tests.models import Pet, Film, FilmDetails
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()
class PetForm(forms.ModelForm):
class Meta:
model = Pet
2018-07-20 02:51:33 +03:00
fields = ("name",)
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
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
def test_return_field_name_is_camelcased(self):
class PetMutation(DjangoModelFormMutation):
class Meta:
form_class = PetForm
model = FilmDetails
self.assertEqual(PetMutation._meta.model, FilmDetails)
2018-07-20 02:51:33 +03:00
self.assertEqual(PetMutation._meta.return_field_name, "filmDetails")
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
model = Film
2018-07-20 02:51:33 +03:00
return_field_name = "animal"
2017-10-02 21:03:20 +03:00
self.assertEqual(PetMutation._meta.model, Film)
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
def test_model_form_mutation_mutate(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
2018-07-20 02:51:33 +03:00
pet = Pet.objects.create(name="Axel")
2017-11-29 23:12:02 +03:00
2018-07-20 02:51:33 +03:00
result = PetMutation.mutate_and_get_payload(None, None, id=pet.pk, name="Mia")
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
self.assertEqual(result.errors, [])
def test_model_form_mutation_updates_existing_(self):
class PetMutation(DjangoModelFormMutation):
class Meta:
form_class = PetForm
2018-07-20 02:51:33 +03:00
result = PetMutation.mutate_and_get_payload(None, None, name="Mia")
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")
2017-10-02 21:15:29 +03:00
self.assertEqual(result.errors, [])
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)
self.assertEqual(len(result.errors), 1)
2018-07-20 02:51:33 +03:00
self.assertEqual(result.errors[0].field, "name")
self.assertEqual(result.errors[0].messages, ["This field is required."])