22
33import pytest
44
5- from modflow_devtools .dfn import Dfn , load
5+ from modflow_devtools .dfn import _load_common , load , load_all , load_tree
66from modflow_devtools .dfn .fetch import fetch_dfns
77from modflow_devtools .dfn2toml import convert
88from modflow_devtools .markers import requires_pkg
99
1010PROJ_ROOT = Path (__file__ ).parents [1 ]
1111DFN_DIR = PROJ_ROOT / "autotest" / "temp" / "dfn"
1212TOML_DIR = DFN_DIR / "toml"
13- VERSIONS = {1 : DFN_DIR , 2 : TOML_DIR }
13+ SPEC_DIRS = {1 : DFN_DIR , 2 : TOML_DIR }
1414MF6_OWNER = "MODFLOW-ORG"
1515MF6_REPO = "modflow6"
1616MF6_REF = "develop"
@@ -30,12 +30,13 @@ def pytest_generate_tests(metafunc):
3030 if "toml_name" in metafunc .fixturenames :
3131 convert (DFN_DIR , TOML_DIR )
3232 dfn_paths = list (DFN_DIR .glob ("*.dfn" ))
33- assert all (
34- ( TOML_DIR / f"{ dfn .stem .replace ('-nam' , '' )} .toml" ). is_file ()
33+ expected_toml_paths = [
34+ TOML_DIR / f"{ dfn .stem .replace ('-nam' , '' )} .toml"
3535 for dfn in dfn_paths
3636 if "common" not in dfn .stem
37- )
37+ ]
3838 toml_names = [toml .stem for toml in TOML_DIR .glob ("*.toml" )]
39+ assert all (toml_path .exists () for toml_path in expected_toml_paths )
3940 metafunc .parametrize ("toml_name" , toml_names , ids = toml_names )
4041
4142
@@ -45,23 +46,24 @@ def test_load_v1(dfn_name):
4546 (DFN_DIR / "common.dfn" ).open () as common_file ,
4647 (DFN_DIR / f"{ dfn_name } .dfn" ).open () as dfn_file ,
4748 ):
48- common , _ = load (common_file )
49+ common , _ = _load_common (common_file )
4950 dfn = load (dfn_file , name = dfn_name , common = common )
50- assert any (dfn )
51+ assert any (dfn . fields )
5152
5253
5354@requires_pkg ("boltons" )
5455def test_load_v2 (toml_name ):
5556 with (TOML_DIR / f"{ toml_name } .toml" ).open (mode = "rb" ) as toml_file :
56- toml = Dfn . load (toml_file , name = toml_name , version = 2 )
57- assert any (toml )
57+ dfn = load (toml_file , name = toml_name , format = "toml" )
58+ assert any (dfn . fields )
5859
5960
6061@requires_pkg ("boltons" )
61- @pytest .mark .parametrize ("version" , list (VERSIONS .keys ()))
62- def test_load_all (version ):
63- dfns = Dfn .load_all (VERSIONS [version ], version = version )
64- assert any (dfns )
62+ @pytest .mark .parametrize ("schema_version" , list (SPEC_DIRS .keys ()))
63+ def test_load_all (schema_version ):
64+ path = SPEC_DIRS [schema_version ]
65+ dfns = load_all (path )
66+ assert all (any (dfn .fields ) for dfn in dfns .values ())
6567
6668
6769@requires_pkg ("boltons" )
@@ -90,37 +92,34 @@ def test_load_tree():
9092 assert gwf_data ["name" ] == "gwf"
9193 assert gwf_data ["parent" ] == "sim"
9294
93- # Test hierarchy enforcement and completeness
94- dfns = Dfn .load_all (tmp_path , version = 2 )
95- roots = [name for name , dfn in dfns .items () if not dfn .get ("parent" )]
96- assert len (roots ) == 1
97- assert roots [0 ] == "sim"
98-
95+ dfns = load_all (tmp_path )
96+ root = load_tree (tmp_path )
97+ roots = []
9998 for dfn in dfns .values ():
100- parent = dfn .get ("parent" )
101- if parent :
102- assert parent in dfns
103-
104- # Test tree building and navigation
105- tree = Dfn .load_tree (tmp_path , version = 2 )
106- assert "sim" in tree
107- assert tree ["sim" ]["name" ] == "sim"
108-
109- for model_type in ["gwf" , "gwt" , "gwe" ]:
110- if model_type in tree ["sim" ]:
111- assert tree ["sim" ][model_type ]["name" ] == model_type
112- assert tree ["sim" ][model_type ]["parent" ] == "sim"
113-
114- if "gwf" in tree ["sim" ]:
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 {}
115116 gwf_packages = [
116- k
117- for k in tree ["sim" ]["gwf" ].keys ()
118- if k .startswith ("gwf-" ) and isinstance (tree ["sim" ]["gwf" ][k ], dict )
117+ k for k in pkgs if k .startswith ("gwf-" ) and isinstance (pkgs [k ], dict )
119118 ]
120119 assert len (gwf_packages ) > 0
121120
122- if "gwf- dis" in tree [ "sim" ][ " gwf" ] :
123- dis = tree [ "sim" ][ " gwf" ][ "gwf -dis"]
124- assert dis [ "name" ] == "gwf-dis "
125- assert dis [ "parent" ] == "gwf"
126- assert "options " in dis or "dimensions" in dis
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 {})
0 commit comments