Skip to content

Commit

Permalink
fix(transforms.py): Fix load_from_file and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Vasenin committed May 11, 2021
1 parent 1bfd6b4 commit e47e809
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ntc/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,13 @@ def clear(self) -> None:
for key in list(self.keys()):
del self[key]

def update(self, new_dict: dict) -> None:
for key, value in new_dict.items():
if key in self and isinstance(self[key], (UserDict, dict)):
self[key].update(value)
else:
self[key] = value


CN = CfgNode

Expand Down
2 changes: 2 additions & 0 deletions tests/data/transforms/extra.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DICT:
FOO: "bar"
9 changes: 9 additions & 0 deletions tests/data/transforms/load_from_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pathlib import Path

from ntc.transforms import load_from_file

from ..base_cfg import cfg

cfg = cfg.inherit()

cfg.add_transform(load_from_file(Path(__file__).parent / "extra.yaml"))
5 changes: 5 additions & 0 deletions tests/data/transforms/load_from_file_changes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from ntc import CN
from tests.data.transforms.load_from_file import cfg

cfg = CN(cfg)
cfg.DICT.FOO = "zoo"
10 changes: 10 additions & 0 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pathlib import Path

from ntc import CN

DATA_DIR = Path(__file__).parent / "data" / "transforms"


def test_load_from_file():
cfg = CN.load(DATA_DIR / "load_from_file_changes.py")
assert cfg.DICT.FOO == "bar"

0 comments on commit e47e809

Please sign in to comment.