mirror of
				https://github.com/django/django.git
				synced 2025-11-03 05:13:23 +00:00 
			
		
		
		
	Added tests for django.utils.autoreload.
This commit is contained in:
		
							parent
							
								
									61225ef721
								
							
						
					
					
						commit
						bf4516a628
					
				
					 1 changed files with 68 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -1,3 +1,4 @@
 | 
			
		|||
import gettext
 | 
			
		||||
import os
 | 
			
		||||
import shutil
 | 
			
		||||
import tempfile
 | 
			
		||||
| 
						 | 
				
			
			@ -5,10 +6,12 @@ from importlib import import_module
 | 
			
		|||
 | 
			
		||||
from django import conf
 | 
			
		||||
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.utils import autoreload
 | 
			
		||||
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')
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -184,3 +187,67 @@ class TestFilenameGenerator(SimpleTestCase):
 | 
			
		|||
            with self.assertRaises(Exception):
 | 
			
		||||
                autoreload.check_errors(import_module)('test_exception')
 | 
			
		||||
        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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue