2016-03-18 20:48:14 +08:00
|
|
|
from copy import copy, deepcopy
|
2015-06-08 20:15:41 +08:00
|
|
|
|
2020-03-22 03:14:02 +08:00
|
|
|
from django.core.checks.templates import (
|
|
|
|
E001,
|
|
|
|
E002,
|
|
|
|
check_setting_app_dirs_loaders,
|
|
|
|
check_string_if_invalid_is_string,
|
|
|
|
)
|
2015-06-04 23:55:58 +08:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
from django.test.utils import override_settings
|
|
|
|
|
|
|
|
|
|
|
|
class CheckTemplateSettingsAppDirsTest(SimpleTestCase):
|
|
|
|
TEMPLATES_APP_DIRS_AND_LOADERS = [
|
|
|
|
{
|
|
|
|
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
|
|
|
"APP_DIRS": True,
|
|
|
|
"OPTIONS": {
|
|
|
|
"loaders": ["django.template.loaders.filesystem.Loader"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
@override_settings(TEMPLATES=TEMPLATES_APP_DIRS_AND_LOADERS)
|
|
|
|
def test_app_dirs_and_loaders(self):
|
|
|
|
"""
|
|
|
|
Error if template loaders are specified and APP_DIRS is True.
|
|
|
|
"""
|
2020-03-22 03:14:02 +08:00
|
|
|
self.assertEqual(check_setting_app_dirs_loaders(None), [E001])
|
2015-06-04 23:55:58 +08:00
|
|
|
|
|
|
|
def test_app_dirs_removed(self):
|
2015-06-08 20:15:41 +08:00
|
|
|
TEMPLATES = deepcopy(self.TEMPLATES_APP_DIRS_AND_LOADERS)
|
2015-06-04 23:55:58 +08:00
|
|
|
del TEMPLATES[0]["APP_DIRS"]
|
|
|
|
with self.settings(TEMPLATES=TEMPLATES):
|
2020-03-22 03:14:02 +08:00
|
|
|
self.assertEqual(check_setting_app_dirs_loaders(None), [])
|
2015-06-04 23:55:58 +08:00
|
|
|
|
|
|
|
def test_loaders_removed(self):
|
2015-06-08 20:15:41 +08:00
|
|
|
TEMPLATES = deepcopy(self.TEMPLATES_APP_DIRS_AND_LOADERS)
|
2015-06-04 23:55:58 +08:00
|
|
|
del TEMPLATES[0]["OPTIONS"]["loaders"]
|
|
|
|
with self.settings(TEMPLATES=TEMPLATES):
|
2020-03-22 03:14:02 +08:00
|
|
|
self.assertEqual(check_setting_app_dirs_loaders(None), [])
|
2016-03-18 20:48:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CheckTemplateStringIfInvalidTest(SimpleTestCase):
|
|
|
|
TEMPLATES_STRING_IF_INVALID = [
|
|
|
|
{
|
|
|
|
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
|
|
|
"OPTIONS": {
|
|
|
|
"string_if_invalid": False,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
|
|
|
"OPTIONS": {
|
|
|
|
"string_if_invalid": 42,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUpClass()
|
2016-03-18 20:48:14 +08:00
|
|
|
cls.error1 = copy(E002)
|
|
|
|
cls.error2 = copy(E002)
|
|
|
|
string_if_invalid1 = cls.TEMPLATES_STRING_IF_INVALID[0]["OPTIONS"][
|
|
|
|
"string_if_invalid"
|
|
|
|
]
|
|
|
|
string_if_invalid2 = cls.TEMPLATES_STRING_IF_INVALID[1]["OPTIONS"][
|
|
|
|
"string_if_invalid"
|
|
|
|
]
|
|
|
|
cls.error1.msg = cls.error1.msg.format(
|
|
|
|
string_if_invalid1, type(string_if_invalid1).__name__
|
|
|
|
)
|
|
|
|
cls.error2.msg = cls.error2.msg.format(
|
|
|
|
string_if_invalid2, type(string_if_invalid2).__name__
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_settings(TEMPLATES=TEMPLATES_STRING_IF_INVALID)
|
|
|
|
def test_string_if_invalid_not_string(self):
|
2020-03-22 03:14:02 +08:00
|
|
|
self.assertEqual(
|
|
|
|
check_string_if_invalid_is_string(None), [self.error1, self.error2]
|
|
|
|
)
|
2016-03-18 20:48:14 +08:00
|
|
|
|
|
|
|
def test_string_if_invalid_first_is_string(self):
|
|
|
|
TEMPLATES = deepcopy(self.TEMPLATES_STRING_IF_INVALID)
|
|
|
|
TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = "test"
|
|
|
|
with self.settings(TEMPLATES=TEMPLATES):
|
2020-03-22 03:14:02 +08:00
|
|
|
self.assertEqual(check_string_if_invalid_is_string(None), [self.error2])
|
2016-03-18 20:48:14 +08:00
|
|
|
|
|
|
|
def test_string_if_invalid_both_are_strings(self):
|
|
|
|
TEMPLATES = deepcopy(self.TEMPLATES_STRING_IF_INVALID)
|
|
|
|
TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = "test"
|
|
|
|
TEMPLATES[1]["OPTIONS"]["string_if_invalid"] = "test"
|
|
|
|
with self.settings(TEMPLATES=TEMPLATES):
|
2020-03-22 03:14:02 +08:00
|
|
|
self.assertEqual(check_string_if_invalid_is_string(None), [])
|
2016-03-18 20:48:14 +08:00
|
|
|
|
|
|
|
def test_string_if_invalid_not_specified(self):
|
|
|
|
TEMPLATES = deepcopy(self.TEMPLATES_STRING_IF_INVALID)
|
|
|
|
del TEMPLATES[1]["OPTIONS"]["string_if_invalid"]
|
|
|
|
with self.settings(TEMPLATES=TEMPLATES):
|
2020-03-22 03:14:02 +08:00
|
|
|
self.assertEqual(check_string_if_invalid_is_string(None), [self.error1])
|