refactor(config): add set_config_value for non-merge replacement
This commit is contained in:
1 parent
fb05409403
commit
d124a7c896
3 files changed
+78
-19
No files matched your search
@@ -0,0 +1,45 @@
|
||||
"""Unit test for config.set_config_value (replaces, doesn't merge)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import yaml
|
||||
|
||||
from presa_mano.config import (
|
||||
set_config_value,
|
||||
get_config,
|
||||
load_config,
|
||||
_STATE,
|
||||
_default_config_path,
|
||||
)
|
||||
|
||||
|
||||
def test_replace_nested_dict_removes_old_keys(tmp_path, monkeypatch):
|
||||
cfg_file = tmp_path / "cfg.yaml"
|
||||
cfg_file.write_text(yaml.safe_dump({
|
||||
"section": {"items": {"a": 1, "b": 2}},
|
||||
}))
|
||||
monkeypatch.setenv("PRESA_MANO_CONFIG", str(cfg_file))
|
||||
# Reset in-memory singleton
|
||||
_STATE["config"] = None
|
||||
_STATE["path"] = None
|
||||
load_config()
|
||||
|
||||
set_config_value(["section", "items"], {"c": 3})
|
||||
|
||||
out = get_config()
|
||||
assert out["section"]["items"] == {"c": 3}
|
||||
# File on disk also updated
|
||||
on_disk = yaml.safe_load(cfg_file.read_text())
|
||||
assert on_disk["section"]["items"] == {"c": 3}
|
||||
|
||||
|
||||
def test_create_intermediate_dicts(tmp_path, monkeypatch):
|
||||
cfg_file = tmp_path / "cfg.yaml"
|
||||
cfg_file.write_text(yaml.safe_dump({}))
|
||||
monkeypatch.setenv("PRESA_MANO_CONFIG", str(cfg_file))
|
||||
_STATE["config"] = None
|
||||
_STATE["path"] = None
|
||||
load_config()
|
||||
|
||||
set_config_value(["a", "b", "c"], 42)
|
||||
|
||||
assert get_config() == {"a": {"b": {"c": 42}}}
|
||||
Reference in new issue
Block a user