2020-10-16 20:57:28 +03:00
|
|
|
from pathlib import Path
|
2022-07-10 05:20:15 +03:00
|
|
|
|
2020-08-27 05:24:20 +03:00
|
|
|
from dependency_injector import providers
|
2022-07-10 05:20:15 +03:00
|
|
|
from pydantic import BaseSettings as PydanticSettings
|
2020-08-27 05:24:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Test 1: to check the getattr
|
|
|
|
config1 = providers.Configuration()
|
|
|
|
provider1 = providers.Factory(dict, a=config1.a)
|
|
|
|
|
|
|
|
# Test 2: to check the from_*() method
|
|
|
|
config2 = providers.Configuration()
|
2022-07-10 05:20:15 +03:00
|
|
|
config2_init_args = providers.Configuration(
|
|
|
|
name="config",
|
|
|
|
strict=True,
|
|
|
|
default={},
|
|
|
|
ini_files=["config.ini", Path("config.ini")],
|
|
|
|
yaml_files=["config.yml", Path("config.yml")],
|
|
|
|
json_files=["config.json", Path("config.json")],
|
|
|
|
pydantic_settings=[PydanticSettings()],
|
|
|
|
)
|
|
|
|
|
2020-08-27 05:24:20 +03:00
|
|
|
config2.from_dict({})
|
2022-07-10 05:20:15 +03:00
|
|
|
config2.from_value({})
|
|
|
|
|
2021-10-01 03:09:42 +03:00
|
|
|
config2.from_ini("config.ini")
|
|
|
|
config2.from_ini(Path("config.ini"))
|
2020-10-16 20:57:28 +03:00
|
|
|
|
2021-10-01 03:09:42 +03:00
|
|
|
config2.from_yaml("config.yml")
|
|
|
|
config2.from_yaml(Path("config.yml"))
|
2021-12-21 01:46:51 +03:00
|
|
|
|
2022-07-10 04:41:29 +03:00
|
|
|
config2.from_json("config.json")
|
|
|
|
config2.from_json(Path("config.json"))
|
|
|
|
|
2021-10-01 03:09:42 +03:00
|
|
|
config2.from_env("ENV", "default")
|
2021-12-21 01:46:51 +03:00
|
|
|
config2.from_env("ENV", as_=int, default=123)
|
|
|
|
config2.from_env("ENV", as_=float, required=True)
|
|
|
|
config2.from_env("ENV", as_=lambda env: str(env))
|
2020-08-27 05:24:20 +03:00
|
|
|
|
2022-07-10 05:20:15 +03:00
|
|
|
config2.from_pydantic(PydanticSettings())
|
|
|
|
|
2020-08-27 05:24:20 +03:00
|
|
|
# Test 3: to check as_*() methods
|
|
|
|
config3 = providers.Configuration()
|
|
|
|
int3: providers.Callable[int] = config3.option.as_int()
|
|
|
|
float3: providers.Callable[float] = config3.option.as_float()
|
|
|
|
int3_custom: providers.Callable[int] = config3.option.as_(int)
|
2021-01-16 16:53:40 +03:00
|
|
|
|
|
|
|
# Test 4: to check required() method
|
|
|
|
config4 = providers.Configuration()
|
|
|
|
option4: providers.ConfigurationOption = config4.option.required()
|