Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
c32d9969
Commit
c32d9969
authored
Mar 22, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add extra tests for errors from incorrect data with multiple create/update
parent
b2dc6644
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
15 deletions
+81
-15
rest_framework/serializers.py
+22
-14
rest_framework/tests/serializer_bulk_update.py
+59
-1
No files found.
rest_framework/serializers.py
View file @
c32d9969
...
@@ -371,22 +371,30 @@ class BaseSerializer(Field):
...
@@ -371,22 +371,30 @@ class BaseSerializer(Field):
identities
=
[
self
.
get_identity
(
self
.
to_native
(
obj
))
for
obj
in
objects
]
identities
=
[
self
.
get_identity
(
self
.
to_native
(
obj
))
for
obj
in
objects
]
identity_to_objects
=
dict
(
zip
(
identities
,
objects
))
identity_to_objects
=
dict
(
zip
(
identities
,
objects
))
for
item
in
data
:
try
:
if
update
:
iter
(
data
)
# Determine which object we're updating
if
isinstance
(
data
,
dict
):
try
:
raise
TypeError
identity
=
self
.
get_identity
(
item
)
except
TypeError
:
except
:
self
.
_errors
=
{
'non_field_errors'
:
[
'Expected a list of items'
]}
self
.
object
=
None
else
:
else
:
for
item
in
data
:
self
.
object
=
identity_to_objects
.
pop
(
identity
,
None
)
if
update
:
# Determine which object we're updating
try
:
identity
=
self
.
get_identity
(
item
)
except
:
self
.
object
=
None
else
:
self
.
object
=
identity_to_objects
.
pop
(
identity
,
None
)
ret
.
append
(
self
.
from_native
(
item
,
None
))
errors
.
append
(
self
.
_errors
)
ret
.
append
(
self
.
from_native
(
item
,
None
))
if
update
:
errors
.
append
(
self
.
_errors
)
self
.
_deleted
=
identity_to_objects
.
values
(
)
if
update
:
self
.
_errors
=
any
(
errors
)
and
errors
or
[]
self
.
_deleted
=
identity_to_objects
.
values
()
self
.
_errors
=
any
(
errors
)
and
errors
or
[]
else
:
else
:
ret
=
self
.
from_native
(
data
,
files
)
ret
=
self
.
from_native
(
data
,
files
)
...
...
rest_framework/tests/serializer_bulk_update.py
View file @
c32d9969
...
@@ -7,6 +7,9 @@ from rest_framework import serializers
...
@@ -7,6 +7,9 @@ from rest_framework import serializers
class
BulkCreateSerializerTests
(
TestCase
):
class
BulkCreateSerializerTests
(
TestCase
):
"""
Creating multiple instances using serializers.
"""
def
setUp
(
self
):
def
setUp
(
self
):
class
BookSerializer
(
serializers
.
Serializer
):
class
BookSerializer
(
serializers
.
Serializer
):
...
@@ -71,11 +74,63 @@ class BulkCreateSerializerTests(TestCase):
...
@@ -71,11 +74,63 @@ class BulkCreateSerializerTests(TestCase):
self
.
assertEqual
(
serializer
.
is_valid
(),
False
)
self
.
assertEqual
(
serializer
.
is_valid
(),
False
)
self
.
assertEqual
(
serializer
.
errors
,
expected_errors
)
self
.
assertEqual
(
serializer
.
errors
,
expected_errors
)
def
test_invalid_list_datatype
(
self
):
"""
Data containing list of incorrect data type should return errors.
"""
data
=
[
'foo'
,
'bar'
,
'baz'
]
serializer
=
self
.
BookSerializer
(
data
=
data
,
many
=
True
)
self
.
assertEqual
(
serializer
.
is_valid
(),
False
)
expected_errors
=
[
{
'non_field_errors'
:
[
'Invalid data'
]},
{
'non_field_errors'
:
[
'Invalid data'
]},
{
'non_field_errors'
:
[
'Invalid data'
]}
]
self
.
assertEqual
(
serializer
.
errors
,
expected_errors
)
def
test_invalid_single_datatype
(
self
):
"""
Data containing a single incorrect data type should return errors.
"""
data
=
123
serializer
=
self
.
BookSerializer
(
data
=
data
,
many
=
True
)
self
.
assertEqual
(
serializer
.
is_valid
(),
False
)
expected_errors
=
{
'non_field_errors'
:
[
'Expected a list of items'
]}
self
.
assertEqual
(
serializer
.
errors
,
expected_errors
)
def
test_invalid_single_object
(
self
):
"""
Data containing only a single object, instead of a list of objects
should return errors.
"""
data
=
{
'id'
:
0
,
'title'
:
'The electric kool-aid acid test'
,
'author'
:
'Tom Wolfe'
}
serializer
=
self
.
BookSerializer
(
data
=
data
,
many
=
True
)
self
.
assertEqual
(
serializer
.
is_valid
(),
False
)
expected_errors
=
{
'non_field_errors'
:
[
'Expected a list of items'
]}
self
.
assertEqual
(
serializer
.
errors
,
expected_errors
)
class
BulkUpdateSerializerTests
(
TestCase
):
class
BulkUpdateSerializerTests
(
TestCase
):
"""
Updating multiple instances using serializers.
"""
def
setUp
(
self
):
def
setUp
(
self
):
class
Book
(
object
):
class
Book
(
object
):
"""
A data type that can be persisted to a mock storage backend
with `.save()` and `.delete()`.
"""
object_map
=
{}
object_map
=
{}
def
__init__
(
self
,
id
,
title
,
author
):
def
__init__
(
self
,
id
,
title
,
author
):
...
@@ -126,6 +181,9 @@ class BulkUpdateSerializerTests(TestCase):
...
@@ -126,6 +181,9 @@ class BulkUpdateSerializerTests(TestCase):
book
.
save
()
book
.
save
()
def
books
(
self
):
def
books
(
self
):
"""
Return all the objects in the mock storage backend.
"""
return
self
.
Book
.
object_map
.
values
()
return
self
.
Book
.
object_map
.
values
()
def
test_bulk_update_success
(
self
):
def
test_bulk_update_success
(
self
):
...
@@ -152,7 +210,7 @@ class BulkUpdateSerializerTests(TestCase):
...
@@ -152,7 +210,7 @@ class BulkUpdateSerializerTests(TestCase):
def
test_bulk_update_error
(
self
):
def
test_bulk_update_error
(
self
):
"""
"""
Correct bulk update serialization should return the input
data.
Incorrect bulk update serialization should return error
data.
"""
"""
data
=
[
data
=
[
{
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment