1
+ import os
1
2
from pathlib import Path
2
3
from typing import Optional
3
4
@@ -24,6 +25,11 @@ def yaml_data() -> dict:
24
25
return {"key2" : "value2" , "key1" : "value1" , "key3" : ["a" , "b" ], "key4" : None }
25
26
26
27
28
+ @pytest .fixture
29
+ def other_yaml_data () -> dict :
30
+ return {"other_key" : "hello" }
31
+
32
+
27
33
@pytest .fixture
28
34
def yaml_str (yaml_data ) -> str :
29
35
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):
51
57
OtherTempYamlModel .load_from (yaml_file )
52
58
53
59
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
+
54
81
def test_save_to (yaml_data , yaml_str , tmp_path ):
55
82
model = TempYamlModel (** yaml_data )
56
83
yaml_file = tmp_path / "saved.yaml"
0 commit comments