10
10
from tests .serializers import GroupSerializer
11
11
from tests .setup import create_fixture
12
12
from tests .viewsets import (
13
- DogViewSet ,
14
13
GroupNoMergeDictViewSet ,
15
- GroupViewSet ,
16
14
UserViewSet
17
15
)
18
16
@@ -159,20 +157,17 @@ class BulkUpdateTestCase(TestCase):
159
157
160
158
def setUp (self ):
161
159
self .fixture = create_fixture ()
162
- self .rf = RequestFactory ()
163
- self .view = DogViewSet .as_view ({'patch' : 'partial_update' })
164
160
165
161
def test_bulk_update_default_style (self ):
166
162
'''
167
163
Test that PATCH request partially updates all submitted resources.
168
164
'''
169
165
data = [{'id' : 1 , 'fur' : 'grey' }, {'id' : 2 , 'fur' : 'grey' }]
170
- request = self .rf .patch (
166
+ response = self .client .patch (
171
167
'/dogs/' ,
172
168
json .dumps (data ),
173
169
content_type = 'application/json'
174
170
)
175
- response = self .view (request )
176
171
self .assertEqual (response .status_code , status .HTTP_200_OK )
177
172
self .assertTrue ('dogs' in response .data )
178
173
self .assertTrue (2 , len (response .data ['dogs' ]))
@@ -182,12 +177,11 @@ def test_bulk_update_default_style(self):
182
177
183
178
def test_bulk_update_drest_style (self ):
184
179
data = {'dogs' : [{'id' : 1 , 'fur' : 'grey' }, {'id' : 2 , 'fur' : 'grey' }]}
185
- request = self .rf .patch (
180
+ response = self .client .patch (
186
181
'/dogs/' ,
187
182
json .dumps (data ),
188
183
content_type = 'application/json'
189
184
)
190
- response = self .view (request )
191
185
self .assertEqual (response .status_code , status .HTTP_200_OK )
192
186
self .assertTrue ('dogs' in response .data )
193
187
@@ -196,12 +190,11 @@ def test_bulk_update_with_filter(self):
196
190
Test that you can patch inside of the filtered queryset.
197
191
'''
198
192
data = [{'id' : 3 , 'fur' : 'gold' }]
199
- request = self .rf .patch (
193
+ response = self .client .patch (
200
194
'/dogs/?filter{fur.contains}=brown' ,
201
195
json .dumps (data ),
202
196
content_type = 'application/json'
203
197
)
204
- response = self .view (request )
205
198
self .assertEqual (response .status_code , status .HTTP_200_OK )
206
199
self .assertTrue (Dog .objects .get (id = 3 ).fur_color == 'gold' )
207
200
@@ -210,30 +203,24 @@ def test_bulk_update_fail_without_lookup_attribute(self):
210
203
Test that PATCH request will fail if lookup attribute wasn't provided.
211
204
'''
212
205
data = [{'fur' : 'grey' }]
213
- request = self .rf .patch (
206
+ response = self .client .patch (
214
207
'/dogs/?filter{fur.contains}=brown' ,
215
208
json .dumps (data ),
216
209
content_type = 'application/json'
217
210
)
218
- response = self .view (request )
219
211
self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
220
212
221
213
222
214
class BulkCreationTestCase (TestCase ):
223
215
224
- def setUp (self ):
225
- self .rf = RequestFactory ()
226
- self .view = GroupViewSet .as_view ({'post' : 'create' })
227
-
228
216
def test_post_single (self ):
229
217
"""
230
218
Test that POST request with single resource only creates a single
231
219
resource.
232
220
"""
233
221
data = {'name' : 'foo' , 'random_input' : [1 , 2 , 3 ]}
234
- request = self .rf .post (
235
- '/group/' , json .dumps (data ), content_type = 'application/json' )
236
- response = self .view (request )
222
+ response = self .client .post (
223
+ '/groups/' , json .dumps (data ), content_type = 'application/json' )
237
224
self .assertEqual (response .status_code , status .HTTP_201_CREATED )
238
225
self .assertEqual (1 , Group .objects .all ().count ())
239
226
@@ -244,12 +231,11 @@ def test_post_bulk_from_resource_plural_name(self):
244
231
{'name' : 'bar' , 'random_input' : [4 , 5 , 6 ]},
245
232
]
246
233
}
247
- request = self .rf .post (
234
+ response = self .client .post (
248
235
'/groups/' ,
249
236
json .dumps (data ),
250
237
content_type = 'application/json'
251
238
)
252
- response = self .view (request )
253
239
self .assertEqual (response .status_code , status .HTTP_201_CREATED )
254
240
self .assertEqual (2 , Group .objects .all ().count ())
255
241
@@ -268,12 +254,11 @@ def test_post_bulk_from_list(self):
268
254
'random_input' : [4 , 5 , 6 ],
269
255
}
270
256
]
271
- request = self .rf .post (
257
+ response = self .client .post (
272
258
'/groups/' ,
273
259
json .dumps (data ),
274
260
content_type = 'application/json'
275
261
)
276
- response = self .view (request )
277
262
self .assertEqual (response .status_code , status .HTTP_201_CREATED )
278
263
self .assertEqual (2 , Group .objects .all ().count ())
279
264
self .assertEqual (
@@ -284,12 +269,11 @@ def test_post_bulk_from_list(self):
284
269
def test_post_bulk_with_existing_items_and_disabled_partial_creation (self ):
285
270
data = [{'name' : 'foo' }, {'name' : 'bar' }]
286
271
Group .objects .create (name = 'foo' )
287
- request = self .rf .post (
272
+ response = self .client .post (
288
273
'/groups/' ,
289
274
json .dumps (data ),
290
275
content_type = 'application/json'
291
276
)
292
- response = self .view (request )
293
277
self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
294
278
self .assertEqual (1 , Group .objects .all ().count ())
295
279
self .assertTrue ('errors' in response .data )
@@ -306,12 +290,11 @@ def test_post_bulk_with_sideloaded_results(self):
306
290
'members' : [u2 .pk ],
307
291
}
308
292
]
309
- request = self .rf .post (
293
+ response = self .client .post (
310
294
'/groups/?include[]=members.' ,
311
295
json .dumps (data ),
312
296
content_type = 'application/json'
313
297
)
314
- response = self .view (request )
315
298
self .assertEqual (response .status_code , status .HTTP_201_CREATED )
316
299
resp_data = response .data
317
300
0 commit comments