basic structure for lazy config loading
This commit is contained in:
2
tests/_test_data/dopt-cfg.toml
Normal file
2
tests/_test_data/dopt-cfg.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[forecast]
|
||||
threshold_month_data_points = 28
|
||||
@@ -33,6 +33,15 @@ def api_base_url(credentials) -> str:
|
||||
return credentials["base_url"]
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def pth_dummy_cfg() -> Path:
|
||||
pwd = Path.cwd()
|
||||
assert "barth" in pwd.parent.name.lower(), "not in project root directory"
|
||||
data_pth = pwd / "./tests/_test_data/dopt-cfg.toml"
|
||||
assert data_pth.exists(), "file to dummy CFG not found"
|
||||
return data_pth
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def sales_data_real() -> pd.DataFrame:
|
||||
pwd = Path.cwd()
|
||||
|
||||
58
tests/test_config.py
Normal file
58
tests/test_config.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import tomllib
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import tomli_w
|
||||
|
||||
from delta_barth import config
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def pth_cfg(pth_dummy_cfg, tmp_path) -> Path:
|
||||
with open(pth_dummy_cfg, "rb") as file:
|
||||
cfg_data = tomllib.load(file)
|
||||
|
||||
target = tmp_path / "dummy_cfg.toml"
|
||||
target.touch()
|
||||
with open(target, "wb") as file:
|
||||
tomli_w.dump(cfg_data, file)
|
||||
|
||||
return target
|
||||
|
||||
|
||||
@pytest.mark.new
|
||||
def test_CfgLoader_Init(pth_cfg):
|
||||
loader = config.LazyCfgLoader(pth_cfg)
|
||||
|
||||
assert loader.path == pth_cfg
|
||||
assert loader._cfg is None
|
||||
|
||||
|
||||
@pytest.mark.new
|
||||
def test_CfgLoader_Get(pth_cfg):
|
||||
loader = config.LazyCfgLoader(pth_cfg)
|
||||
|
||||
parsed_cfg = loader.get()
|
||||
assert isinstance(parsed_cfg, config.Config)
|
||||
assert parsed_cfg.forecast.threshold_month_data_points == 28
|
||||
|
||||
|
||||
@pytest.mark.new
|
||||
def test_CfgLoader_Reload(pth_cfg):
|
||||
loader = config.LazyCfgLoader(pth_cfg)
|
||||
|
||||
parsed_cfg = loader.get()
|
||||
assert isinstance(parsed_cfg, config.Config)
|
||||
assert parsed_cfg.forecast.threshold_month_data_points == 28
|
||||
# modify config and reload
|
||||
with open(pth_cfg, "rb") as file:
|
||||
cfg_data = tomllib.load(file)
|
||||
cfg_data["forecast"]["threshold_month_data_points"] = 30
|
||||
with open(pth_cfg, "wb") as file:
|
||||
tomli_w.dump(cfg_data, file)
|
||||
|
||||
assert parsed_cfg.forecast.threshold_month_data_points == 28
|
||||
loader.reload()
|
||||
parsed_cfg = loader.get()
|
||||
assert isinstance(parsed_cfg, config.Config)
|
||||
assert parsed_cfg.forecast.threshold_month_data_points == 30
|
||||
Reference in New Issue
Block a user