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