Completed test coverage for django.urls.utils.get_callable().

This commit is contained in:
Hasan Ramezani 2018-07-20 01:18:23 +04:30 committed by Tim Graham
parent 0adfba968e
commit 55b6f7af0c
1 changed files with 34 additions and 9 deletions

View File

@ -1137,20 +1137,45 @@ class ErroneousViewTests(SimpleTestCase):
class ViewLoadingTests(SimpleTestCase): class ViewLoadingTests(SimpleTestCase):
def test_view_loading(self): def test_view_loading(self):
self.assertEqual(get_callable('urlpatterns_reverse.views.empty_view'), empty_view) self.assertEqual(get_callable('urlpatterns_reverse.views.empty_view'), empty_view)
# passing a callable should return the callable
self.assertEqual(get_callable(empty_view), empty_view) self.assertEqual(get_callable(empty_view), empty_view)
def test_exceptions(self): def test_view_does_not_exist(self):
# A missing view (identified by an AttributeError) should raise msg = "View does not exist in module urlpatterns_reverse.views."
# ViewDoesNotExist, ... with self.assertRaisesMessage(ViewDoesNotExist, msg):
with self.assertRaisesMessage(ViewDoesNotExist, "View does not exist in"):
get_callable('urlpatterns_reverse.views.i_should_not_exist') get_callable('urlpatterns_reverse.views.i_should_not_exist')
# ... but if the AttributeError is caused by something else don't
# swallow it. def test_attributeerror_not_hidden(self):
with self.assertRaises(AttributeError): msg = 'I am here to confuse django.urls.get_callable'
with self.assertRaisesMessage(AttributeError, msg):
get_callable('urlpatterns_reverse.views_broken.i_am_broken') get_callable('urlpatterns_reverse.views_broken.i_am_broken')
def test_non_string_value(self):
msg = "'1' is not a callable or a dot-notation path"
with self.assertRaisesMessage(ViewDoesNotExist, msg):
get_callable(1)
def test_string_without_dot(self):
msg = "Could not import 'test'. The path must be fully qualified."
with self.assertRaisesMessage(ImportError, msg):
get_callable('test')
def test_module_does_not_exist(self):
with self.assertRaisesMessage(ImportError, "No module named 'foo'"):
get_callable('foo.bar')
def test_parent_module_does_not_exist(self):
msg = 'Parent module urlpatterns_reverse.foo does not exist.'
with self.assertRaisesMessage(ViewDoesNotExist, msg):
get_callable('urlpatterns_reverse.foo.bar')
def test_not_callable(self):
msg = (
"Could not import 'urlpatterns_reverse.tests.resolve_test_data'. "
"View is not callable."
)
with self.assertRaisesMessage(ViewDoesNotExist, msg):
get_callable('urlpatterns_reverse.tests.resolve_test_data')
class IncludeTests(SimpleTestCase): class IncludeTests(SimpleTestCase):
url_patterns = [ url_patterns = [