|
| 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 |
0 commit comments