Skip to content

Commit 1d787a4

Browse files
authored
feat: models api (#191)
1 parent e64ce4c commit 1d787a4

8 files changed

Lines changed: 12663 additions & 36 deletions

File tree

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,3 @@ autotest/temp/
149149

150150
# uv lockfile
151151
uv.lock
152-
153-
modflow_devtools/data

autotest/test_models.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import random
2+
import sys
3+
from pathlib import Path
4+
5+
import pytest
6+
import tomli
7+
8+
import modflow_devtools.models as models
9+
10+
MODELS_TOML_PATH = Path(models.DATA_PATH) / models.MODELMAP_NAME
11+
12+
13+
# TODO: Remove when we drop support for python 3.9 (soon?)
14+
if sys.version_info[:2] == (3, 9):
15+
pytest.skip("Unsupported for python 3.9", allow_module_level=True)
16+
17+
18+
@pytest.fixture
19+
def models_toml():
20+
with MODELS_TOML_PATH.open("rb") as f:
21+
return tomli.load(f)
22+
23+
24+
@pytest.fixture
25+
def temp_cache_dir(tmpdir, monkeypatch):
26+
temp_dir = tmpdir.mkdir("pooch_cache")
27+
monkeypatch.setenv("MF_DATA_DIR", str(temp_dir))
28+
models.FETCHER.path = temp_dir # Update the fetcher path
29+
return temp_dir
30+
31+
32+
def test_registry_loaded():
33+
assert models.FETCHER.registry is not None, "Registry was not loaded"
34+
assert len(models.FETCHER.registry) > 0, "Registry is empty"
35+
36+
37+
def test_generated_functions_exist(models_toml):
38+
for model_name in models_toml.keys():
39+
assert hasattr(models, model_name), (
40+
f"Function {model_name} not found in models module"
41+
)
42+
43+
44+
def test_generated_functions_return_files(models_toml, temp_cache_dir):
45+
for model_name, files in models_toml.items():
46+
model_function = getattr(models, model_name)
47+
fetched_files = model_function()
48+
cached_files = temp_cache_dir.listdir()
49+
assert isinstance(fetched_files, list), (
50+
f"Function {model_name} did not return a list"
51+
)
52+
assert len(fetched_files) == len(files), (
53+
f"Function {model_name} did not return the correct number of files"
54+
)
55+
for fetched_file in fetched_files:
56+
assert Path(fetched_file).exists(), (
57+
f"Fetched file {fetched_file} does not exist"
58+
)
59+
assert Path(temp_cache_dir) / Path(fetched_file).name in cached_files, (
60+
f"Fetched file {fetched_file} is not in the temp cache directory"
61+
)
62+
if random.randint(0, 5) % 5 == 0:
63+
break # just the first few so we dont ddos github

docs/md/models.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ For example, to create a registry of models in the MF6 test models repositories,
2626

2727
```shell
2828
python -m modflow_devtools.make_registry -p modflow6-testmodels -b https://github.com/MODFLOW-ORG/modflow6-testmodels/raw/master
29-
python -m modflow_devtools.make_registry -p modflow6-largetestmodels -a -b https://github.com/MODFLOW-ORG/modflow6-largetestmodels/raw/master
29+
python -m modflow_devtools.make_registry -a -p modflow6-largetestmodels -b https://github.com/MODFLOW-ORG/modflow6-largetestmodels/raw/master
3030
```

0 commit comments

Comments
 (0)