Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_invalidvalueerror():
config = ConfigManager.from_dict({"foo_bar": "bat"})
with pytest.raises(InvalidValueError) as excinfo:
config("bar", namespace="foo", parser=bool)
assert excinfo.value.namespace == "foo"
assert excinfo.value.key == "bar"
assert excinfo.value.parser == bool
def test_alternate_keys_with_namespace(key, alternate_keys, expected):
config = ConfigManager.from_dict(
{
"COMMON_FOO": "common_foo_abc",
"FOO": "foo_abc",
"FOO_BAR": "foo_bar_abc",
"FOO_BAR_BAZ": "foo_bar_baz_abc",
}
)
config = config.with_namespace("FOO")
assert config(key, alternate_keys=alternate_keys) == expected
def test_bound_config(self):
config = ConfigManager.from_dict({"foo": 12345})
class ComponentA(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option("foo", parser=int)
required_config.add_option("bar", parser=int, default="1")
def __init__(self, config):
self.config = config.with_options(self)
comp = ComponentA(config)
assert list(comp.get_runtime_config()) == [
([], "foo", "12345", Option(key="foo", parser=int)),
([], "bar", "1", Option(key="bar", parser=int, default="1")),
]
def test_config_from_dict():
config = ConfigManager.from_dict({})
assert config("FOO", raise_error=False) is NO_VALUE
config = ConfigManager.from_dict({"FOO": "bar"})
assert config("FOO", raise_error=False) == "bar"
def test_doc():
config = ConfigManager.from_dict({"FOO_BAR": "bat"})
class SomeComponent(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option("foo_bar", parser=int, doc="omg!")
def __init__(self, config):
self.config = config.with_options(self)
comp = SomeComponent(config)
try:
# This throws an exception becase "bat" is not an int
comp.config("foo_bar")
except Exception as exc:
# We're going to lazily assert that omg! is in exc msg because if it
# is, it came from the option and that's what we want to know.
def test_get_namespace():
config = ConfigManager.from_dict(
{"FOO": "abc", "FOO_BAR": "abc", "FOO_BAR_BAZ": "abc"}
)
assert config.get_namespace() == []
ns_foo_config = config.with_namespace("foo")
assert ns_foo_config.get_namespace() == ["foo"]
ns_foo_bar_config = ns_foo_config.with_namespace("bar")
assert ns_foo_bar_config.get_namespace() == ["foo", "bar"]
def test_nested_options():
"""Verify nested BoundOptions works."""
config = ConfigManager.from_dict({})
class Foo(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option("option1", default="opt1default", parser=str)
class Bar(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option("option2", default="opt2default", parser=str)
config = ConfigManager.basic_config()
config = config.with_options(Foo)
config = config.with_options(Bar)
assert config("option2") == "opt2default"
with pytest.raises(ConfigurationError):
config("option1")
required_config = ConfigOptions()
required_config.add_option(
'username',
alternate_keys=['root:db_username']
)
required_config.add_option(
'password',
alternate_keys=['root:db_password']
)
def __init__(self, config):
self.config = config.with_options(self)
# Define a shared configuration
config = ConfigManager.from_dict({
'DB_USERNAME': 'foo',
'DB_PASSWORD': 'bar'
})
reader = DBReader(config.with_namespace('reader'))
assert reader.config('username') == 'foo'
assert reader.config('password') == 'bar'
writer = DBWriter(config.with_namespace('writer'))
assert writer.config('username') == 'foo'
assert writer.config('password') == 'bar'
# Or define different credentials
config = ConfigManager.from_dict({
'READER_USERNAME': 'joe',
#!/usr/bin/env python3
import logging
from everett import InvalidValueError
from everett.manager import ConfigManager
logging.basicConfig()
config = ConfigManager.from_dict({
'debug_mode': 'monkey'
})
try:
some_val = config('debug_mode', parser=bool)
except InvalidValueError:
# The "debug_mode" configuration value is incorrect--alert
# user in the logs.
logging.exception('gah!')
self.read_dir = os.path.join(basedir, 'read')
class FSWriter(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option(
'file_type',
default='json'
)
def __init__(self, config, basedir):
self.config = config.with_options(self)
self.write_dir = os.path.join(basedir, 'write')
config = ConfigManager.from_dict({
'BASEDIR': '/tmp',
'READER': '__main__.FSReader',
'WRITER': '__main__.FSWriter',
'READER_FILE_TYPE': 'json',
'WRITER_FILE_TYPE': 'yaml'
})
app = App(config)
assert app.reader.read_dir == '/tmp/read'
assert app.writer.write_dir == '/tmp/write'