mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-13 01:32:24 +03:00
Update tests
This commit is contained in:
parent
8a9aa12c2c
commit
4f6e017b6c
|
@ -2,6 +2,8 @@ from django import forms
|
|||
from django.test import TestCase
|
||||
from py.test import raises
|
||||
|
||||
from graphene import ObjectType, Schema, String
|
||||
from graphene_django import DjangoObjectType
|
||||
from graphene_django.tests.models import Film, FilmDetails, Pet
|
||||
|
||||
from ...settings import graphene_settings
|
||||
|
@ -18,6 +20,24 @@ class PetForm(forms.ModelForm):
|
|||
fields = "__all__"
|
||||
|
||||
|
||||
class PetType(DjangoObjectType):
|
||||
class Meta:
|
||||
model = Pet
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
class FilmType(DjangoObjectType):
|
||||
class Meta:
|
||||
model = Film
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
class FilmDetailsType(DjangoObjectType):
|
||||
class Meta:
|
||||
model = FilmDetails
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
def test_needs_form_class():
|
||||
with raises(Exception) as exc:
|
||||
|
||||
|
@ -59,6 +79,10 @@ def test_mutation_error_camelcased():
|
|||
graphene_settings.CAMELCASE_ERRORS = False
|
||||
|
||||
|
||||
class MockQuery(ObjectType):
|
||||
a = String()
|
||||
|
||||
|
||||
class ModelFormMutationTests(TestCase):
|
||||
def test_default_meta_fields(self):
|
||||
class PetMutation(DjangoModelFormMutation):
|
||||
|
@ -113,34 +137,66 @@ class ModelFormMutationTests(TestCase):
|
|||
self.assertEqual(PetMutation._meta.return_field_name, "animal")
|
||||
self.assertIn("animal", PetMutation._meta.fields)
|
||||
|
||||
def test_model_form_mutation_mutate(self):
|
||||
def test_model_form_mutation_mutate_existing(self):
|
||||
class PetMutation(DjangoModelFormMutation):
|
||||
class Meta:
|
||||
form_class = PetForm
|
||||
|
||||
class Mutation(ObjectType):
|
||||
pet_mutation = PetMutation.Field()
|
||||
|
||||
schema = Schema(query=MockQuery, mutation=Mutation)
|
||||
|
||||
pet = Pet.objects.create(name="Axel", age=10)
|
||||
|
||||
result = PetMutation.mutate_and_get_payload(
|
||||
None, None, id=pet.pk, name="Mia", age=10
|
||||
result = schema.execute(
|
||||
""" mutation PetMutation($pk: ID!) {
|
||||
petMutation(input: { id: $pk, name: "Mia", age: 10 }) {
|
||||
pet {
|
||||
name
|
||||
age
|
||||
}
|
||||
}
|
||||
}
|
||||
""",
|
||||
variables={"pk": pet.pk},
|
||||
)
|
||||
|
||||
self.assertIs(result.errors, None)
|
||||
self.assertEqual(result.data["petMutation"]["pet"], {"name": "Mia", "age": 10})
|
||||
|
||||
self.assertEqual(Pet.objects.count(), 1)
|
||||
pet.refresh_from_db()
|
||||
self.assertEqual(pet.name, "Mia")
|
||||
self.assertEqual(result.errors, [])
|
||||
|
||||
def test_model_form_mutation_updates_existing_(self):
|
||||
def test_model_form_mutation_creates_new(self):
|
||||
class PetMutation(DjangoModelFormMutation):
|
||||
class Meta:
|
||||
form_class = PetForm
|
||||
|
||||
result = PetMutation.mutate_and_get_payload(None, None, name="Mia", age=10)
|
||||
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})
|
||||
|
||||
self.assertEqual(Pet.objects.count(), 1)
|
||||
pet = Pet.objects.get()
|
||||
self.assertEqual(pet.name, "Mia")
|
||||
self.assertEqual(pet.age, 10)
|
||||
self.assertEqual(result.errors, [])
|
||||
|
||||
def test_model_form_mutation_mutate_invalid_form(self):
|
||||
class PetMutation(DjangoModelFormMutation):
|
||||
|
|
Loading…
Reference in New Issue
Block a user