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
1b6fe9ab
Commit
1b6fe9ab
authored
Oct 30, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Hyperlinked serializer tests
parent
51a53b49
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
1 deletions
+43
-1
rest_framework/tests/hyperlinkedserializers.py
+43
-1
No files found.
rest_framework/tests/hyperlinkedserializers.py
View file @
1b6fe9ab
...
@@ -2,11 +2,19 @@ from django.conf.urls.defaults import patterns, url
...
@@ -2,11 +2,19 @@ from django.conf.urls.defaults import patterns, url
from
django.test
import
TestCase
from
django.test
import
TestCase
from
django.test.client
import
RequestFactory
from
django.test.client
import
RequestFactory
from
rest_framework
import
generics
,
status
,
serializers
from
rest_framework
import
generics
,
status
,
serializers
from
rest_framework.tests.models
import
Anchor
,
BasicModel
,
ManyToManyModel
from
rest_framework.tests.models
import
Anchor
,
BasicModel
,
ManyToManyModel
,
BlogPost
,
BlogPostComment
factory
=
RequestFactory
()
factory
=
RequestFactory
()
class
BlogPostCommentSerializer
(
serializers
.
Serializer
):
text
=
serializers
.
CharField
()
blog_post_url
=
serializers
.
HyperlinkedRelatedField
(
source
=
'blog_post'
,
view_name
=
'blogpost-detail'
,
queryset
=
BlogPost
.
objects
.
all
())
def
restore_object
(
self
,
attrs
,
instance
=
None
):
return
BlogPostComment
(
**
attrs
)
class
BasicList
(
generics
.
ListCreateAPIView
):
class
BasicList
(
generics
.
ListCreateAPIView
):
model
=
BasicModel
model
=
BasicModel
model_serializer_class
=
serializers
.
HyperlinkedModelSerializer
model_serializer_class
=
serializers
.
HyperlinkedModelSerializer
...
@@ -32,12 +40,22 @@ class ManyToManyDetail(generics.RetrieveAPIView):
...
@@ -32,12 +40,22 @@ class ManyToManyDetail(generics.RetrieveAPIView):
model_serializer_class
=
serializers
.
HyperlinkedModelSerializer
model_serializer_class
=
serializers
.
HyperlinkedModelSerializer
class
BlogPostCommentListCreate
(
generics
.
ListCreateAPIView
):
model
=
BlogPostComment
model_serializer_class
=
BlogPostCommentSerializer
class
BlogPostDetail
(
generics
.
RetrieveAPIView
):
model
=
BlogPost
urlpatterns
=
patterns
(
''
,
urlpatterns
=
patterns
(
''
,
url
(
r'^basic/$'
,
BasicList
.
as_view
(),
name
=
'basicmodel-list'
),
url
(
r'^basic/$'
,
BasicList
.
as_view
(),
name
=
'basicmodel-list'
),
url
(
r'^basic/(?P<pk>\d+)/$'
,
BasicDetail
.
as_view
(),
name
=
'basicmodel-detail'
),
url
(
r'^basic/(?P<pk>\d+)/$'
,
BasicDetail
.
as_view
(),
name
=
'basicmodel-detail'
),
url
(
r'^anchor/(?P<pk>\d+)/$'
,
AnchorDetail
.
as_view
(),
name
=
'anchor-detail'
),
url
(
r'^anchor/(?P<pk>\d+)/$'
,
AnchorDetail
.
as_view
(),
name
=
'anchor-detail'
),
url
(
r'^manytomany/$'
,
ManyToManyList
.
as_view
(),
name
=
'manytomanymodel-list'
),
url
(
r'^manytomany/$'
,
ManyToManyList
.
as_view
(),
name
=
'manytomanymodel-list'
),
url
(
r'^manytomany/(?P<pk>\d+)/$'
,
ManyToManyDetail
.
as_view
(),
name
=
'manytomanymodel-detail'
),
url
(
r'^manytomany/(?P<pk>\d+)/$'
,
ManyToManyDetail
.
as_view
(),
name
=
'manytomanymodel-detail'
),
url
(
r'^posts/(?P<pk>\d+)/$'
,
BlogPostDetail
.
as_view
(),
name
=
'blogpost-detail'
),
url
(
r'^comments/$'
,
BlogPostCommentListCreate
.
as_view
(),
name
=
'blogpostcomment-list'
)
)
)
...
@@ -124,3 +142,27 @@ class TestManyToManyHyperlinkedView(TestCase):
...
@@ -124,3 +142,27 @@ class TestManyToManyHyperlinkedView(TestCase):
response
=
self
.
detail_view
(
request
,
pk
=
1
)
.
render
()
response
=
self
.
detail_view
(
request
,
pk
=
1
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEquals
(
response
.
data
,
self
.
data
[
0
])
self
.
assertEquals
(
response
.
data
,
self
.
data
[
0
])
class
TestCreateWithForeignKeys
(
TestCase
):
urls
=
'rest_framework.tests.hyperlinkedserializers'
def
setUp
(
self
):
"""
Create a blog post
"""
self
.
post
=
BlogPost
.
objects
.
create
(
title
=
"Test post"
)
self
.
create_view
=
BlogPostCommentListCreate
.
as_view
()
def
test_create_comment
(
self
):
data
=
{
'text'
:
'A test comment'
,
'blog_post_url'
:
'/posts/1/'
}
request
=
factory
.
post
(
'/comments/'
,
data
=
data
)
response
=
self
.
create_view
(
request
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
201
)
self
.
assertEqual
(
self
.
post
.
blogpostcomment_set
.
count
(),
1
)
self
.
assertEqual
(
self
.
post
.
blogpostcomment_set
.
all
()[
0
]
.
text
,
'A test comment'
)
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