1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
from django.test.utils import override_settings
from .models import (AppConf, TestConf, PrefixConf,
YetAnotherPrefixConf, SeparateConf,
ProxyConf, CustomHolderConf, custom_holder)
class TestConfTests(TestCase):
def test_basic(self):
self.assertEqual(TestConf._meta.prefix, 'tests')
def test_simple(self):
self.assertTrue(hasattr(settings, 'TESTS_SIMPLE_VALUE'))
self.assertEqual(settings.TESTS_SIMPLE_VALUE, True)
def test_configured(self):
self.assertTrue(hasattr(settings, 'TESTS_CONFIGURED_VALUE'))
self.assertEqual(settings.TESTS_CONFIGURED_VALUE, 'correct')
def test_configure_method(self):
self.assertTrue(hasattr(settings, 'TESTS_CONFIGURE_METHOD_VALUE'))
self.assertEqual(settings.TESTS_CONFIGURE_METHOD_VALUE, True)
def test_init_kwargs(self):
custom_conf = TestConf(CUSTOM_VALUE='custom')
self.assertEqual(custom_conf.CUSTOM_VALUE, 'custom')
self.assertEqual(settings.TESTS_CUSTOM_VALUE, 'custom')
self.assertRaises(AttributeError,
lambda: custom_conf.TESTS_CUSTOM_VALUE)
custom_conf.CUSTOM_VALUE_SETATTR = 'custom'
self.assertEqual(settings.TESTS_CUSTOM_VALUE_SETATTR, 'custom')
custom_conf.custom_value_lowercase = 'custom'
self.assertRaises(AttributeError,
lambda: settings.custom_value_lowercase)
def test_init_kwargs_with_prefix(self):
custom_conf = TestConf(TESTS_CUSTOM_VALUE2='custom2')
self.assertEqual(custom_conf.TESTS_CUSTOM_VALUE2, 'custom2')
self.assertEqual(settings.TESTS_CUSTOM_VALUE2, 'custom2')
def test_proxy(self):
custom_conf = ProxyConf(CUSTOM_VALUE3='custom3')
self.assertEqual(custom_conf.CUSTOM_VALUE3, 'custom3')
self.assertEqual(settings.TESTS_CUSTOM_VALUE3, 'custom3')
self.assertEqual(custom_conf.TESTS_CUSTOM_VALUE3, 'custom3')
self.assertTrue('tests' in custom_conf.INSTALLED_APPS)
def test_dir_members(self):
custom_conf = TestConf()
self.assertTrue('TESTS_SIMPLE_VALUE' in dir(settings))
self.assertTrue('SIMPLE_VALUE' in dir(custom_conf))
self.assertFalse('TESTS_SIMPLE_VALUE' in dir(custom_conf))
def test_custom_holder(self):
CustomHolderConf()
self.assertTrue(hasattr(custom_holder, 'CUSTOM_HOLDER_SIMPLE_VALUE'))
self.assertEqual(custom_holder.CUSTOM_HOLDER_SIMPLE_VALUE, True)
def test_subclass_configured_data(self):
self.assertTrue('TESTS_CONFIGURE_METHOD_VALUE2' in dir(settings))
self.assertEqual(settings.TESTS_CONFIGURE_METHOD_VALUE2, False)
# Pair of tests checking override_settings compat.
# See:
# https://github.com/django-compressor/django-appconf/issues/29
# https://github.com/django-compressor/django-appconf/issues/30
@override_settings(TESTS_SIMPLE_VALUE=False)
def test_override_settings_once(self):
self.assertEqual(settings.TESTS_SIMPLE_VALUE, False)
@override_settings(TESTS_SIMPLE_VALUE=False)
def test_override_settings_twice(self):
self.assertEqual(settings.TESTS_SIMPLE_VALUE, False)
class PrefixConfTests(TestCase):
def test_prefix(self):
self.assertEqual(PrefixConf._meta.prefix, 'prefix')
def test_simple(self):
self.assertTrue(hasattr(settings, 'PREFIX_SIMPLE_VALUE'))
self.assertEqual(settings.PREFIX_SIMPLE_VALUE, True)
def test_configured(self):
self.assertTrue(hasattr(settings, 'PREFIX_CONFIGURED_VALUE'))
self.assertEqual(settings.PREFIX_CONFIGURED_VALUE, 'correct')
def test_configure_method(self):
self.assertTrue(hasattr(settings, 'PREFIX_CONFIGURE_METHOD_VALUE'))
self.assertEqual(settings.PREFIX_CONFIGURE_METHOD_VALUE, True)
class YetAnotherPrefixConfTests(TestCase):
def test_prefix(self):
self.assertEqual(YetAnotherPrefixConf._meta.prefix,
'yetanother_prefix')
def test_simple(self):
self.assertTrue(hasattr(settings,
'YETANOTHER_PREFIX_SIMPLE_VALUE'))
self.assertEqual(settings.YETANOTHER_PREFIX_SIMPLE_VALUE, False)
def test_configured(self):
self.assertTrue(hasattr(settings,
'YETANOTHER_PREFIX_CONFIGURED_VALUE'))
self.assertEqual(settings.YETANOTHER_PREFIX_CONFIGURED_VALUE,
'correct')
def test_configure_method(self):
self.assertTrue(hasattr(settings,
'YETANOTHER_PREFIX_CONFIGURE_METHOD_VALUE'))
self.assertEqual(settings.YETANOTHER_PREFIX_CONFIGURE_METHOD_VALUE,
True)
class SeparateConfTests(TestCase):
def test_prefix(self):
self.assertEqual(SeparateConf._meta.prefix, 'prefix')
def test_simple(self):
self.assertTrue(hasattr(settings, 'PREFIX_SEPARATE_VALUE'))
self.assertEqual(settings.PREFIX_SEPARATE_VALUE, True)
class RequiredSettingsTests(TestCase):
def create_invalid_conf(self):
class RequirementConf(AppConf):
class Meta:
required = ['NOT_PRESENT']
def test_value_is_defined(self):
class RequirementConf(AppConf):
class Meta:
holder = 'tests.models.custom_holder'
prefix = 'holder'
required = ['VALUE']
def test_default_is_defined(self):
class RequirementConf(AppConf):
SIMPLE_VALUE = True
class Meta:
required = ['SIMPLE_VALUE']
def test_missing(self):
self.assertRaises(ImproperlyConfigured, self.create_invalid_conf)
|