Skip to content

Commit c72e7e5

Browse files
committed
added ModelSerializer tests
1 parent 3d30038 commit c72e7e5

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

django_test_settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
INSTALLED_APPS = [
1010
'graphene_django',
11+
'graphene_django.rest_framework',
1112
'graphene_django.tests',
1213
'starwars',
1314
]

graphene_django/rest_framework/tests/test_mutation.py

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
from django.db import models
1+
import datetime
2+
23
from graphene import Field
34
from graphene.types.inputobjecttype import InputObjectType
45
from py.test import raises
6+
from py.test import mark
57
from rest_framework import serializers
68

79
from ...types import DjangoObjectType
10+
from ..models import MyFakeModel
811
from ..mutation import SerializerMutation
912

1013

11-
class MyFakeModel(models.Model):
12-
cool_name = models.CharField(max_length=50)
13-
created = models.DateTimeField(auto_now_add=True)
14-
15-
1614
class MyModelSerializer(serializers.ModelSerializer):
1715
class Meta:
1816
model = MyFakeModel
@@ -90,6 +88,19 @@ class Meta:
9088
assert result.errors is None
9189

9290

91+
@mark.django_db
92+
def test_model_mutate_and_get_payload_success():
93+
class MyMutation(SerializerMutation):
94+
class Meta:
95+
serializer_class = MyModelSerializer
96+
97+
result = MyMutation.mutate_and_get_payload(None, None, **{
98+
'cool_name': 'Narf',
99+
})
100+
assert result.errors is None
101+
assert result.cool_name == 'Narf'
102+
assert isinstance(result.created, datetime.datetime)
103+
93104
def test_mutate_and_get_payload_error():
94105

95106
class MyMutation(SerializerMutation):
@@ -98,4 +109,14 @@ class Meta:
98109

99110
# missing required fields
100111
result = MyMutation.mutate_and_get_payload(None, None, **{})
101-
assert len(result.errors) > 0
112+
assert len(result.errors) > 0
113+
114+
def test_model_mutate_and_get_payload_error():
115+
116+
class MyMutation(SerializerMutation):
117+
class Meta:
118+
serializer_class = MyModelSerializer
119+
120+
# missing required fields
121+
result = MyMutation.mutate_and_get_payload(None, None, **{})
122+
assert len(result.errors) > 0

0 commit comments

Comments
 (0)