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
d70e387f
Commit
d70e387f
authored
Oct 18, 2012
by
Ian Strachan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added tests for dotted notation in serializer field source
parent
c3417993
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
4 deletions
+23
-4
rest_framework/tests/serializer.py
+23
-4
No files found.
rest_framework/tests/serializer.py
View file @
d70e387f
...
...
@@ -4,6 +4,11 @@ from rest_framework import serializers
from
rest_framework.tests.models
import
*
class
SubComment
(
object
):
def
__init__
(
self
,
sub_comment
):
self
.
sub_comment
=
sub_comment
class
Comment
(
object
):
def
__init__
(
self
,
email
,
content
,
created
):
self
.
email
=
email
...
...
@@ -14,11 +19,16 @@ class Comment(object):
return
all
([
getattr
(
self
,
attr
)
==
getattr
(
other
,
attr
)
for
attr
in
(
'email'
,
'content'
,
'created'
)])
def
get_sub_comment
(
self
):
sub_comment
=
SubComment
(
'And Merry Christmas!'
)
return
sub_comment
class
CommentSerializer
(
serializers
.
Serializer
):
email
=
serializers
.
EmailField
()
content
=
serializers
.
CharField
(
max_length
=
1000
)
created
=
serializers
.
DateTimeField
()
sub_comment
=
serializers
.
Field
(
source
=
'get_sub_comment.sub_comment'
)
def
restore_object
(
self
,
data
,
instance
=
None
):
if
instance
is
None
:
...
...
@@ -42,7 +52,14 @@ class BasicTests(TestCase):
self
.
data
=
{
'email'
:
'tom@example.com'
,
'content'
:
'Happy new year!'
,
'created'
:
datetime
.
datetime
(
2012
,
1
,
1
)
'created'
:
datetime
.
datetime
(
2012
,
1
,
1
),
'sub_comment'
:
'This wont change'
}
self
.
expected
=
{
'email'
:
'tom@example.com'
,
'content'
:
'Happy new year!'
,
'created'
:
datetime
.
datetime
(
2012
,
1
,
1
),
'sub_comment'
:
'And Merry Christmas!'
}
def
test_empty
(
self
):
...
...
@@ -50,14 +67,14 @@ class BasicTests(TestCase):
expected
=
{
'email'
:
''
,
'content'
:
''
,
'created'
:
None
'created'
:
None
,
'sub_comment'
:
''
}
self
.
assertEquals
(
serializer
.
data
,
expected
)
def
test_retrieve
(
self
):
serializer
=
CommentSerializer
(
instance
=
self
.
comment
)
expected
=
self
.
data
self
.
assertEquals
(
serializer
.
data
,
expected
)
self
.
assertEquals
(
serializer
.
data
,
self
.
expected
)
def
test_create
(
self
):
serializer
=
CommentSerializer
(
self
.
data
)
...
...
@@ -65,6 +82,7 @@ class BasicTests(TestCase):
self
.
assertEquals
(
serializer
.
is_valid
(),
True
)
self
.
assertEquals
(
serializer
.
object
,
expected
)
self
.
assertFalse
(
serializer
.
object
is
expected
)
self
.
assertEquals
(
serializer
.
data
[
'sub_comment'
],
'And Merry Christmas!'
)
def
test_update
(
self
):
serializer
=
CommentSerializer
(
self
.
data
,
instance
=
self
.
comment
)
...
...
@@ -72,6 +90,7 @@ class BasicTests(TestCase):
self
.
assertEquals
(
serializer
.
is_valid
(),
True
)
self
.
assertEquals
(
serializer
.
object
,
expected
)
self
.
assertTrue
(
serializer
.
object
is
expected
)
self
.
assertEquals
(
serializer
.
data
[
'sub_comment'
],
'And Merry Christmas!'
)
class
ValidationTests
(
TestCase
):
...
...
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