mirror of
https://github.com/django/django.git
synced 2024-12-23 17:46:27 +00:00
93 lines
3.1 KiB
Python
93 lines
3.1 KiB
Python
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
from django.template import autoreload
|
|
from django.test import SimpleTestCase, override_settings
|
|
from django.test.utils import require_jinja2
|
|
|
|
ROOT = Path(__file__).parent.absolute()
|
|
EXTRA_TEMPLATES_DIR = ROOT / "templates_extra"
|
|
|
|
|
|
@override_settings(
|
|
INSTALLED_APPS=['template_tests'],
|
|
TEMPLATES=[{
|
|
'BACKEND': 'django.template.backends.dummy.TemplateStrings',
|
|
'APP_DIRS': True,
|
|
}, {
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
'DIRS': [EXTRA_TEMPLATES_DIR],
|
|
'OPTIONS': {
|
|
'context_processors': [
|
|
'django.template.context_processors.request',
|
|
],
|
|
'loaders': [
|
|
'django.template.loaders.filesystem.Loader',
|
|
'django.template.loaders.app_directories.Loader',
|
|
]
|
|
},
|
|
}])
|
|
class TemplateReloadTests(SimpleTestCase):
|
|
@mock.patch('django.template.autoreload.reset_loaders')
|
|
def test_template_changed(self, mock_reset):
|
|
template_path = Path(__file__).parent / 'templates' / 'index.html'
|
|
self.assertTrue(autoreload.template_changed(None, template_path))
|
|
mock_reset.assert_called_once()
|
|
|
|
@mock.patch('django.template.autoreload.reset_loaders')
|
|
def test_non_template_changed(self, mock_reset):
|
|
self.assertIsNone(autoreload.template_changed(None, Path(__file__)))
|
|
mock_reset.assert_not_called()
|
|
|
|
def test_watch_for_template_changes(self):
|
|
mock_reloader = mock.MagicMock()
|
|
autoreload.watch_for_template_changes(mock_reloader)
|
|
self.assertSequenceEqual(
|
|
sorted(mock_reloader.watch_dir.call_args_list),
|
|
[
|
|
mock.call(ROOT / 'templates', '**/*'),
|
|
mock.call(ROOT / 'templates_extra', '**/*')
|
|
]
|
|
)
|
|
|
|
def test_get_template_directories(self):
|
|
self.assertSetEqual(
|
|
autoreload.get_template_directories(),
|
|
{
|
|
ROOT / 'templates_extra',
|
|
ROOT / 'templates',
|
|
}
|
|
)
|
|
|
|
@mock.patch('django.template.loaders.base.Loader.reset')
|
|
def test_reset_all_loaders(self, mock_reset):
|
|
autoreload.reset_loaders()
|
|
self.assertEqual(mock_reset.call_count, 2)
|
|
|
|
|
|
@require_jinja2
|
|
@override_settings(INSTALLED_APPS=['template_tests'])
|
|
class Jinja2TemplateReloadTests(SimpleTestCase):
|
|
def test_watch_for_template_changes(self):
|
|
mock_reloader = mock.MagicMock()
|
|
autoreload.watch_for_template_changes(mock_reloader)
|
|
self.assertSequenceEqual(
|
|
sorted(mock_reloader.watch_dir.call_args_list),
|
|
[
|
|
mock.call(ROOT / 'templates', '**/*'),
|
|
]
|
|
)
|
|
|
|
def test_get_template_directories(self):
|
|
self.assertSetEqual(
|
|
autoreload.get_template_directories(),
|
|
{
|
|
ROOT / 'templates',
|
|
}
|
|
)
|
|
|
|
@mock.patch('django.template.loaders.base.Loader.reset')
|
|
def test_reset_all_loaders(self, mock_reset):
|
|
autoreload.reset_loaders()
|
|
self.assertEqual(mock_reset.call_count, 0)
|