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
cf72b9a8
Commit
cf72b9a8
authored
Sep 19, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moar tests
parent
b361c54c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
0 deletions
+35
-0
rest_framework/serializers.py
+2
-0
tests/test_model_serializer.py
+33
-0
No files found.
rest_framework/serializers.py
View file @
cf72b9a8
...
@@ -24,6 +24,7 @@ from rest_framework.utils.field_mapping import (
...
@@ -24,6 +24,7 @@ from rest_framework.utils.field_mapping import (
lookup_class
lookup_class
)
)
import
copy
import
copy
import
inspect
# Note: We do the following so that users of the framework can use this style:
# Note: We do the following so that users of the framework can use this style:
#
#
...
@@ -268,6 +269,7 @@ class ListSerializer(BaseSerializer):
...
@@ -268,6 +269,7 @@ class ListSerializer(BaseSerializer):
def
__init__
(
self
,
*
args
,
**
kwargs
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
child
=
kwargs
.
pop
(
'child'
,
copy
.
deepcopy
(
self
.
child
))
self
.
child
=
kwargs
.
pop
(
'child'
,
copy
.
deepcopy
(
self
.
child
))
assert
self
.
child
is
not
None
,
'`child` is a required argument.'
assert
self
.
child
is
not
None
,
'`child` is a required argument.'
assert
not
inspect
.
isclass
(
self
.
child
),
'`child` has not been instantiated.'
self
.
context
=
kwargs
.
pop
(
'context'
,
{})
self
.
context
=
kwargs
.
pop
(
'context'
,
{})
kwargs
.
pop
(
'partial'
,
None
)
kwargs
.
pop
(
'partial'
,
None
)
...
...
tests/test_model_serializer.py
View file @
cf72b9a8
...
@@ -473,3 +473,36 @@ class TestIntegration(TestCase):
...
@@ -473,3 +473,36 @@ class TestIntegration(TestCase):
'through'
:
[]
'through'
:
[]
}
}
self
.
assertEqual
(
serializer
.
data
,
expected
)
self
.
assertEqual
(
serializer
.
data
,
expected
)
# Tests for bulk create using `ListSerializer`.
class
BulkCreateModel
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
10
)
class
TestBulkCreate
(
TestCase
):
def
test_bulk_create
(
self
):
class
BasicModelSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
BulkCreateModel
fields
=
(
'name'
,)
class
BulkCreateSerializer
(
serializers
.
ListSerializer
):
child
=
BasicModelSerializer
()
data
=
[{
'name'
:
'a'
},
{
'name'
:
'b'
},
{
'name'
:
'c'
}]
serializer
=
BulkCreateSerializer
(
data
=
data
)
assert
serializer
.
is_valid
()
# Objects are returned by save().
instances
=
serializer
.
save
()
assert
len
(
instances
)
==
3
assert
[
item
.
name
for
item
in
instances
]
==
[
'a'
,
'b'
,
'c'
]
# Objects have been created in the database.
assert
BulkCreateModel
.
objects
.
count
()
==
3
assert
list
(
BulkCreateModel
.
objects
.
values_list
(
'name'
,
flat
=
True
))
==
[
'a'
,
'b'
,
'c'
]
# Serializer returns correct data.
assert
serializer
.
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