def test_comma_separated_list_empty(self): self.create_fixture(typed(comma_separated_list, mock_value="")) loader = load_from_dict() metadata = Metadata("test", testing=True) config = configure(self.registry.defaults, metadata, loader) assert_that(config, has_entries(foo=has_entries(value=[], ), ))
def create_object_graph(name, debug=False, testing=False, import_name=None, root_path=None, loader=load_from_environ, registry=_registry): """ Create a new object graph. :param name: the name of the microservice :param debug: is development debugging enabled? :param testing: is unit testing enabled? :param loader: the configuration loader to use :param registry: the registry to use (defaults to the global) """ metadata = Metadata( name=name, debug=debug, testing=testing, import_name=import_name, root_path=root_path, ) config = Configuration( {key: get_defaults(value) for key, value in registry.all.items()}) config.merge(loader(metadata)) return ObjectGraph( metadata=metadata, config=config, registry=registry, )
def test_mock_value(self): self.create_fixture(required(boolean, mock_value="true")) loader = load_from_dict() metadata = Metadata("test", testing=True) config = configure(self.registry.defaults, metadata, loader) assert_that(config, has_entries(foo=has_entries(value=True, ), ))
def test_unsupported_arg(self): self.create_fixture(value=required(int, mock_value=0, spaghetti="foo")) loader = load_from_dict() metadata = Metadata("test", testing=True) config = configure(self.registry.defaults, metadata, loader) assert_that(config, has_entries(foo=has_entries(value=0, ), ))
def load_secrets(metadata): """ Load secrets using injected environment variables. Generate an "internal" naming prefix to differentiate secrets from other config. """ return load_from_environ(Metadata(f"_{metadata.name}"))
def test_load_from_environ_multipart_name(): """ Return configuration from environment. """ metadata = Metadata("foo-bar") with envvar("FOO_BAR__BAZ", "blah"): config = load_from_environ(metadata) assert_that(config, is_(equal_to({"baz": "blah"})))
def test_load_from_environ_json(): """ Return json configuration from environment. """ metadata = Metadata("foo") with envvar("FOO__BAR", '["baz"]'): with envvar("FOO__BAZ", 'true'): config = load_from_environ_as_json(metadata) assert_that(config, is_(equal_to({"bar": ["baz"], "baz": True})))
def test_empty_loader(): """ Return an empty loader. """ metadata = Metadata("foo") loader = load_each(empty_loader) config = loader(metadata) assert_that(config, is_(equal_to({})))
def test_comma_separated_list_unconverted(self): self.create_fixture(value=required(comma_separated_list, mock_value=["abc", "def", "ghi"])) loader = load_from_dict() metadata = Metadata("test", testing=True) config = configure(self.registry.defaults, metadata, loader) assert_that( config, has_entries(foo=has_entries(value=["abc", "def", "ghi"], ), ))
def test_load_partitioned(): metadata = Metadata("foo") loader = load_partitioned( foo=load_from_dict( foo=dict(value="foo", ), baz=dict( value="foo", foo=dict(value="foo", ), ), ), bar=load_from_dict( bar=dict(value="bar", ), baz=dict( value="bar", bar=dict(value="bar", ), ), ), ) config = loader(metadata) # configuration is loaded as a usual merge assert_that( config, is_( equal_to( dict( foo=dict(value="foo", ), bar=dict(value="bar", ), baz=dict( value="bar", foo=dict(value="foo", ), bar=dict(value="bar", ), ), )))) # loader retains partitions assert_that( loader.foo, is_( equal_to( dict( foo=dict(value="foo", ), baz=dict(foo=dict(value="foo", ), ), )))) assert_that( loader.bar, is_( equal_to( dict( bar=dict(value="bar", ), baz=dict( value="bar", bar=dict(value="bar", ), ), ))))
def test_two_stage_loader_basic(): metadata = Metadata("foo") initial_loader = load_from_dict(foo="bar", ) loader = two_stage_loader(initial_loader, secondary_loader) config = loader(metadata) assert_that(config, is_(equal_to(dict( foo="bar", bar="bazman", ))))
def test_nullable(self): self.create_fixture(value=typed( int, default_value=0, nullable=True, mock_value=None, )) loader = load_from_dict() metadata = Metadata("test", testing=True) config = configure(self.registry.defaults, metadata, loader) assert_that(config, has_entries(foo=has_entries(value=None, ), ))
def test_two_stage_loader_prefer_primary(): metadata = Metadata("foo") initial_loader = load_from_dict( foo="bar", bar="hello", ) loader = two_stage_loader( initial_loader, secondary_loader, prefer_secondary=False, ) config = loader(metadata) assert_that(config, is_(equal_to(dict( foo="bar", bar="hello", ))))
def create_object_graph(name, debug=False, testing=False, import_name=None, root_path=None, loader=load_from_environ, registry=_registry, profiler=None, cache=None): """ Create a new object graph. :param name: the name of the microservice :param debug: is development debugging enabled? :param testing: is unit testing enabled? :param loader: the configuration loader to use :param registry: the registry to use (defaults to the global) """ metadata = Metadata( name=name, debug=debug, testing=testing, import_name=import_name, root_path=root_path, ) defaults = registry.defaults config = configure(defaults, metadata, loader) if profiler is None: profiler = NoopProfiler() if cache is None or isinstance(cache, str): cache = create_cache(cache) return ObjectGraph( metadata=metadata, config=config, registry=registry, profiler=profiler, cache=cache, loader=loader, )
def test_load_from_environ(): """ Return configuration from environment. """ metadata = Metadata("foo-dow") with envvar("FOO_DOW__BAR", "baz"): with envvar("FOO_DOW__BAZ", "bar"): with envvar("FOO_DOW__BAR_BAZ__THIS", "that"): config = load_from_environ(metadata) assert_that( config, is_(equal_to({ "bar": "baz", "baz": "bar", "bar_baz": { "this": "that" } })))
def test_load_each(): """ Return the merged union of two loaders. """ metadata = Metadata("foo") with settings(dumps(dict(foo="bar"))) as settings_: with envvar("FOO__SETTINGS", settings_.name): with envvar("FOO__BAR", "baz"): loader = load_each(load_from_json_file, load_from_environ) config = loader(metadata) assert_that( config, is_(equal_to({ "bar": "baz", "foo": "bar", "settings": settings_.name })))
def test_load_multiple_values_for_on_componentfrom_environ(): """ Return configuration from environment. """ metadata = Metadata("foo") with envvar("FOO__BAR", "baz"): with envvar("FOO__FOO__THIS", "that"): with envvar("FOO__FOO__THAT", "this"): config = load_from_environ(metadata) assert_that( config, is_(equal_to({ "bar": "baz", "foo": { "this": "that", "that": "this" } })))
def test_load_config_and_secrets(): metadata = Metadata("foo") loader = load_config_and_secrets( config=load_from_dict(credentials=dict(username="******", ), ), secrets=load_from_dict(credentials=dict(password="******", ), ), ) config = loader(metadata) # configuration is loaded as a usual merge assert_that( config, is_( equal_to( dict(credentials=dict( username="******", password="******", ), )))) assert_that(loader.config, is_(equal_to(dict(credentials=dict(username="******", ), )))) assert_that(loader.secrets, is_(equal_to(dict(credentials=dict(password="******", ), ))))
def setup(self): self.metadata = Metadata("test") self.registry = Registry()