def test_migrate_config_file(): spec = YapconfSpec({'foo': {'bootstrap': True}}) with patch('yapconf.open', mock_open(read_data='{}')): config = spec.migrate_config_file('/path/to/file', create=False, include_bootstrap=False) assert config == {}
def test_env_names_with_prefixes(env_name, apply_prefix, env_prefix, key): spec = YapconfSpec( { 'bg_host': { 'type': 'str', 'env_name': env_name, 'apply_env_prefix': apply_prefix, } }, env_prefix=env_prefix) config = spec.load_config(('ENVIRONMENT', {key: 'host_value'})) assert config.bg_host == 'host_value'
def test_migrate_config_file_update_previous_default(): spec = YapconfSpec( {'foo': { 'default': 'baz', 'previous_defaults': ['bar'] }}) open_path = builtins_path + '.open' current_config = '{"foo": "bar"}' with patch(open_path, mock_open(read_data=current_config)): new_config = spec.migrate_config_file('/path/to/file', create=False, update_defaults=True) assert new_config == {'foo': 'baz'}
def simple_spec(): """Simple YapconfSpec for all YapconfItem variations""" return YapconfSpec({ 'my_string': { 'type': 'str', 'required': True, }, 'my_int': { 'type': 'int', 'required': True, }, 'my_long': { 'type': 'long', 'required': True, }, 'my_float': { 'type': 'float', 'required': True, }, 'my_bool': { 'type': 'bool', 'required': True, }, 'my_complex': { 'type': 'complex', 'required': True, }, })
def spec_with_lists(): """YapconfSpec for testing YapconfListItem variations""" return YapconfSpec({ 'simple_list': { 'type': 'list', 'required': True, 'items': { 'list_item': { 'type': 'str', 'required': True } } }, 'top_list': { 'type': 'list', 'required': True, 'items': { 'nested_list': { 'type': 'list', 'required': True, 'items': { 'nested_list_items': { 'type': 'int', 'required': True } } } } }, 'list_of_dictionaries': { 'type': 'list', 'required': True, 'items': { 'list_item': { 'type': 'dict', 'required': True, 'items': { 'foo': { 'type': 'str', 'required': True, }, 'bar': { 'type': 'str', 'required': False, } } } } } })
def spec_with_dicts(): """YapconfSpec for testing YapconfDictItem variations""" return YapconfSpec({ 'database': { 'type': 'dict', 'required': True, 'items': { 'name': { 'type': 'str', 'required': True, }, 'host': { 'type': 'str', 'required': True, }, 'port': { 'type': 'int', 'required': True, }, } }, 'foo': { 'type': 'dict', 'required': True, 'items': { 'bar': { 'type': 'dict', 'required': True, 'items': { 'baz': { 'type': 'str', 'required': True, }, }, }, 'bat': { 'type': 'bool', } }, }, })
def basic_spec(): """Most basic spec you can have""" return YapconfSpec({'foo': {'type': 'str', 'required': True}})
def test_load_bad_specification_from_file(): open_path = builtins_path + '.open' with patch(open_path, mock_open(read_data="[]")): with pytest.raises(YapconfSpecError): YapconfSpec('/path/to/bad/spec', file_type='json')
def test_load_specification_from_file(file_type, file_data, overrides, expected_value): open_path = builtins_path + '.open' with patch(open_path, mock_open(read_data=file_data)): spec = YapconfSpec('/path/to/specification', file_type=file_type) assert spec.load_config(overrides) == expected_value
def test_invalid_specification(bad_data): with pytest.raises(YapconfSpecError): YapconfSpec(bad_data)
def test_spec_bad_file_type(): with pytest.raises(YapconfSpecError): YapconfSpec({}, file_type='INVALID')
def real_world_spec(): """YapconfSpec based on a 'real-world' example""" return YapconfSpec( { 'file': { 'type': 'str', 'required': True, 'default': '/default/path/to/file.yaml', 'bootstrap': True, 'env_name': 'MY_APP_CONFIG_FILE', 'cli_short_name': 'f', 'previous_defaults': ['./file.yaml'] }, 'web_port': { 'type': 'int', 'required': True, 'default': 3000, 'previous_names': ['web.port'], 'cli_short_name': 'p', 'previous_defaults': [1234, 2345] }, 'ssl': { 'type': 'dict', 'required': True, 'items': { 'private_key': { 'type': 'str', 'required': True, 'default': '/etc/certs/private.key', 'previous_names': ['web.key'] }, 'public_key': { 'type': 'str', 'required': True, 'default': '/etc/certs/public.crt', 'previous_names': ['web.crt'] } } }, 'database': { 'type': 'dict', 'required': True, 'items': { 'name': { 'type': 'str', 'required': True, 'default': 'myapp', 'previous_names': ['db_name'], 'previous_defaults': ['test'] }, 'host': { 'type': 'str', 'required': True, 'default': 'localhost', 'previous_names': ['db_host'], }, 'port': { 'type': 'int', 'required': True, 'default': 3306, 'previous_names': ['db_port'] }, 'verbose': { 'type': 'bool', 'required': True, 'default': False, 'previous_defaults': [True], } } } }, env_prefix='MY_APP_')