Skip to content

Commit

Permalink
Clean up create and update tests
Browse files Browse the repository at this point in the history
- Move an update test to the update file
- Add a new create test for one to one relations

Signed-off-by: Tormod Haugland <[email protected]>
  • Loading branch information
tOgg1 committed Jul 3, 2024
1 parent 94bb940 commit 09e5c5b
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 93 deletions.
117 changes: 25 additions & 92 deletions graphene_django_cud/tests/test_create_mutation.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import graphene
from addict import Dict
from django.test import TestCase
from graphene import Schema
from graphene import ResolveInfo
from graphene import Schema
from graphql_relay import to_global_id

from graphene_django_cud.mutations import DjangoUpdateMutation, DjangoCreateMutation
from graphene_django_cud.mutations import DjangoCreateMutation
from graphene_django_cud.tests.dummy_query import DummyQuery
from graphene_django_cud.tests.factories import (
UserFactory,
CatFactory,
DogFactory,
FishFactory,
)
from graphene_django_cud.tests.dummy_query import DummyQuery
from graphene_django_cud.tests.models import User, Cat, Dog, DogRegistration, Fish
from graphene_django_cud.util import disambiguate_id

Expand All @@ -34,7 +34,7 @@ def mock_info(context=None):

class TestCreateMutationManyToOneExtras(TestCase):
def test_many_to_one_extras__auto_calling_mutation_with_setting_field__does_nothing(
self,
self,
):
# This registers the UserNode type
from .schema import UserNode # noqa: F401
Expand Down Expand Up @@ -230,95 +230,31 @@ class Mutations(graphene.ObjectType):
self.assertEqual(user.cats.all().count(), 5)


class TestUpdateWithOneToOneField(TestCase):
def test__one_to_one_relation_exists__updates_specified_fields(self):

class TestCreateWithOneToOneField(TestCase):
def test__one_to_one__without_extra__assigns_field(self):
# This registers the UserNode type
from .schema import UserNode # noqa: F401
from .schema import UserNode

class UpdateDogMutation(DjangoUpdateMutation):
class CreateDogRegistrationMutation(DjangoCreateMutation):
class Meta:
model = Dog
one_to_one_extras = {"registration": {"type": "auto"}}
model = DogRegistration

class Mutations(graphene.ObjectType):
update_dog = UpdateDogMutation.Field()
create_dog_registration = CreateDogRegistrationMutation.Field()

user = UserFactory.create()
dog = DogFactory.create()
DogRegistration.objects.create(dog=dog, registration_number="1234")

schema = Schema(query=DummyQuery, mutation=Mutations)
mutation = """
mutation UpdateDog(
$id: ID!,
$input: UpdateDogInput!
){
updateDog(id: $id, input: $input){
dog{
id
registration{
id
registrationNumber
}
}
}
}
"""

result = schema.execute(
mutation,
variables={
"id": to_global_id("DogNode", dog.id),
"input": {
"name": dog.name,
"breed": dog.breed,
"tag": dog.tag,
"owner": to_global_id("UserNode", dog.owner.id),
"registration": {"registrationNumber": "12345"},
},
},
context=Dict(user=user),
)
self.assertIsNone(result.errors)
data = Dict(result.data)
self.assertIsNone(result.errors)
self.assertEqual("12345", data.updateDog.dog.registration.registrationNumber)

# Load from database
dog.refresh_from_db()
self.assertEqual(dog.registration.registration_number, "12345")

def test__reverse_one_to_one_exists__updates_specified_fields(self):
# This registers the UserNode type
from .schema import UserNode # noqa: F401

class UpdateDogRegistrationMutation(DjangoUpdateMutation):
class Meta:
model = DogRegistration
one_to_one_extras = {"dog": {"type": "auto"}}

class Mutations(graphene.ObjectType):
update_dog_registration = UpdateDogRegistrationMutation.Field()

user = UserFactory.create()
dog = DogFactory.create(breed="HUSKY")
dog_registration = DogRegistration.objects.create(dog=dog, registration_number="1234")

schema = Schema(query=DummyQuery, mutation=Mutations)
mutation = """
mutation UpdateDogRegistration(
$id: ID!,
$input: UpdateDogRegistrationInput!
mutation CreateDogRegistration(
$input: CreateDogRegistrationInput!
){
updateDogRegistration(id: $id, input: $input){
createDogRegistration(input: $input){
dogRegistration{
id
registrationNumber
dog{
id
breed
}
}
}
}
Expand All @@ -327,31 +263,27 @@ class Mutations(graphene.ObjectType):
result = schema.execute(
mutation,
variables={
"id": to_global_id("DogRegistrationNode", dog_registration.id),
"input": {
"registrationNumber": dog_registration.registration_number,
"dog": {
"name": dog.name,
"breed": "LABRADOR",
"tag": dog.tag,
"owner": to_global_id("UserNode", dog.owner.id),
},
"registrationNumber": "12345",
"dog": to_global_id("DogNode", dog.id),
},
},
context=Dict(user=user),
)

self.assertIsNone(result.errors)
data = Dict(result.data)
self.assertEqual("LABRADOR", data.updateDogRegistration.dogRegistration.dog.breed)

# Load from database
dog_registration.refresh_from_db()
dog.refresh_from_db()
self.assertEqual("12345", data.createDogRegistration.dogRegistration.registrationNumber)

self.assertEqual(dog.breed, "LABRADOR")
dog_registration = DogRegistration.objects.get(
pk=disambiguate_id(data.createDogRegistration.dogRegistration.id))
self.assertEqual(dog_registration.registration_number, "12345")
dog = getattr(dog_registration, "dog", None)
self.assertIsNotNone(dog)
self.assertEqual(dog.id, dog.id)


class TestCreateWithOneToOneField(TestCase):
def test__one_to_one_relation_exists__creates_specified_fields(self):
# This registers the UserNode type
from .schema import UserNode # noqa: F401
Expand Down Expand Up @@ -403,6 +335,7 @@ class Mutations(graphene.ObjectType):

# Load from database
dog = Dog.objects.get(pk=disambiguate_id(data.createDog.dog.id))
dog.refresh_from_db()
registration = getattr(dog, "registration", None)
self.assertIsNotNone(registration)
self.assertEqual(registration.registration_number, "12345")
Expand Down Expand Up @@ -599,7 +532,7 @@ class Mutations(graphene.ObjectType):

class TestCreateMutationCustomFields(TestCase):
def test_custom_field__separate_from_model_fields__adds_new_field_which_can_be_handled(
self,
self,
):
# This registers the UserNode type
from .schema import UserNode # noqa: F401
Expand Down
122 changes: 121 additions & 1 deletion graphene_django_cud/tests/test_update_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
MouseFactory,
FishFactory,
)
from graphene_django_cud.tests.models import User, Cat, Dog, Fish
from graphene_django_cud.tests.models import User, Cat, Dog, Fish, DogRegistration
from graphene_django_cud.util import disambiguate_id
from graphene_django_cud.tests.dummy_query import DummyQuery

Expand Down Expand Up @@ -1616,3 +1616,123 @@ class Mutations(graphene.ObjectType):

dog.refresh_from_db()
self.assertEqual(1, dog.bark_count)


class TestUpdateWithOneToOneField(TestCase):
def test__one_to_one_relation_exists__updates_specified_fields(self):
# This registers the UserNode type
from .schema import UserNode # noqa: F401

class UpdateDogMutation(DjangoUpdateMutation):
class Meta:
model = Dog
one_to_one_extras = {"registration": {"type": "auto"}}

class Mutations(graphene.ObjectType):
update_dog = UpdateDogMutation.Field()

user = UserFactory.create()
dog = DogFactory.create()
DogRegistration.objects.create(dog=dog, registration_number="1234")

schema = Schema(query=DummyQuery, mutation=Mutations)
mutation = """
mutation UpdateDog(
$id: ID!,
$input: UpdateDogInput!
){
updateDog(id: $id, input: $input){
dog{
id
registration{
id
registrationNumber
}
}
}
}
"""

result = schema.execute(
mutation,
variables={
"id": to_global_id("DogNode", dog.id),
"input": {
"name": dog.name,
"breed": dog.breed,
"tag": dog.tag,
"owner": to_global_id("UserNode", dog.owner.id),
"registration": {"registrationNumber": "12345"},
},
},
context=Dict(user=user),
)
self.assertIsNone(result.errors)
data = Dict(result.data)
self.assertIsNone(result.errors)
self.assertEqual("12345", data.updateDog.dog.registration.registrationNumber)

# Load from database
dog.refresh_from_db()
self.assertEqual(dog.registration.registration_number, "12345")

def test__reverse_one_to_one_exists__updates_specified_fields(self):
# This registers the UserNode type
from .schema import UserNode # noqa: F401

class UpdateDogRegistrationMutation(DjangoUpdateMutation):
class Meta:
model = DogRegistration
one_to_one_extras = {"dog": {"type": "auto"}}

class Mutations(graphene.ObjectType):
update_dog_registration = UpdateDogRegistrationMutation.Field()

user = UserFactory.create()
dog = DogFactory.create(breed="HUSKY")
dog_registration = DogRegistration.objects.create(dog=dog, registration_number="1234")

schema = Schema(query=DummyQuery, mutation=Mutations)
mutation = """
mutation UpdateDogRegistration(
$id: ID!,
$input: UpdateDogRegistrationInput!
){
updateDogRegistration(id: $id, input: $input){
dogRegistration{
id
registrationNumber
dog{
id
breed
}
}
}
}
"""

result = schema.execute(
mutation,
variables={
"id": to_global_id("DogRegistrationNode", dog_registration.id),
"input": {
"registrationNumber": dog_registration.registration_number,
"dog": {
"name": dog.name,
"breed": "LABRADOR",
"tag": dog.tag,
"owner": to_global_id("UserNode", dog.owner.id),
},
},
},
context=Dict(user=user),
)
self.assertIsNone(result.errors)
data = Dict(result.data)
self.assertEqual("LABRADOR", data.updateDogRegistration.dogRegistration.dog.breed)

# Load from database
dog_registration.refresh_from_db()
dog.refresh_from_db()

self.assertEqual(dog.breed, "LABRADOR")

0 comments on commit 09e5c5b

Please sign in to comment.