Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,504 additions
and
1,445 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Configuration provider tests.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Fixtures module.""" | ||
|
||
from dependency_injector import providers | ||
from pytest import fixture | ||
|
||
|
||
@fixture | ||
def config_type(): | ||
return "default" | ||
|
||
|
||
@fixture | ||
def config(config_type): | ||
if config_type == "strict": | ||
return providers.Configuration(strict=True) | ||
elif config_type == "default": | ||
return providers.Configuration() | ||
else: | ||
raise ValueError("Undefined config type \"{0}\"".format(config_type)) |
130 changes: 130 additions & 0 deletions
130
tests/unit/providers/configuration/test_config_linking_py2_py3.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
"""Tests for configuration provider linking.""" | ||
|
||
|
||
from dependency_injector import containers, providers | ||
|
||
|
||
class Core(containers.DeclarativeContainer): | ||
config = providers.Configuration("core") | ||
value_getter = providers.Callable(lambda _: _, config.value) | ||
|
||
|
||
class Services(containers.DeclarativeContainer): | ||
config = providers.Configuration("services") | ||
value_getter = providers.Callable(lambda _: _, config.value) | ||
|
||
|
||
def test(): | ||
root_config = providers.Configuration("main") | ||
core = Core(config=root_config.core) | ||
services = Services(config=root_config.services) | ||
|
||
root_config.override( | ||
{ | ||
"core": { | ||
"value": "core", | ||
}, | ||
"services": { | ||
"value": "services", | ||
}, | ||
}, | ||
) | ||
|
||
assert core.config() == {"value": "core"} | ||
assert core.config.value() == "core" | ||
assert core.value_getter() == "core" | ||
|
||
assert services.config() == {"value": "services"} | ||
assert services.config.value() == "services" | ||
assert services.value_getter() == "services" | ||
|
||
|
||
def test_double_override(): | ||
root_config = providers.Configuration("main") | ||
core = Core(config=root_config.core) | ||
services = Services(config=root_config.services) | ||
|
||
root_config.override( | ||
{ | ||
"core": { | ||
"value": "core1", | ||
}, | ||
"services": { | ||
"value": "services1", | ||
}, | ||
}, | ||
) | ||
root_config.override( | ||
{ | ||
"core": { | ||
"value": "core2", | ||
}, | ||
"services": { | ||
"value": "services2", | ||
}, | ||
}, | ||
) | ||
|
||
assert core.config() == {"value": "core2"} | ||
assert core.config.value() == "core2" | ||
assert core.value_getter() == "core2" | ||
|
||
assert services.config() == {"value": "services2"} | ||
assert services.config.value() == "services2" | ||
assert services.value_getter() == "services2" | ||
|
||
|
||
def test_reset_overriding_cache(): | ||
# See: https://github.com/ets-labs/python-dependency-injector/issues/428 | ||
class Core(containers.DeclarativeContainer): | ||
config = providers.Configuration() | ||
|
||
greetings = providers.Factory(str, config.greeting) | ||
|
||
class Application(containers.DeclarativeContainer): | ||
config = providers.Configuration() | ||
|
||
core = providers.Container( | ||
Core, | ||
config=config, | ||
) | ||
|
||
greetings = providers.Factory(str, config.greeting) | ||
|
||
container = Application() | ||
|
||
container.config.set("greeting", "Hello World") | ||
assert container.greetings() == "Hello World" | ||
assert container.core.greetings() == "Hello World" | ||
|
||
container.config.set("greeting", "Hello Bob") | ||
assert container.greetings() == "Hello Bob" | ||
assert container.core.greetings() == "Hello Bob" | ||
|
||
|
||
def test_reset_overriding_cache_for_option(): | ||
# See: https://github.com/ets-labs/python-dependency-injector/issues/428 | ||
class Core(containers.DeclarativeContainer): | ||
config = providers.Configuration() | ||
|
||
greetings = providers.Factory(str, config.greeting) | ||
|
||
class Application(containers.DeclarativeContainer): | ||
config = providers.Configuration() | ||
|
||
core = providers.Container( | ||
Core, | ||
config=config.option, | ||
) | ||
|
||
greetings = providers.Factory(str, config.option.greeting) | ||
|
||
container = Application() | ||
|
||
container.config.set("option.greeting", "Hello World") | ||
assert container.greetings() == "Hello World" | ||
assert container.core.greetings() == "Hello World" | ||
|
||
container.config.set("option.greeting", "Hello Bob") | ||
assert container.greetings() == "Hello Bob" | ||
assert container.core.greetings() == "Hello Bob" |
Oops, something went wrong.