22
33import sys
44
5+ import numpy as np
56import pytest
67from conftest import EXAMPLES_PATH
78from modflow_devtools .misc import run_cmd
@@ -19,38 +20,74 @@ def pytest_generate_tests(metafunc):
1920 metafunc .parametrize ("example_script" , scripts .values (), ids = scripts .keys ())
2021
2122
22- def check_heads (compare_fpth , check_path ):
23- import numpy as np
23+ def compare_heads (compare_fpth , check_path ):
2424 from flopy .utils import HeadFile
2525
26- if not compare_fpth .exists () :
27- return
28-
29- hds_compare = np .load (compare_fpth )
26+ if compare_fpth .suffix == ".hds" :
27+ hds_compare = HeadFile ( compare_fpth , precision = "double" ). get_data ()
28+ elif compare_fpth . suffix == ".npy" :
29+ hds_compare = np .load (compare_fpth )
3030
3131 # check *.hds files
3232 for f in check_path .rglob ("*.hds" ):
3333 hds = HeadFile (f , precision = "double" )
3434 assert np .allclose (hds_compare , hds .get_data ())
3535
3636
37- def check (example_script ):
37+ def compare_grb (compare_fpth , check_path ):
38+ from flopy .mf6 .utils import MfGrdFile
39+
40+ grb_compare = MfGrdFile (compare_fpth )._datadict
41+
42+ # check *.grb files
43+ for f in check_path .rglob ("*.grb" ):
44+ grb = MfGrdFile (f )
45+ np .testing .assert_equal (grb_compare , grb ._datadict )
46+
47+
48+ def compare_bud (compare_fpth , check_path ):
49+ from flopy .utils .binaryfile import CellBudgetFile
50+
51+ bud_compare = CellBudgetFile (compare_fpth )
52+
53+ # check *.bud and *.cbc files
54+ pattern = ["*.bud" , "*.cbc" ]
55+ files = [f for p in pattern for f in check_path .rglob (p )]
56+ for f in files :
57+ bud = CellBudgetFile (f )
58+ names = bud_compare .get_unique_record_names ()
59+ for n in names :
60+ bud_cmp = bud_compare .get_data (text = n )
61+ bud_chk = bud .get_data (text = n )
62+ if isinstance (bud_cmp [0 ], np .rec .recarray ):
63+ assert len (bud_cmp ) == len (bud_chk )
64+ for ra_cmp , ra in zip (bud_cmp , bud_chk ):
65+ assert len (ra_cmp ) == len (ra )
66+ for i , rec in enumerate (ra_cmp ):
67+ assert all (np .allclose (x , y ) for x , y in zip (rec , ra [i ]))
68+ else :
69+ assert np .allclose (bud_compare .get_data (text = n ), bud .get_data (text = n ))
70+
71+
72+ def compare (example_script ):
3873 from pathlib import Path
3974
4075 test_name = "test_examples"
4176 check_path = Path (f"{ example_script .parent } /{ example_script .stem } " )
42- test_dir = Path (f"{ example_script .parent } /../../ test" )
77+ compare_path = Path (f"{ example_script .parent . parent . parent } / test/__compare__/ { test_name } " )
4378
44- # checks; assume test output path parent has example name
45- check_heads (
46- test_dir / f"__compare__/{ test_name } /{ example_script .stem } .hds.npy" ,
47- check_path ,
48- )
79+ for f in compare_path .glob (f"{ example_script .stem } .*" ):
80+ if f .suffix == ".bud" or f .suffix == ".cbc" :
81+ compare_bud (f , check_path )
82+ if f .suffix == ".hds" or f .match ("*hds.npy" ):
83+ compare_heads (f , check_path )
84+ elif f .suffix == ".grb" :
85+ compare_grb (f , check_path )
4986
5087
5188@pytest .mark .slow
5289def test_scripts (example_script ):
5390 args = [sys .executable , example_script ]
5491 stdout , stderr , retcode = run_cmd (* args , verbose = True )
5592 assert not retcode , stdout + stderr
56- check (example_script )
93+ compare (example_script )
0 commit comments