From c91dcd36fc46ad562af054bc0cad2f0055b0f7eb Mon Sep 17 00:00:00 2001 From: Anton Samarchyan Date: Wed, 26 Jul 2017 20:00:28 -0400 Subject: [PATCH] Improved test coverage for django.conf. --- tests/settings_tests/tests.py | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/settings_tests/tests.py b/tests/settings_tests/tests.py index edcb2dabc89..1ab08cbd14e 100644 --- a/tests/settings_tests/tests.py +++ b/tests/settings_tests/tests.py @@ -3,6 +3,7 @@ import sys import unittest import warnings from types import ModuleType +from unittest import mock from django.conf import ENVIRONMENT_VARIABLE, LazySettings, Settings, settings from django.core.exceptions import ImproperlyConfigured @@ -11,6 +12,7 @@ from django.test import ( SimpleTestCase, TestCase, TransactionTestCase, modify_settings, override_settings, signals, ) +from django.test.utils import requires_tz_support @modify_settings(ITEMS={ @@ -286,6 +288,42 @@ class SettingsTests(SimpleTestCase): with self.assertRaises(AttributeError): 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): def setUp(self):