Skip to content

Commit

Permalink
Migrate configuration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmk135 committed Oct 18, 2021
1 parent 98cb4ab commit b44a624
Show file tree
Hide file tree
Showing 14 changed files with 1,504 additions and 1,445 deletions.
1 change: 1 addition & 0 deletions tests/unit/providers/configuration/__init__.py
@@ -0,0 +1 @@
"""Configuration provider tests."""
19 changes: 19 additions & 0 deletions tests/unit/providers/configuration/conftest.py
@@ -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 tests/unit/providers/configuration/test_config_linking_py2_py3.py
@@ -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"

0 comments on commit b44a624

Please sign in to comment.