1
1
import datetime
2
2
3
- from graphene import Field
3
+ from graphene import Field , ResolveInfo
4
4
from graphene .types .inputobjecttype import InputObjectType
5
5
from py .test import raises
6
6
from py .test import mark
10
10
from ..models import MyFakeModel
11
11
from ..mutation import SerializerMutation
12
12
13
+ def mock_info ():
14
+ return ResolveInfo (
15
+ None ,
16
+ None ,
17
+ None ,
18
+ None ,
19
+ schema = None ,
20
+ fragments = None ,
21
+ root_value = None ,
22
+ operation = None ,
23
+ variable_values = None ,
24
+ context = None
25
+ )
26
+
13
27
14
28
class MyModelSerializer (serializers .ModelSerializer ):
15
29
class Meta :
16
30
model = MyFakeModel
17
31
fields = '__all__'
18
32
33
+ class MyModelMutation (SerializerMutation ):
34
+ class Meta :
35
+ serializer_class = MyModelSerializer
19
36
20
37
class MySerializer (serializers .Serializer ):
21
38
text = serializers .CharField ()
@@ -92,7 +109,7 @@ class MyMutation(SerializerMutation):
92
109
class Meta :
93
110
serializer_class = MySerializer
94
111
95
- result = MyMutation .mutate_and_get_payload (None , None , ** {
112
+ result = MyMutation .mutate_and_get_payload (None , mock_info () , ** {
96
113
'text' : 'value' ,
97
114
'model' : {
98
115
'cool_name' : 'other_value'
@@ -102,34 +119,58 @@ class Meta:
102
119
103
120
104
121
@mark .django_db
105
- def test_model_mutate_and_get_payload_success ():
106
- class MyMutation (SerializerMutation ):
107
- class Meta :
108
- serializer_class = MyModelSerializer
109
-
110
- result = MyMutation .mutate_and_get_payload (None , None , ** {
122
+ def test_model_add_mutate_and_get_payload_success ():
123
+ result = MyModelMutation .mutate_and_get_payload (None , mock_info (), ** {
111
124
'cool_name' : 'Narf' ,
112
125
})
113
126
assert result .errors is None
114
127
assert result .cool_name == 'Narf'
115
128
assert isinstance (result .created , datetime .datetime )
116
129
130
+ @mark .django_db
131
+ def test_model_update_mutate_and_get_payload_success ():
132
+ instance = MyFakeModel .objects .create (cool_name = "Narf" )
133
+ result = MyModelMutation .mutate_and_get_payload (None , mock_info (), ** {
134
+ 'id' : instance .id ,
135
+ 'cool_name' : 'New Narf' ,
136
+ })
137
+ assert result .errors is None
138
+ assert result .cool_name == 'New Narf'
139
+
140
+ @mark .django_db
141
+ def test_model_invalid_update_mutate_and_get_payload_success ():
142
+ class InvalidModelMutation (SerializerMutation ):
143
+ class Meta :
144
+ serializer_class = MyModelSerializer
145
+ model_operations = ['update' ]
146
+
147
+ with raises (Exception ) as exc :
148
+ result = InvalidModelMutation .mutate_and_get_payload (None , mock_info (), ** {
149
+ 'cool_name' : 'Narf' ,
150
+ })
151
+
152
+ assert '"id" required' in str (exc .value )
153
+
117
154
def test_mutate_and_get_payload_error ():
118
155
119
156
class MyMutation (SerializerMutation ):
120
157
class Meta :
121
158
serializer_class = MySerializer
122
159
123
160
# missing required fields
124
- result = MyMutation .mutate_and_get_payload (None , None , ** {})
161
+ result = MyMutation .mutate_and_get_payload (None , mock_info () , ** {})
125
162
assert len (result .errors ) > 0
126
163
127
164
def test_model_mutate_and_get_payload_error ():
128
-
129
- class MyMutation (SerializerMutation ):
130
- class Meta :
131
- serializer_class = MyModelSerializer
132
-
133
165
# missing required fields
134
- result = MyMutation .mutate_and_get_payload (None , None , ** {})
166
+ result = MyModelMutation .mutate_and_get_payload (None , mock_info () , ** {})
135
167
assert len (result .errors ) > 0
168
+
169
+ def test_invalid_serializer_operations ():
170
+ with raises (Exception ) as exc :
171
+ class MyModelMutation (SerializerMutation ):
172
+ class Meta :
173
+ serializer_class = MyModelSerializer
174
+ model_operations = ['Add' ]
175
+
176
+ assert 'model_operations' in str (exc .value )
0 commit comments