Improved test coverage for django.conf.

This commit is contained in:
Anton Samarchyan 2017-07-26 20:00:28 -04:00 committed by Tim Graham
parent 0eefda493b
commit c91dcd36fc
1 changed files with 38 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import sys
import unittest import unittest
import warnings import warnings
from types import ModuleType from types import ModuleType
from unittest import mock
from django.conf import ENVIRONMENT_VARIABLE, LazySettings, Settings, settings from django.conf import ENVIRONMENT_VARIABLE, LazySettings, Settings, settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
@ -11,6 +12,7 @@ from django.test import (
SimpleTestCase, TestCase, TransactionTestCase, modify_settings, SimpleTestCase, TestCase, TransactionTestCase, modify_settings,
override_settings, signals, override_settings, signals,
) )
from django.test.utils import requires_tz_support
@modify_settings(ITEMS={ @modify_settings(ITEMS={
@ -286,6 +288,42 @@ class SettingsTests(SimpleTestCase):
with self.assertRaises(AttributeError): with self.assertRaises(AttributeError):
getattr(settings, 'TEST2') getattr(settings, 'TEST2')
def test_no_secret_key(self):
settings_module = ModuleType('fake_settings_module')
sys.modules['fake_settings_module'] = settings_module
msg = 'The SECRET_KEY setting must not be empty.'
try:
with self.assertRaisesMessage(ImproperlyConfigured, msg):
Settings('fake_settings_module')
finally:
del sys.modules['fake_settings_module']
def test_no_settings_module(self):
msg = (
'Requested setting%s, but settings are not configured. You '
'must either define the environment variable DJANGO_SETTINGS_MODULE '
'or call settings.configure() before accessing settings.'
)
orig_settings = os.environ[ENVIRONMENT_VARIABLE]
os.environ[ENVIRONMENT_VARIABLE] = ''
try:
with self.assertRaisesMessage(ImproperlyConfigured, msg % 's'):
settings._setup()
with self.assertRaisesMessage(ImproperlyConfigured, msg % ' TEST'):
settings._setup('TEST')
finally:
os.environ[ENVIRONMENT_VARIABLE] = orig_settings
def test_already_configured(self):
with self.assertRaisesMessage(RuntimeError, 'Settings already configured.'):
settings.configure()
@requires_tz_support
@mock.patch('django.conf.global_settings.TIME_ZONE', 'test')
def test_incorrect_timezone(self):
with self.assertRaisesMessage(ValueError, 'Incorrect timezone setting: test'):
settings._setup()
class TestComplexSettingOverride(SimpleTestCase): class TestComplexSettingOverride(SimpleTestCase):
def setUp(self): def setUp(self):