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
ea8c4052
Commit
ea8c4052
authored
Nov 03, 2014
by
tanwanirahul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tests for validating custom_method_name router attribute
parent
d972df7c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
6 deletions
+26
-6
tests/test_routers.py
+26
-6
No files found.
tests/test_routers.py
View file @
ea8c4052
...
...
@@ -8,6 +8,7 @@ from rest_framework.decorators import detail_route, list_route
from
rest_framework.response
import
Response
from
rest_framework.routers
import
SimpleRouter
,
DefaultRouter
from
rest_framework.test
import
APIRequestFactory
from
collections
import
namedtuple
factory
=
APIRequestFactory
()
...
...
@@ -260,6 +261,14 @@ class DynamicListAndDetailViewSet(viewsets.ViewSet):
def
detail_route_get
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'link2'
})
@list_route
(
custom_method_name
=
"list_custom-route"
)
def
list_custom_route_get
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'link1'
})
@detail_route
(
custom_method_name
=
"detail_custom-route"
)
def
detail_custom_route_get
(
self
,
request
,
*
args
,
**
kwargs
):
return
Response
({
'method'
:
'link2'
})
class
TestDynamicListAndDetailRouter
(
TestCase
):
def
setUp
(
self
):
...
...
@@ -268,22 +277,33 @@ class TestDynamicListAndDetailRouter(TestCase):
def
test_list_and_detail_route_decorators
(
self
):
routes
=
self
.
router
.
get_routes
(
DynamicListAndDetailViewSet
)
decorator_routes
=
[
r
for
r
in
routes
if
not
(
r
.
name
.
endswith
(
'-list'
)
or
r
.
name
.
endswith
(
'-detail'
))]
MethodNamesMap
=
namedtuple
(
'MethodNamesMap'
,
'method_name custom_method_name'
)
# Make sure all these endpoints exist and none have been clobbered
for
i
,
endpoint
in
enumerate
([
'list_route_get'
,
'list_route_post'
,
'detail_route_get'
,
'detail_route_post'
]):
for
i
,
endpoint
in
enumerate
([
MethodNamesMap
(
'list_custom_route_get'
,
'list_custom-route'
),
MethodNamesMap
(
'list_route_get'
,
'list_route_get'
),
MethodNamesMap
(
'list_route_post'
,
'list_route_post'
),
MethodNamesMap
(
'detail_custom_route_get'
,
'detail_custom-route'
),
MethodNamesMap
(
'detail_route_get'
,
'detail_route_get'
),
MethodNamesMap
(
'detail_route_post'
,
'detail_route_post'
)
]):
route
=
decorator_routes
[
i
]
# check url listing
if
endpoint
.
startswith
(
'list_'
):
method_name
=
endpoint
.
method_name
custom_method_name
=
endpoint
.
custom_method_name
if
method_name
.
startswith
(
'list_'
):
self
.
assertEqual
(
route
.
url
,
'^{{prefix}}/{0}{{trailing_slash}}$'
.
format
(
endpoint
))
'^{{prefix}}/{0}{{trailing_slash}}$'
.
format
(
custom_method_name
))
else
:
self
.
assertEqual
(
route
.
url
,
'^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'
.
format
(
endpoint
))
'^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'
.
format
(
custom_method_name
))
# check method to function mapping
if
endpoint
.
endswith
(
'_post'
):
if
method_name
.
endswith
(
'_post'
):
method_map
=
'post'
else
:
method_map
=
'get'
self
.
assertEqual
(
route
.
mapping
[
method_map
],
endpoint
)
self
.
assertEqual
(
route
.
mapping
[
method_map
],
method_name
)
class
TestRootWithAListlessViewset
(
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