22
33import pytest
44
5- from modflow_devtools .dfn import _load_common , load , load_all , load_tree
5+ from modflow_devtools .dfn import _load_common , load , load_all
66from modflow_devtools .dfn .fetch import fetch_dfns
77from modflow_devtools .dfn2toml import convert
88from modflow_devtools .markers import requires_pkg
@@ -29,14 +29,11 @@ def pytest_generate_tests(metafunc):
2929
3030 if "toml_name" in metafunc .fixturenames :
3131 convert (DFN_DIR , TOML_DIR )
32- dfn_paths = list (DFN_DIR .glob ("*.dfn" ))
3332 expected_toml_paths = [
34- TOML_DIR / f"{ dfn .stem .replace ('-nam' , '' )} .toml"
35- for dfn in dfn_paths
36- if "common" not in dfn .stem
33+ dfn for dfn in DFN_DIR .glob ("*.dfn" ) if "common" not in dfn .stem
3734 ]
38- toml_names = [toml .stem for toml in TOML_DIR .glob ("*.toml" )]
3935 assert all (toml_path .exists () for toml_path in expected_toml_paths )
36+ toml_names = [toml .stem for toml in TOML_DIR .glob ("*.toml" )]
4037 metafunc .parametrize ("toml_name" , toml_names , ids = toml_names )
4138
4239
@@ -47,7 +44,7 @@ def test_load_v1(dfn_name):
4744 (DFN_DIR / f"{ dfn_name } .dfn" ).open () as dfn_file ,
4845 ):
4946 common , _ = _load_common (common_file )
50- dfn = load (dfn_file , name = dfn_name , common = common )
47+ dfn = load (dfn_file , name = dfn_name , format = "dfn" , common = common )
5148 assert any (dfn .fields )
5249
5350
@@ -61,65 +58,57 @@ def test_load_v2(toml_name):
6158@requires_pkg ("boltons" )
6259@pytest .mark .parametrize ("schema_version" , list (SPEC_DIRS .keys ()))
6360def test_load_all (schema_version ):
64- path = SPEC_DIRS [schema_version ]
65- dfns = load_all (path )
61+ dfns = load_all (path = SPEC_DIRS [schema_version ])
6662 assert all (any (dfn .fields ) for dfn in dfns .values ())
6763
6864
69- @requires_pkg ("boltons" )
70- def test_load_tree ():
71- import tempfile
72-
65+ @requires_pkg ("boltons" , "tomli" )
66+ def test_convert (function_tmpdir ):
7367 import tomli
7468
75- with tempfile .TemporaryDirectory () as tmp_dir :
76- tmp_path = Path (tmp_dir )
77- convert (DFN_DIR , tmp_path )
78-
79- # Test file conversion and naming
80- assert (tmp_path / "sim.toml" ).exists ()
81- assert (tmp_path / "gwf.toml" ).exists ()
82- assert not (tmp_path / "sim-nam.toml" ).exists ()
83-
84- # Test parent relationships in files
85- with (tmp_path / "sim.toml" ).open ("rb" ) as f :
86- sim_data = tomli .load (f )
87- assert sim_data ["name" ] == "sim"
88- assert "parent" not in sim_data
89-
90- with (tmp_path / "gwf.toml" ).open ("rb" ) as f :
91- gwf_data = tomli .load (f )
92- assert gwf_data ["name" ] == "gwf"
93- assert gwf_data ["parent" ] == "sim"
94-
95- dfns = load_all (tmp_path )
96- root = load_tree (tmp_path )
97- roots = []
98- for dfn in dfns .values ():
99- if dfn .parent :
100- assert dfn .parent in dfns
101- else :
102- roots .append (dfn .name )
103- assert len (roots ) == 1
104- assert root .name == "sim"
105- assert root == roots [0 ]
106-
107- model_types = ["gwf" , "gwt" , "gwe" ]
108- models = root .children or {}
109- for model_type in model_types :
110- if model_type in models :
111- assert models [model_type ].name == model_type
112- assert models [model_type ].parent == "sim"
113-
114- if "gwf" in models :
115- pkgs = models ["gwf" ].children or {}
116- gwf_packages = [
117- k for k in pkgs if k .startswith ("gwf-" ) and isinstance (pkgs [k ], dict )
118- ]
119- assert len (gwf_packages ) > 0
120-
121- if dis := pkgs .get ("gwf-dis" , None ):
122- assert dis .name == "gwf-dis"
123- assert dis .parent == "gwf"
124- assert "options" in (dis .blocks or {})
125- assert "dimensions" in (dis .blocks or {})
69+ convert (DFN_DIR , function_tmpdir )
70+
71+ assert (function_tmpdir / "sim-nam.toml" ).exists ()
72+ assert (function_tmpdir / "gwf-nam.toml" ).exists ()
73+
74+ with (function_tmpdir / "sim-nam.toml" ).open ("rb" ) as f :
75+ sim_data = tomli .load (f )
76+ assert sim_data ["name" ] == "sim-nam"
77+ assert sim_data ["schema_version" ] == "2"
78+ assert "parent" not in sim_data
79+
80+ with (function_tmpdir / "gwf-nam.toml" ).open ("rb" ) as f :
81+ gwf_data = tomli .load (f )
82+ assert gwf_data ["name" ] == "gwf-nam"
83+ assert gwf_data ["parent" ] == "sim-nam"
84+ assert gwf_data ["schema_version" ] == "2"
85+
86+ dfns = load_all (function_tmpdir )
87+ roots = []
88+ for dfn in dfns .values ():
89+ if dfn .parent :
90+ assert dfn .parent in dfns
91+ else :
92+ roots .append (dfn .name )
93+ assert len (roots ) == 1
94+ root = dfns [roots [0 ]]
95+ assert root .name == "sim-nam"
96+
97+ models = root .children or {}
98+ for mdl in models :
99+ assert models [mdl ].name == mdl
100+ assert models [mdl ].parent == "sim-nam"
101+
102+ if gwf := models .get ("gwf-nam" , None ):
103+ pkgs = gwf .children or {}
104+ pkgs = {
105+ k : v
106+ for k , v in pkgs .items ()
107+ if k .startswith ("gwf-" ) and isinstance (v , dict )
108+ }
109+ assert len (pkgs ) > 0
110+ if dis := pkgs .get ("gwf-dis" , None ):
111+ assert dis .name == "gwf-dis"
112+ assert dis .parent == "gwf"
113+ assert "options" in (dis .blocks or {})
114+ assert "dimensions" in (dis .blocks or {})
0 commit comments