Kaydet (Commit) bf4516a6 authored tarafından Phil Tysoe's avatar Phil Tysoe Kaydeden (comit) Tim Graham

Added tests for django.utils.autoreload.

üst 61225ef7
import gettext
import os import os
import shutil import shutil
import tempfile import tempfile
...@@ -5,10 +6,12 @@ from importlib import import_module ...@@ -5,10 +6,12 @@ from importlib import import_module
from django import conf from django import conf
from django.contrib import admin from django.contrib import admin
from django.test import SimpleTestCase, override_settings from django.test import SimpleTestCase, mock, override_settings
from django.test.utils import extend_sys_path from django.test.utils import extend_sys_path
from django.utils import autoreload from django.utils import autoreload
from django.utils._os import npath from django.utils._os import npath
from django.utils.six.moves import _thread
from django.utils.translation import trans_real
LOCALE_PATH = os.path.join(os.path.dirname(__file__), 'locale') LOCALE_PATH = os.path.join(os.path.dirname(__file__), 'locale')
...@@ -184,3 +187,67 @@ class TestFilenameGenerator(SimpleTestCase): ...@@ -184,3 +187,67 @@ class TestFilenameGenerator(SimpleTestCase):
with self.assertRaises(Exception): with self.assertRaises(Exception):
autoreload.check_errors(import_module)('test_exception') autoreload.check_errors(import_module)('test_exception')
self.assertFileFound(filename) self.assertFileFound(filename)
class CleanFilesTests(SimpleTestCase):
TEST_MAP = {
# description: (input_file_list, expected_returned_file_list)
'falsies': ([None, False], []),
'pycs': (['myfile.pyc'], ['myfile.py']),
'pyos': (['myfile.pyo'], ['myfile.py']),
'$py.class': (['myclass$py.class'], ['myclass.py']),
'combined': (
[None, 'file1.pyo', 'file2.pyc', 'myclass$py.class'],
['file1.py', 'file2.py', 'myclass.py'],
)
}
def _run_tests(self, mock_files_exist=True):
with mock.patch('django.utils.autoreload.os.path.exists', return_value=mock_files_exist):
for description, values in self.TEST_MAP.items():
filenames, expected_returned_filenames = values
self.assertEqual(
autoreload.clean_files(filenames),
expected_returned_filenames if mock_files_exist else [],
msg='{} failed for input file list: {}; returned file list: {}'.format(
description, filenames, expected_returned_filenames
),
)
def test_files_exist(self):
"""
If the file exists, any compiled files (pyc, pyo, $py.class) are
transformed as their source files.
"""
self._run_tests()
def test_files_do_not_exist(self):
"""
If the files don't exist, they aren't in the returned file list.
"""
self._run_tests(mock_files_exist=False)
class ResetTranslationsTests(SimpleTestCase):
def setUp(self):
self.gettext_translations = gettext._translations.copy()
self.trans_real_translations = trans_real._translations.copy()
def tearDown(self):
gettext._translations = self.gettext_translations
trans_real._translations = self.trans_real_translations
def test_resets_gettext(self):
gettext._translations = {'foo': 'bar'}
autoreload.reset_translations()
self.assertEqual(gettext._translations, {})
def test_resets_trans_real(self):
trans_real._translations = {'foo': 'bar'}
trans_real._default = 1
trans_real._active = False
autoreload.reset_translations()
self.assertEqual(trans_real._translations, {})
self.assertIsNone(trans_real._default)
self.assertIsInstance(trans_real._active, _thread._local)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment