diff options
| author | Alasdair Nicol <alasdair@thenicols.net> | 2016-04-02 12:49:12 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-04-30 20:09:31 -0400 |
| commit | eb5d7bc2f465b055f4eb54a3d238502bdddb6d7e (patch) | |
| tree | 597da9f4f1a56316ee5f8616d30638604c3f69a8 /tests/check_framework | |
| parent | 914c72be2abb1c6dd860cb9279beaa66409ae1b2 (diff) | |
Fixed #26440 -- Added a warning for non-url()s in urlpatterns.
Thanks Burhan Khalid for the initial patch and knbk/timgraham
for review.
Diffstat (limited to 'tests/check_framework')
| -rw-r--r-- | tests/check_framework/test_urls.py | 30 | ||||
| -rw-r--r-- | tests/check_framework/urls/contains_tuple.py | 3 |
2 files changed, 32 insertions, 1 deletions
diff --git a/tests/check_framework/test_urls.py b/tests/check_framework/test_urls.py index a0c113aac3..a1ac727057 100644 --- a/tests/check_framework/test_urls.py +++ b/tests/check_framework/test_urls.py @@ -1,5 +1,7 @@ from django.conf import settings -from django.core.checks.urls import check_url_config +from django.core.checks.urls import ( + check_url_config, get_warning_for_invalid_pattern, +) from django.test import SimpleTestCase from django.test.utils import override_settings @@ -27,6 +29,16 @@ class CheckUrlsTest(SimpleTestCase): expected_msg = "Your URL pattern '^include-with-dollar$' uses include with a regex ending with a '$'." self.assertIn(expected_msg, warning.msg) + @override_settings(ROOT_URLCONF='check_framework.urls.contains_tuple') + def test_contains_tuple_not_url_instance(self): + result = check_url_config(None) + warning = result[0] + self.assertEqual(warning.id, 'urls.E004') + self.assertRegexpMatches(warning.msg, ( + r"^Your URL pattern \('\^tuple/\$', <function <lambda> at 0x(\w+)>\) is " + r"invalid. Ensure that urlpatterns is a list of url\(\) instances.$" + )) + @override_settings(ROOT_URLCONF='check_framework.urls.beginning_with_slash') def test_beginning_with_slash(self): result = check_url_config(None) @@ -50,3 +62,19 @@ class CheckUrlsTest(SimpleTestCase): delattr(settings, 'ROOT_URLCONF') result = check_url_config(None) self.assertEqual(result, []) + + def test_get_warning_for_invalid_pattern_string(self): + warning = get_warning_for_invalid_pattern('')[0] + self.assertEqual( + warning.hint, + "Try removing the string ''. The list of urlpatterns should " + "not have a prefix string as the first element.", + ) + + def test_get_warning_for_invalid_pattern_tuple(self): + warning = get_warning_for_invalid_pattern((r'^$', lambda x: x))[0] + self.assertEqual(warning.hint, "Try using url() instead of a tuple.") + + def test_get_warning_for_invalid_pattern_other(self): + warning = get_warning_for_invalid_pattern(object())[0] + self.assertIsNone(warning.hint) diff --git a/tests/check_framework/urls/contains_tuple.py b/tests/check_framework/urls/contains_tuple.py new file mode 100644 index 0000000000..56aa3ea364 --- /dev/null +++ b/tests/check_framework/urls/contains_tuple.py @@ -0,0 +1,3 @@ +urlpatterns = [ + (r'^tuple/$', lambda x: x), +] |
