def test_exclude(): config = Config(exclude_default=True) assert len(config) == 0 os.environ['name13'] = 'vvv' config = Config(exclude=[Source.env]) assert 'name13' not in config
def test_update_from_func(): config = Config(exclude_default=True) assert len(config) == 0 ONLY_VAR = 123 config.update_from_locals() print(config) assert len(config) == 1
def test_add(): config = Config(exclude=[Source.env]) config.insert({'kkk': 'vvv'}) assert config.get('kkk') == 'vvv' config.insert('custom.py') assert config.contains('INSERT_VARIABLE') assert config.get_raw('INSERT_VARIABLE') == '1' * 10
def test_dotted_access(): config = Config() config.set('a', {'b': 3}) assert config.get('a') == {'b': 3} assert config.get('a.b') == 3 config.set('value', {'key': {'other': 123}}) assert config.int('value.key.other') == 123
def test_config_in_files(): config = Config() assert config.list_config == ['first', 'second', 'third'] assert config.dict('dict_config') == { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' } assert config.str('dict_config.key1') == 'value1' with pytest.raises(KeyError): a = config['unknown'] assert config.get('bool_prop') is True assert config.bool('bool_prop') is True assert config.bool('false_prop') is False assert config.str('logger.mode') == 'DEBUG'
def config(): return Config()
def test_working(): config = Config()
def test_empty_ini(): config = Config('empty.ini')
import os from bestconfig import Config from tests.integration.project.subfolder.module import get_path if __name__ == '__main__': config = Config() config.assert_contains('VERSION') print(os.path.abspath(__file__), get_path(), os.getcwd())
def test_get_bool(): config = Config() config.set('a', '') config.set('b', '123') config.set('c', '0') config.set('d', 'on') config.set('e', 'False') config.set('g', 'OFF') config.set('n', 'hello') assert config.bool('a') is False assert config.bool('b') is True assert config.bool('c') is False assert config.bool('d') is True assert config.bool('e') is False assert config.bool('g') is False assert config.bool('n') is True
def test_py_config(): config = Config('settings.py') assert config.lowercase_setting == 'value' assert config.int('ONLY_PYTHON_VAR') == 1233
def test_overload(): config = Config() assert config['overload'] == 'hello' config = Config('config.py') assert config.overload == 'hello2'
""" Точка входа в модуль app """ from bestconfig import Config config = Config('../dev.env', 'app.yaml') if __name__ == '__main__': authorized_user_limit = config.int('limits.authorized.messages_per_second') DEBUG = config.bool('DEBUG') if DEBUG: print('RUN IN DEBUG MODE') else: print('RUN IN PRODUCTION MODE')
from bestconfig import Config config = Config() config.insert({'name': 'Ivan', 'lastname': 'Urgant'}) FULL_NAME = f'{config.name} {config.lastname}' config.update_from_locals() def test_update_from_locals(): assert config.get('FULL_NAME') == 'Ivan Urgant' def test_update_from_func(): config = Config(exclude_default=True) assert len(config) == 0 ONLY_VAR = 123 config.update_from_locals() print(config) assert len(config) == 1
""" Основной конфигурационный файл для проекта """ from bestconfig import Config, Source config = Config('dev.env', exclude=[Source.env]) config.assert_contains('VALID') print(config)
def test_ini_config(): config = Config() assert config.get('general.value') == 34 with pytest.raises(KeyError): config.get('asdf', raise_absent=True) with pytest.raises(KeyError): a = config.asdffdsa assert config.bool('section_a.bool_val') is False assert config.float('section_a.pi_val') == 3.14 assert 'Port' in config.get('topsecret.server.com') assert config.get('topsecret.server.com')['Port'] == 50022 assert config.get('bitbucket.org')['CompressionLevel'] == 9 with pytest.raises(KeyError): var = config.get('bitbucket.org').unknown
def test_exclude(): config = Config(exclude=['config.json']) assert config.get('overload') is None
from bestconfig import Config config = Config()
def test_default_get(): config = Config() assert config.get('limit_users') == 10 assert isinstance(config.get('logger'), dict) config.set('a', 'b') config.set('b', '{1,2,3}') config.set('c', 'True') assert config.get('a') == 'b' assert config.get('b') == {1, 2, 3} assert config.get('c') is True
def test_empty_yaml(): config = Config('empty_yaml.yaml')
def test_cast(): config = Config() config.set('1', 123) assert config.str('1') == '123' config.set('a', 123) assert config.bool('a') is True config.set('a', False) assert config.bool('a') is False config.set('a', {1: 2}) assert config.dict('a') == {1: 2} config.set('a', 'asdfs') assert config.dict('a') is None config.set('a', '[[[') assert config.list('a') is None config.set('a', [1, 2, 3]) assert config.list('a') == [1, 2, 3] config.set('a', '') assert config.list('a') == [] config.set('a', '{}') assert config.dict('a') == {} config.set('', '')
from bestconfig import Config if __name__ == '__main__': config = Config() # Проверяем, что переменные указаны config.assert_contains('VALID') print('BOT_ID', config.bot.id) print('BOT_TOKEN', config.BOT_TOKEN) assert isinstance(config.bot.id, int) # Пример обращения через точку print('USERNAME', config.get('admin.username'))