Skip to content

Commit 879f4a4

Browse files
authored
Merge pull request #131 from AltSchool/feature/bulk-patch-list
Add patch to list route
2 parents 0818b8e + 849cfd5 commit 879f4a4

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

dynamic_rest/routers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,16 @@ def sort_key(r):
5757
return directory_list
5858

5959

60+
def add_patch_to_list_route(routes):
61+
# Identify the list route, add PATCH so we can
62+
# have bulk PATCH requests
63+
list_route = next(i for i in routes if i.name == '{basename}-list')
64+
list_route.mapping['patch'] = 'partial_update'
65+
66+
6067
class DynamicRouter(DefaultRouter):
68+
routes = list(DefaultRouter.routes)
69+
add_patch_to_list_route(routes)
6170

6271
def __init__(self, *args, **kwargs):
6372
optional_trailing_slash = kwargs.pop('optional_trailing_slash', True)

tests/test_viewsets.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
from tests.serializers import GroupSerializer
1111
from tests.setup import create_fixture
1212
from tests.viewsets import (
13-
DogViewSet,
1413
GroupNoMergeDictViewSet,
15-
GroupViewSet,
1614
UserViewSet
1715
)
1816

@@ -159,20 +157,17 @@ class BulkUpdateTestCase(TestCase):
159157

160158
def setUp(self):
161159
self.fixture = create_fixture()
162-
self.rf = RequestFactory()
163-
self.view = DogViewSet.as_view({'patch': 'partial_update'})
164160

165161
def test_bulk_update_default_style(self):
166162
'''
167163
Test that PATCH request partially updates all submitted resources.
168164
'''
169165
data = [{'id': 1, 'fur': 'grey'}, {'id': 2, 'fur': 'grey'}]
170-
request = self.rf.patch(
166+
response = self.client.patch(
171167
'/dogs/',
172168
json.dumps(data),
173169
content_type='application/json'
174170
)
175-
response = self.view(request)
176171
self.assertEqual(response.status_code, status.HTTP_200_OK)
177172
self.assertTrue('dogs' in response.data)
178173
self.assertTrue(2, len(response.data['dogs']))
@@ -182,12 +177,11 @@ def test_bulk_update_default_style(self):
182177

183178
def test_bulk_update_drest_style(self):
184179
data = {'dogs': [{'id': 1, 'fur': 'grey'}, {'id': 2, 'fur': 'grey'}]}
185-
request = self.rf.patch(
180+
response = self.client.patch(
186181
'/dogs/',
187182
json.dumps(data),
188183
content_type='application/json'
189184
)
190-
response = self.view(request)
191185
self.assertEqual(response.status_code, status.HTTP_200_OK)
192186
self.assertTrue('dogs' in response.data)
193187

@@ -196,12 +190,11 @@ def test_bulk_update_with_filter(self):
196190
Test that you can patch inside of the filtered queryset.
197191
'''
198192
data = [{'id': 3, 'fur': 'gold'}]
199-
request = self.rf.patch(
193+
response = self.client.patch(
200194
'/dogs/?filter{fur.contains}=brown',
201195
json.dumps(data),
202196
content_type='application/json'
203197
)
204-
response = self.view(request)
205198
self.assertEqual(response.status_code, status.HTTP_200_OK)
206199
self.assertTrue(Dog.objects.get(id=3).fur_color == 'gold')
207200

@@ -210,30 +203,24 @@ def test_bulk_update_fail_without_lookup_attribute(self):
210203
Test that PATCH request will fail if lookup attribute wasn't provided.
211204
'''
212205
data = [{'fur': 'grey'}]
213-
request = self.rf.patch(
206+
response = self.client.patch(
214207
'/dogs/?filter{fur.contains}=brown',
215208
json.dumps(data),
216209
content_type='application/json'
217210
)
218-
response = self.view(request)
219211
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
220212

221213

222214
class BulkCreationTestCase(TestCase):
223215

224-
def setUp(self):
225-
self.rf = RequestFactory()
226-
self.view = GroupViewSet.as_view({'post': 'create'})
227-
228216
def test_post_single(self):
229217
"""
230218
Test that POST request with single resource only creates a single
231219
resource.
232220
"""
233221
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')
237224
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
238225
self.assertEqual(1, Group.objects.all().count())
239226

@@ -244,12 +231,11 @@ def test_post_bulk_from_resource_plural_name(self):
244231
{'name': 'bar', 'random_input': [4, 5, 6]},
245232
]
246233
}
247-
request = self.rf.post(
234+
response = self.client.post(
248235
'/groups/',
249236
json.dumps(data),
250237
content_type='application/json'
251238
)
252-
response = self.view(request)
253239
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
254240
self.assertEqual(2, Group.objects.all().count())
255241

@@ -268,12 +254,11 @@ def test_post_bulk_from_list(self):
268254
'random_input': [4, 5, 6],
269255
}
270256
]
271-
request = self.rf.post(
257+
response = self.client.post(
272258
'/groups/',
273259
json.dumps(data),
274260
content_type='application/json'
275261
)
276-
response = self.view(request)
277262
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
278263
self.assertEqual(2, Group.objects.all().count())
279264
self.assertEqual(
@@ -284,12 +269,11 @@ def test_post_bulk_from_list(self):
284269
def test_post_bulk_with_existing_items_and_disabled_partial_creation(self):
285270
data = [{'name': 'foo'}, {'name': 'bar'}]
286271
Group.objects.create(name='foo')
287-
request = self.rf.post(
272+
response = self.client.post(
288273
'/groups/',
289274
json.dumps(data),
290275
content_type='application/json'
291276
)
292-
response = self.view(request)
293277
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
294278
self.assertEqual(1, Group.objects.all().count())
295279
self.assertTrue('errors' in response.data)
@@ -306,12 +290,11 @@ def test_post_bulk_with_sideloaded_results(self):
306290
'members': [u2.pk],
307291
}
308292
]
309-
request = self.rf.post(
293+
response = self.client.post(
310294
'/groups/?include[]=members.',
311295
json.dumps(data),
312296
content_type='application/json'
313297
)
314-
response = self.view(request)
315298
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
316299
resp_data = response.data
317300

0 commit comments

Comments
 (0)