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
0186b073
Commit
0186b073
authored
Jan 19, 2017
by
Artem Muterko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor router tests to pytest style
parent
635dc035
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
68 deletions
+24
-68
tests/test_routers.py
+24
-68
No files found.
tests/test_routers.py
View file @
0186b073
...
...
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
import
json
from
collections
import
namedtuple
import
pytest
from
django.conf.urls
import
include
,
url
from
django.core.exceptions
import
ImproperlyConfigured
from
django.db
import
models
...
...
@@ -124,8 +125,7 @@ class TestSimpleRouter(TestCase):
for
i
,
endpoint
in
enumerate
([
'action1'
,
'action2'
,
'action3'
,
'link1'
,
'link2'
]):
route
=
decorator_routes
[
i
]
# check url listing
self
.
assertEqual
(
route
.
url
,
'^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'
.
format
(
endpoint
))
assert
route
.
url
==
'^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'
.
format
(
endpoint
)
# check method to function mapping
if
endpoint
==
'action3'
:
methods_map
=
[
'post'
,
'delete'
]
...
...
@@ -134,28 +134,18 @@ class TestSimpleRouter(TestCase):
else
:
methods_map
=
[
'get'
]
for
method
in
methods_map
:
self
.
assertEqual
(
route
.
mapping
[
method
],
endpoint
)
assert
route
.
mapping
[
method
]
==
endpoint
@override_settings
(
ROOT_URLCONF
=
'tests.test_routers'
)
class
TestRootView
(
TestCase
):
def
test_retrieve_namespaced_root
(
self
):
response
=
self
.
client
.
get
(
'/namespaced/'
)
self
.
assertEqual
(
response
.
data
,
{
"example"
:
"http://testserver/namespaced/example/"
,
}
)
assert
response
.
data
==
{
"example"
:
"http://testserver/namespaced/example/"
}
def
test_retrieve_non_namespaced_root
(
self
):
response
=
self
.
client
.
get
(
'/non-namespaced/'
)
self
.
assertEqual
(
response
.
data
,
{
"example"
:
"http://testserver/non-namespaced/example/"
,
}
)
assert
response
.
data
==
{
"example"
:
"http://testserver/non-namespaced/example/"
}
@override_settings
(
ROOT_URLCONF
=
'tests.test_routers'
)
...
...
@@ -169,27 +159,15 @@ class TestCustomLookupFields(TestCase):
def
test_custom_lookup_field_route
(
self
):
detail_route
=
notes_router
.
urls
[
-
1
]
detail_url_pattern
=
detail_route
.
regex
.
pattern
self
.
assertIn
(
'<uuid>'
,
detail_url_pattern
)
assert
'<uuid>'
in
detail_url_pattern
def
test_retrieve_lookup_field_list_view
(
self
):
response
=
self
.
client
.
get
(
'/example/notes/'
)
self
.
assertEqual
(
response
.
data
,
[{
"url"
:
"http://testserver/example/notes/123/"
,
"uuid"
:
"123"
,
"text"
:
"foo bar"
}]
)
assert
response
.
data
==
[{
"url"
:
"http://testserver/example/notes/123/"
,
"uuid"
:
"123"
,
"text"
:
"foo bar"
}]
def
test_retrieve_lookup_field_detail_view
(
self
):
response
=
self
.
client
.
get
(
'/example/notes/123/'
)
self
.
assertEqual
(
response
.
data
,
{
"url"
:
"http://testserver/example/notes/123/"
,
"uuid"
:
"123"
,
"text"
:
"foo bar"
}
)
assert
response
.
data
==
{
"url"
:
"http://testserver/example/notes/123/"
,
"uuid"
:
"123"
,
"text"
:
"foo bar"
}
class
TestLookupValueRegex
(
TestCase
):
...
...
@@ -210,7 +188,7 @@ class TestLookupValueRegex(TestCase):
def
test_urls_limited_by_lookup_value_regex
(
self
):
expected
=
[
'^notes/$'
,
'^notes/(?P<uuid>[0-9a-f]{32})/$'
]
for
idx
in
range
(
len
(
expected
)):
self
.
assertEqual
(
expected
[
idx
],
self
.
urls
[
idx
]
.
regex
.
pattern
)
assert
expected
[
idx
]
==
self
.
urls
[
idx
]
.
regex
.
pattern
@override_settings
(
ROOT_URLCONF
=
'tests.test_routers'
)
...
...
@@ -226,17 +204,11 @@ class TestLookupUrlKwargs(TestCase):
def
test_custom_lookup_url_kwarg_route
(
self
):
detail_route
=
kwarged_notes_router
.
urls
[
-
1
]
detail_url_pattern
=
detail_route
.
regex
.
pattern
self
.
assertIn
(
'^notes/(?P<text>'
,
detail_url_pattern
)
assert
'^notes/(?P<text>'
in
detail_url_pattern
def
test_retrieve_lookup_url_kwarg_detail_view
(
self
):
response
=
self
.
client
.
get
(
'/example2/notes/fo/'
)
self
.
assertEqual
(
response
.
data
,
{
"url"
:
"http://testserver/example/notes/123/"
,
"uuid"
:
"123"
,
"text"
:
"foo bar"
}
)
assert
response
.
data
==
{
"url"
:
"http://testserver/example/notes/123/"
,
"uuid"
:
"123"
,
"text"
:
"foo bar"
}
class
TestTrailingSlashIncluded
(
TestCase
):
...
...
@@ -251,7 +223,7 @@ class TestTrailingSlashIncluded(TestCase):
def
test_urls_have_trailing_slash_by_default
(
self
):
expected
=
[
'^notes/$'
,
'^notes/(?P<pk>[^/.]+)/$'
]
for
idx
in
range
(
len
(
expected
)):
self
.
assertEqual
(
expected
[
idx
],
self
.
urls
[
idx
]
.
regex
.
pattern
)
assert
expected
[
idx
]
==
self
.
urls
[
idx
]
.
regex
.
pattern
class
TestTrailingSlashRemoved
(
TestCase
):
...
...
@@ -266,7 +238,7 @@ class TestTrailingSlashRemoved(TestCase):
def
test_urls_can_have_trailing_slash_removed
(
self
):
expected
=
[
'^notes$'
,
'^notes/(?P<pk>[^/.]+)$'
]
for
idx
in
range
(
len
(
expected
)):
self
.
assertEqual
(
expected
[
idx
],
self
.
urls
[
idx
]
.
regex
.
pattern
)
assert
expected
[
idx
]
==
self
.
urls
[
idx
]
.
regex
.
pattern
class
TestNameableRoot
(
TestCase
):
...
...
@@ -281,7 +253,7 @@ class TestNameableRoot(TestCase):
def
test_router_has_custom_name
(
self
):
expected
=
'nameable-root'
self
.
assertEqual
(
expected
,
self
.
urls
[
-
1
]
.
name
)
assert
expected
==
self
.
urls
[
-
1
]
.
name
class
TestActionKeywordArgs
(
TestCase
):
...
...
@@ -307,10 +279,7 @@ class TestActionKeywordArgs(TestCase):
def
test_action_kwargs
(
self
):
request
=
factory
.
post
(
'/test/0/custom/'
)
response
=
self
.
view
(
request
)
self
.
assertEqual
(
response
.
data
,
{
'permission_classes'
:
[
permissions
.
AllowAny
]}
)
assert
response
.
data
==
{
'permission_classes'
:
[
permissions
.
AllowAny
]}
class
TestActionAppliedToExistingRoute
(
TestCase
):
...
...
@@ -331,7 +300,7 @@ class TestActionAppliedToExistingRoute(TestCase):
self
.
router
=
SimpleRouter
()
self
.
router
.
register
(
r'test'
,
TestViewSet
,
base_name
=
'test'
)
with
self
.
assertR
aises
(
ImproperlyConfigured
):
with
pytest
.
r
aises
(
ImproperlyConfigured
):
self
.
router
.
urls
...
...
@@ -391,17 +360,15 @@ class TestDynamicListAndDetailRouter(TestCase):
url_path
=
endpoint
.
url_path
if
method_name
.
startswith
(
'list_'
):
self
.
assertEqual
(
route
.
url
,
'^{{prefix}}/{0}{{trailing_slash}}$'
.
format
(
url_path
))
assert
route
.
url
==
'^{{prefix}}/{0}{{trailing_slash}}$'
.
format
(
url_path
)
else
:
self
.
assertEqual
(
route
.
url
,
'^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'
.
format
(
url_path
))
assert
route
.
url
==
'^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'
.
format
(
url_path
)
# check method to function mapping
if
method_name
.
endswith
(
'_post'
):
method_map
=
'post'
else
:
method_map
=
'get'
self
.
assertEqual
(
route
.
mapping
[
method_map
],
method_name
)
assert
route
.
mapping
[
method_map
]
==
method_name
def
test_list_and_detail_route_decorators
(
self
):
self
.
_test_list_and_detail_route_decorators
(
DynamicListAndDetailViewSet
)
...
...
@@ -414,22 +381,11 @@ class TestDynamicListAndDetailRouter(TestCase):
class
TestEmptyPrefix
(
TestCase
):
def
test_empty_prefix_list
(
self
):
response
=
self
.
client
.
get
(
'/empty-prefix/'
)
self
.
assertEqual
(
200
,
response
.
status_code
)
self
.
assertEqual
(
json
.
loads
(
response
.
content
.
decode
(
'utf-8'
)),
[
{
'uuid'
:
'111'
,
'text'
:
'First'
},
{
'uuid'
:
'222'
,
'text'
:
'Second'
}
]
)
assert
response
.
status_code
==
200
assert
json
.
loads
(
response
.
content
.
decode
(
'utf-8'
))
==
[{
'uuid'
:
'111'
,
'text'
:
'First'
},
{
'uuid'
:
'222'
,
'text'
:
'Second'
}]
def
test_empty_prefix_detail
(
self
):
response
=
self
.
client
.
get
(
'/empty-prefix/1/'
)
self
.
assertEqual
(
200
,
response
.
status_code
)
self
.
assertEqual
(
json
.
loads
(
response
.
content
.
decode
(
'utf-8'
)),
{
'uuid'
:
'111'
,
'text'
:
'First'
}
)
assert
response
.
status_code
==
200
assert
json
.
loads
(
response
.
content
.
decode
(
'utf-8'
))
==
{
'uuid'
:
'111'
,
'text'
:
'First'
}
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