Skip to content

Commit

Permalink
Fixes attribute error on patch_list
Browse files Browse the repository at this point in the history
Also adds tests for a couple untested PATCH scenarios:

* Updating an existing resource
* Fallback creation of a resource with a non-existent `resource_uri`
  • Loading branch information
ipmb authored and toastdriven committed Mar 30, 2012
1 parent 858a182 commit 30f1a36
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 0 additions & 1 deletion tastypie/resources.py
Expand Up @@ -1293,7 +1293,6 @@ def patch_list(self, request, **kwargs):
# so this is a create-by-PUT equivalent.
data = self.alter_deserialized_detail_data(request, data)
bundle = self.build_bundle(data=dict_strip_unicode_keys(data))
bundle.obj.pk = obj.pk
self.obj_create(bundle, request=request)
else:
# There's no resource URI, so this is a create call just
Expand Down
21 changes: 20 additions & 1 deletion tests/core/tests/resources.py
Expand Up @@ -1816,7 +1816,7 @@ def test_patch_list(self):
request._read_started = False

self.assertEqual(Note.objects.count(), 6)
request._raw_post_data = request._body = '{"objects": [{"content": "The cat is back. The dog coughed him up out back.", "created": "2010-04-03 20:05:00", "is_active": true, "slug": "cat-is-back-again", "title": "The Cat Is Back", "updated": "2010-04-03 20:05:00"}], "deleted_objects": ["/api/v1/notes/1/"]}'
request._raw_post_data = request._body = '{"objects": [{"content": "The cat is back. The dog coughed him up out back.", "created": "2010-04-03 20:05:00", "is_active": true, "slug": "cat-is-back-again", "title": "The Cat Is Back", "updated": "2010-04-03 20:05:00"}, {"resource_uri": "/api/v1/notes/2/", "content": "This is note 2."}], "deleted_objects": ["/api/v1/notes/1/"]}'

resp = resource.patch_list(request)
self.assertEqual(resp.status_code, 202)
Expand All @@ -1825,6 +1825,25 @@ def test_patch_list(self):
self.assertEqual(Note.objects.filter(is_active=True).count(), 4)
new_note = Note.objects.get(slug='cat-is-back-again')
self.assertEqual(new_note.content, "The cat is back. The dog coughed him up out back.")
updated_note = Note.objects.get(pk=2)
self.assertEqual(updated_note.content, "This is note 2.")

def test_patch_list_bad_resource_uri(self):
resource = NoteResource()
request = HttpRequest()
request.GET = {'format': 'json'}
request.method = 'PATCH'
request._read_started = False

self.assertEqual(Note.objects.count(), 6)
request._raw_post_data = request._body = '{"objects": [{"resource_uri": "/api/v1/notes/99999/", "content": "This is an invalid resource_uri", "created": "2010-04-03 20:05:00", "is_active": true, "slug": "invalid-uri", "title": "Invalid URI", "updated": "2010-04-03 20:05:00"}]}'

resp = resource.patch_list(request)
self.assertEqual(resp.status_code, 202)
self.assertEqual(resp.content, '')
self.assertEqual(Note.objects.count(), 7)
new_note = Note.objects.get(slug='invalid-uri')
self.assertEqual(new_note.content, "This is an invalid resource_uri")

def test_patch_detail(self):
self.assertEqual(Note.objects.count(), 6)
Expand Down

0 comments on commit 30f1a36

Please sign in to comment.