mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-24 10:34:01 +03:00
94aca21fb8
* Add pytest and pytest-asyncio to the requirements * Update aiohttp ext test * Update setup.cfg * Update tox.ini * Add pytest to the tox requirements * Update tox.ini * Move configuration to tox.ini * Add pytest configs * Rename pytest-py34-py35.ini -> pytest-py35.ini * Update config file paths * Update makefile * Migrate common tests to pytest * Migrate FastAPI and Flask wiring tests * Rename flask and fastapi wiring test files * Move wiring autoloader tests * Add pytest-asyncio to the tox.ini * Migrate wiring async injection tests * Migrate main wiring tests * Migrate wiring string module and package names tests * Migrate wiring config tests * Migrate misc wiring tests * Update tests structure * Migrate misc wiring tests * Refactor container.from_schema() API tests * Migrate container.from_schema() integration tests * Rename schema samples * Update sample imports * Migrate container tests * Refactor container tests * Migrate container self tests * Migrate container instance tests * Migrate container custom string attribute name tests * Migrate container async resource tests * Fix py2 container tests * Migrate container cls tests * Migrate container class custom string cls as atrribute name tests * Migrate ext.aiohttp tests * Migrate ext.flasks tests * Update ext package tests doc block * Migrate provider utils tests * Migrate Factory async mode tests * Migrate async tests * Rename common test module * Refactor asserts in provider tests * Migrate factory tests * Migrate selector provider tests * Migrate object provider tests * Migrate self provider tests * Migrate delegate provider tests * Migrate provider tests * Migrate dependency provider tests * Migrate dependencies container provider tests * Fix warnings * Migrate list provider tests * Migrate dict provider tests * Migrate callable tests * Migrate injection tests * Migrate container provider tests * Migrate coroutine providers * Migrate traversal tests * Migrate resource tests * Migrate configuration tests * Migrate provided instance provider tests * Update doc blocks and imports * Migrate singleton tests * Update changelog and cosmetic fixes
185 lines
5.1 KiB
Python
185 lines
5.1 KiB
Python
"""Configuration.from_pydantic() tests."""
|
|
|
|
import pydantic
|
|
from dependency_injector import providers, errors
|
|
from pytest import fixture, mark, raises
|
|
|
|
|
|
class Section11(pydantic.BaseModel):
|
|
value1 = 1
|
|
|
|
|
|
class Section12(pydantic.BaseModel):
|
|
value2 = 2
|
|
|
|
|
|
class Settings1(pydantic.BaseSettings):
|
|
section1 = Section11()
|
|
section2 = Section12()
|
|
|
|
|
|
class Section21(pydantic.BaseModel):
|
|
value1 = 11
|
|
value11 = 11
|
|
|
|
|
|
class Section3(pydantic.BaseModel):
|
|
value3 = 3
|
|
|
|
|
|
class Settings2(pydantic.BaseSettings):
|
|
section1 = Section21()
|
|
section3 = Section3()
|
|
|
|
@fixture
|
|
def no_pydantic_module_installed():
|
|
providers.pydantic = None
|
|
yield
|
|
providers.pydantic = pydantic
|
|
|
|
|
|
def test(config):
|
|
config.from_pydantic(Settings1())
|
|
|
|
assert config() == {"section1": {"value1": 1}, "section2": {"value2": 2}}
|
|
assert config.section1() == {"value1": 1}
|
|
assert config.section1.value1() == 1
|
|
assert config.section2() == {"value2": 2}
|
|
assert config.section2.value2() == 2
|
|
|
|
|
|
def test_kwarg(config):
|
|
config.from_pydantic(Settings1(), exclude={"section2"})
|
|
|
|
assert config() == {"section1": {"value1": 1}}
|
|
assert config.section1() == {"value1": 1}
|
|
assert config.section1.value1() == 1
|
|
|
|
|
|
def test_merge(config):
|
|
config.from_pydantic(Settings1())
|
|
config.from_pydantic(Settings2())
|
|
|
|
assert config() == {
|
|
"section1": {
|
|
"value1": 11,
|
|
"value11": 11,
|
|
},
|
|
"section2": {
|
|
"value2": 2,
|
|
},
|
|
"section3": {
|
|
"value3": 3,
|
|
},
|
|
}
|
|
assert config.section1() == {"value1": 11, "value11": 11}
|
|
assert config.section1.value1() == 11
|
|
assert config.section1.value11() == 11
|
|
assert config.section2() == {"value2": 2}
|
|
assert config.section2.value2() == 2
|
|
assert config.section3() == {"value3": 3}
|
|
assert config.section3.value3() == 3
|
|
|
|
|
|
def test_empty_settings(config):
|
|
config.from_pydantic(pydantic.BaseSettings())
|
|
assert config() == {}
|
|
|
|
|
|
@mark.parametrize("config_type", ["strict"])
|
|
def test_empty_settings_strict_mode(config):
|
|
with raises(ValueError):
|
|
config.from_pydantic(pydantic.BaseSettings())
|
|
|
|
|
|
def test_option_empty_settings(config):
|
|
config.option.from_pydantic(pydantic.BaseSettings())
|
|
assert config.option() == {}
|
|
|
|
|
|
@mark.parametrize("config_type", ["strict"])
|
|
def test_option_empty_settings_strict_mode(config):
|
|
with raises(ValueError):
|
|
config.option.from_pydantic(pydantic.BaseSettings())
|
|
|
|
|
|
def test_required_empty_settings(config):
|
|
with raises(ValueError):
|
|
config.from_pydantic(pydantic.BaseSettings(), required=True)
|
|
|
|
|
|
def test_required_option_empty_settings(config):
|
|
with raises(ValueError):
|
|
config.option.from_pydantic(pydantic.BaseSettings(), required=True)
|
|
|
|
|
|
@mark.parametrize("config_type", ["strict"])
|
|
def test_not_required_empty_settings_strict_mode(config):
|
|
config.from_pydantic(pydantic.BaseSettings(), required=False)
|
|
assert config() == {}
|
|
|
|
|
|
@mark.parametrize("config_type", ["strict"])
|
|
def test_not_required_option_empty_settings_strict_mode(config):
|
|
config.option.from_pydantic(pydantic.BaseSettings(), required=False)
|
|
assert config.option() == {}
|
|
assert config() == {"option": {}}
|
|
|
|
|
|
def test_not_instance_of_settings(config):
|
|
with raises(errors.Error) as error:
|
|
config.from_pydantic({})
|
|
assert error.value.args[0] == (
|
|
"Unable to recognize settings instance, expect \"pydantic.BaseSettings\", "
|
|
"got {0} instead".format({})
|
|
)
|
|
|
|
|
|
def test_option_not_instance_of_settings(config):
|
|
with raises(errors.Error) as error:
|
|
config.option.from_pydantic({})
|
|
assert error.value.args[0] == (
|
|
"Unable to recognize settings instance, expect \"pydantic.BaseSettings\", "
|
|
"got {0} instead".format({})
|
|
)
|
|
|
|
|
|
def test_subclass_instead_of_instance(config):
|
|
with raises(errors.Error) as error:
|
|
config.from_pydantic(Settings1)
|
|
assert error.value.args[0] == (
|
|
"Got settings class, but expect instance: "
|
|
"instead \"Settings1\" use \"Settings1()\""
|
|
)
|
|
|
|
|
|
def test_option_subclass_instead_of_instance(config):
|
|
with raises(errors.Error) as error:
|
|
config.option.from_pydantic(Settings1)
|
|
assert error.value.args[0] == (
|
|
"Got settings class, but expect instance: "
|
|
"instead \"Settings1\" use \"Settings1()\""
|
|
)
|
|
|
|
|
|
@mark.usefixtures("no_pydantic_module_installed")
|
|
def test_no_pydantic_installed(config):
|
|
with raises(errors.Error) as error:
|
|
config.from_pydantic(Settings1())
|
|
assert error.value.args[0] == (
|
|
"Unable to load pydantic configuration - pydantic is not installed. "
|
|
"Install pydantic or install Dependency Injector with pydantic extras: "
|
|
"\"pip install dependency-injector[pydantic]\""
|
|
)
|
|
|
|
|
|
@mark.usefixtures("no_pydantic_module_installed")
|
|
def test_option_no_pydantic_installed(config):
|
|
with raises(errors.Error) as error:
|
|
config.option.from_pydantic(Settings1())
|
|
assert error.value.args[0] == (
|
|
"Unable to load pydantic configuration - pydantic is not installed. "
|
|
"Install pydantic or install Dependency Injector with pydantic extras: "
|
|
"\"pip install dependency-injector[pydantic]\""
|
|
)
|