def test_as_type(): subject = Configuration({'as_int': 5, 'as_str': '5'}) assert subject.get('as_int') == 5 assert subject.get('as_str') == '5' assert subject.get('as_str', as_type=str) == '5' assert subject.get('as_str', as_type=int) == 5 assert subject.get('as_str', as_type=bool) is True assert subject.get('as_str', as_type=lambda value: int(value) - 2) == 3
def test_as_type(): subject = Configuration({'as_int': 5, 'as_str': '5'}) assert subject.get('as_int') == 5 assert subject.get('as_str') == '5' assert subject.get('as_str', as_type=str) == '5' assert subject.get('as_str', as_type=int) == 5 assert subject.get('as_str', as_type=bool) is True assert subject.get('as_str', as_type=lambda value: int(value) - 2) == 3
def test_value_types(): config = Configuration({ 'ns.str': 'string', 'ns.int': 42, 'ns.float': 2.0, 'ns.bool': True, 'ns.ref1': 'prefix ${ns.str} suffix', 'ns.ref2': 'p${ns.int}s', 'ns.ref3': '${ns.float}', 'ns.ref4': '${ns.bool}', }) assert config.ns.str == 'string' assert config.ns.ref1 == config.get('ns.ref1') == 'prefix string suffix' assert config.ns.ref2 == config.get('ns.ref2') == 'p42s' assert config.ns.ref3 == config.get('ns.ref3') == 2.0 assert config.ns.ref4 == config.get('ns.ref4') is True
def test_value_types(): config = Configuration({ 'ns.str': 'string', 'ns.int': 42, 'ns.float': 2.0, 'ns.bool': True, 'ns.ref1': 'prefix ${ns.str} suffix', 'ns.ref2': 'p${ns.int}s', 'ns.ref3': '${ns.float}', 'ns.ref4': '${ns.bool}', }) assert config.ns.str == 'string' assert config.ns.ref1 == config.get('ns.ref1') == 'prefix string suffix' assert config.ns.ref2 == config.get('ns.ref2') == 'p42s' assert config.ns.ref3 == config.get('ns.ref3') == 2.0 assert config.ns.ref4 == config.get('ns.ref4') is True
def test_sub_config_reference(): config = Configuration({ 'key': 'string', 'ns.test1': '${key}', 'ns.test2': '${ns.test1}', }) assert config.key == 'string' ns = config.ns assert ns.test1 == ns.get('test1') == 'string' assert ns.test2 == ns.get('test2') == 'string' ns = config.get('ns') assert ns.test1 == ns.get('test1') == 'string' assert ns.test2 == ns.get('test2') == 'string'
def test_sub_config_reference(): config = Configuration({ 'key': 'string', 'ns.test1': '${key}', 'ns.test2': '${ns.test1}', }) assert config.key == 'string' ns = config.ns assert ns.test1 == ns.get('test1') == 'string' assert ns.test2 == ns.get('test2') == 'string' ns = config.get('ns') assert ns.test1 == ns.get('test1') == 'string' assert ns.test2 == ns.get('test2') == 'string'
def test_missing_reference(): config = Configuration({ 'key': 'string', 'template.working': '${key}', 'template.missing': '${ns.key}', }) assert config.key == 'string' assert config.template.working == 'string' with pytest.raises(ConfiguredReferenceError) as e: assert not config.template.missing assert 'ns.key' in str(e.value) with pytest.raises(ConfiguredReferenceError) as e: assert not config.get('template.missing') assert 'ns.key' in str(e.value)
def test_missing_reference(): config = Configuration({ 'key': 'string', 'template.working': '${key}', 'template.missing': '${ns.key}', }) assert config.key == 'string' assert config.template.working == 'string' with pytest.raises(ConfiguredReferenceError) as e: assert not config.template.missing assert 'ns.key' in str(e.value) with pytest.raises(ConfiguredReferenceError) as e: assert not config.get('template.missing') assert 'ns.key' in str(e.value)
import os from confidence import Configuration BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) config = Configuration(BASE_DIR) SECRET_KEY = config.get('environment.secret_key', 'verysecretkey') DEBUG = config.get('environment.debug', True) ALLOWED_HOSTS = config.get('environment.allowed_hosts', []) INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'confidence', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware',