Add tests also for default router with path

This commit is contained in:
sevdog 2022-11-21 18:05:24 +01:00
parent 06a9244bc9
commit ff69c69658

View File

@ -96,6 +96,9 @@ notes_router.register(r'notes', NoteViewSet)
notes_path_router = SimpleRouter(use_regex_path=False) notes_path_router = SimpleRouter(use_regex_path=False)
notes_path_router.register('notes', NoteViewSet) notes_path_router.register('notes', NoteViewSet)
notes_path_default_router = DefaultRouter(use_regex_path=False)
notes_path_default_router.register('notes', NoteViewSet)
kwarged_notes_router = SimpleRouter() kwarged_notes_router = SimpleRouter()
kwarged_notes_router.register(r'notes', KWargedNoteViewSet) kwarged_notes_router.register(r'notes', KWargedNoteViewSet)
@ -484,7 +487,8 @@ class TestUrlPath(URLPatternsTestCase, TestCase):
client_class = APIClient client_class = APIClient
urlpatterns = [ urlpatterns = [
path('path/', include(url_path_router.urls)), path('path/', include(url_path_router.urls)),
path('example/', include(notes_path_router.urls)) path('default/', include(notes_path_default_router.urls)),
path('example/', include(notes_path_router.urls)),
] ]
def setUp(self): def setUp(self):
@ -503,17 +507,23 @@ class TestUrlPath(URLPatternsTestCase, TestCase):
assert RouterTestModel.objects.filter(uuid='foo').exists() assert RouterTestModel.objects.filter(uuid='foo').exists()
def test_retrieve(self): def test_retrieve(self):
response = self.client.get('/example/notes/123/') for url in ('/example/notes/123/', '/default/notes/123/'):
assert response.status_code == 200 with self.subTest(url=url):
assert response.data == {"url": "http://testserver/example/notes/123/", "uuid": "123", "text": "foo bar"} response = self.client.get(url)
assert response.status_code == 200
# only gets example path since was the last to be registered
assert response.data == {"url": "http://testserver/example/notes/123/", "uuid": "123", "text": "foo bar"}
def test_list(self): def test_list(self):
response = self.client.get('/example/notes/') for url in ('/example/notes/', '/default/notes/'):
assert response.status_code == 200 with self.subTest(url=url):
assert response.data == [ response = self.client.get(url)
{"url": "http://testserver/example/notes/123/", "uuid": "123", "text": "foo bar"}, assert response.status_code == 200
{"url": "http://testserver/example/notes/a%20b/", "uuid": "a b", "text": "baz qux"}, # only gets example path since was the last to be registered
] assert response.data == [
{"url": "http://testserver/example/notes/123/", "uuid": "123", "text": "foo bar"},
{"url": "http://testserver/example/notes/a%20b/", "uuid": "a b", "text": "baz qux"},
]
def test_update(self): def test_update(self):
updated_note = { updated_note = {
@ -541,6 +551,12 @@ class TestUrlPath(URLPatternsTestCase, TestCase):
assert response.status_code == 200 assert response.status_code == 200
assert json.loads(response.content.decode()) == {'pk': pk, 'kwarg': kwarg} assert json.loads(response.content.decode()) == {'pk': pk, 'kwarg': kwarg}
def test_defaultrouter_root(self):
response = self.client.get('/default/')
assert response.status_code == 200
# only gets example path since was the last to be registered
assert response.data == {"notes": "http://testserver/example/notes/"}
class TestViewInitkwargs(URLPatternsTestCase, TestCase): class TestViewInitkwargs(URLPatternsTestCase, TestCase):
urlpatterns = [ urlpatterns = [