Skip to content

Commit cf8031f

Browse files
committed
Add test for robust loading from directories
1 parent a46aa92 commit cf8031f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

midi_app_controller/models/_tests/test_utils.py

+27
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from pathlib import Path
23
from typing import Optional
34

@@ -24,6 +25,11 @@ def yaml_data() -> dict:
2425
return {"key2": "value2", "key1": "value1", "key3": ["a", "b"], "key4": None}
2526

2627

28+
@pytest.fixture
29+
def other_yaml_data() -> dict:
30+
return {"other_key": "hello"}
31+
32+
2733
@pytest.fixture
2834
def yaml_str(yaml_data) -> str:
2935
dumped = yaml.safe_dump(yaml_data, default_flow_style=False, sort_keys=False)
@@ -51,6 +57,27 @@ def test_load_from_when_invalid_data(yaml_file, yaml_data):
5157
OtherTempYamlModel.load_from(yaml_file)
5258

5359

60+
def test_load_all_from_robustness(tmp_path, yaml_data, other_yaml_data):
61+
d1 = tmp_path / "1"
62+
os.mkdir(d1)
63+
d2 = tmp_path / "2"
64+
os.mkdir(d2)
65+
non_existent_dir = tmp_path / "none"
66+
with open(d1 / "m1.yaml", "w") as f:
67+
yaml.safe_dump(yaml_data, f)
68+
with open(d1 / "m2.yaml", "w") as f:
69+
yaml.safe_dump(other_yaml_data, f)
70+
with open(d2 / "m1.yml", "w") as f:
71+
yaml.safe_dump(yaml_data, f)
72+
with open(d2 / "distractor.txt", "w") as f:
73+
f.write("not relevant\n")
74+
with pytest.warns(UserWarning, match="Unable to load model"):
75+
models = TempYamlModel.load_all_from([d1, d2, non_existent_dir])
76+
assert len(models) == 2
77+
assert models[0][1] == d1 / "m1.yaml"
78+
assert models[1][1] == d2 / "m1.yml"
79+
80+
5481
def test_save_to(yaml_data, yaml_str, tmp_path):
5582
model = TempYamlModel(**yaml_data)
5683
yaml_file = tmp_path / "saved.yaml"

0 commit comments

Comments
 (0)