def test_can_set_lowercase_attribute_as_attribute(self): config = Config() config.debug = True # seems like an attribute assert hasattr(config, 'debug') # actually is an attribute assert 'debug' in config.__dict__
def test_uppercase_items_are_also_attributes(self): config = Config() config.DEBUG = True assert config['DEBUG'] config['PORT'] = 5000 assert config.PORT == 5000
def test_cant_set_uppercase_attribute_as_attribute(self): config = Config() config.DEBUG = True # seems like an attribute assert hasattr(config, 'DEBUG') # actually is not an attribute assert 'DEBUG' not in config.__dict__
def test_uppercase_items_are_also_attributes(self): config = Config() config.DEBUG = True assert config['DEBUG'] config['PORT'] = 5000 assert config.PORT == 5000
def test_cant_set_uppercase_attribute_as_attribute(self): config = Config() config.DEBUG = True # seems like an attribute assert hasattr(config, 'DEBUG') # actually is not an attribute assert 'DEBUG' not in config.__dict__
def test_can_set_lowercase_attribute_as_attribute(self): config = Config() config.debug = True # seems like an attribute assert hasattr(config, 'debug') # actually is an attribute assert 'debug' in config.__dict__
def test_load_from_either_mapping_or_object(self): config1 = Config() config1.load({'DEBUG': True}) assert config1.DEBUG config2 = Config() config2.load(AppConfig) assert config2.SECRET_KEY == '123***456'
def test_load_from_envvar(self): config = Config(datasrc=os.environ) assert 'SECRET_KEY' not in config os.environ.setdefault('SECRET_KEY', '123***456') assert config.SECRET_KEY == '123***456' os.environ['SECRET_KEY'] = 'xxx***yyy' assert config.SECRET_KEY == 'xxx***yyy'
def test_post_index_page_with_expired_token(self): token = make_token() data = {'greeting': 'hello', '_onetimetoken': token} mock_settings = Config(dict( PAGE_LOADER=settings.PAGE_LOADER, TOKEN_EXPIRES=0 )) with mock.patch('confpages.token.settings', mock_settings): response = client.post('/p/index/', data) self.assertEqual(response.status_code, 403) self.assertEqual(response.content, 'Expired one-time token')
def test_load_from_either_mapping_or_object(self): config1 = Config() config1.load({'DEBUG': True}) assert config1.DEBUG config2 = Config() config2.load(AppConfig) assert config2.SECRET_KEY == '123***456'
def test_load_from_object(self): config = Config() config.from_object(AppConfig) assert config.SECRET_KEY == '123***456'
def test_cant_load_lowercase_keys(self): config = Config({'debug': True}) assert 'debug' not in config assert not hasattr(config, 'debug')
def test_load_from_mapping(self): config = Config() config.from_mapping({'PORT': 5000}) assert config.PORT == 5000
def test_can_load_defaults_from_object(self): config = Config(AppConfig) assert config.SECRET_KEY == '123***456'
def test_can_load_defaults_from_mapping(self): config = Config({'DEBUG': True}) assert config.DEBUG
def test_load_from_mapping(self): config = Config() config.from_mapping({'PORT': 5000}) assert config.PORT == 5000
# -*- coding: utf-8 -*- from easyconfig import Config config = Config({'TOKEN': '', 'ENCODING_AES_KEY': '', 'APP_ID': ''})
from __future__ import absolute_import from easyconfig import Config, str_object from . import default config = Config(default) # Override the default configuration if `RESTART_CONFIG_MODULE` is given config.load(str_object('RESTART_CONFIG_MODULE', silent=True, is_envvar=True))
from django.conf import settings as django_settings from easyconfig import Config default_settings = { 'PAGE_LOADER': 'confpages.loaders.DefaultLoader', 'TOKEN_EXPIRES': 600 # in seconds } settings = Config(default_settings) # Get the global settings for the confpages app global_settings = getattr(django_settings, 'CONFPAGES', {}) # Validate the global settings assert isinstance(global_settings, dict), \ 'The `CONFPAGES` setting must be a dictionary' difference = set(global_settings.keys()) - set(default_settings.keys()) assert not len(difference), ('The keys %r in the `CONFPAGES` setting are ' 'not supported' % tuple(difference)) # Override the default settings by global ones settings.from_mapping(global_settings)
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import import os from easyconfig import Config, yaml_mapping # Initialize config config = Config({ 'COMMANDS_PATH': '', 'ENTRY_POINT': 'main', }) # Override config yaml_filename = os.environ.get('CMDR_CONFIG_YAML') if yaml_filename: config.from_mapping(yaml_mapping(yaml_filename)) # Validate config if not config.COMMANDS_PATH: raise RuntimeError('Configuration `COMMANDS_PATH` is empty') if not config.ENTRY_POINT: raise RuntimeError('Configuration `ENTRY_POINT` is empty')
#!/usr/bin/env python # -*- coding: utf-8 -*- from easyconfig import Config, envvar_object from . import default_settings settings = Config() settings.from_object(default_settings) # Override default settings if `RESOURCE_SETTINGS_MODULE` is given settings.from_object(envvar_object('RESOURCE_SETTINGS_MODULE', True))
def test_can_set_uppercase_item(self): config = Config() config['DEBUG'] = True assert config['DEBUG']
def test_load_from_none_object(self): config = Config() config.from_object(None) uppercase_attrs = [attr for attr in dir(config) if attr.isupper()] assert not uppercase_attrs
def test_load_from_empty_envvar(self): config = Config(datasrc=os.environ) assert not hasattr(config, 'NULL_KEY')
def test_load_from_object(self): config = Config() config.from_object(AppConfig) assert config.SECRET_KEY == '123***456'
def test_load_from_none_object(self): config = Config() config.from_object(None) uppercase_attrs = [attr for attr in dir(config) if attr.isupper()] assert not uppercase_attrs
#!/usr/bin/env python # -*- coding: utf-8 -*- from easyconfig import Config, envvar_object from . import default_settings settings = Config() settings.from_object(default_settings) # Override default settings if `RESOURCE_SETTINGS_MODULE` is given settings.from_object(envvar_object('RESOURCE_SETTINGS_MODULE', True))
def test_cant_set_lowercase_item(self): config = Config() with pytest.raises(ValueError): config['debug'] = True