From 0186b073bff2e26e373b0862873e1cf40dbdbe62 Mon Sep 17 00:00:00 2001 From: Artem Muterko Date: Thu, 19 Jan 2017 18:00:07 +0200 Subject: [PATCH] Refactor router tests to pytest style --- tests/test_routers.py | 92 +++++++++++-------------------------------- 1 file changed, 24 insertions(+), 68 deletions(-) diff --git a/tests/test_routers.py b/tests/test_routers.py index d28e301a0..99e4391c0 100644 --- a/tests/test_routers.py +++ b/tests/test_routers.py @@ -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('', detail_url_pattern) + assert '' 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[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', detail_url_pattern) + assert '^notes/(?P' 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[^/.]+)/$'] 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[^/.]+)$'] 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.assertRaises(ImproperlyConfigured): + with pytest.raises(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'}