Skip to content

Commit

Permalink
Test with modflow6 release and nightly, add test.common module
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtoews committed Feb 20, 2024
1 parent 5d7c290 commit 9424342
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 127 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
matrix:
python-version: ["3.10", "3.11", "3.12"]
os: [ubuntu-latest, macos-latest, windows-latest]
modflow6-repo: [modflow6, modflow6-nightly-build]

steps:
- uses: actions/checkout@v4
Expand All @@ -26,14 +27,16 @@ jobs:
python-version: "${{ matrix.python-version }}"
- name: Setup Fortran
uses: fortran-lang/setup-fortran@v1
- name: Install MODFLOW 6
uses: modflowpy/install-modflow-action@v1
with:
repo: ${{ matrix.modflow6-repo }}
- name: Install test dependencies
run: |
pip install -e ".[tests]"
- name: Test with pytest
# The flag -s stops pytest from capturing output
# This is necessary until proper error reporting is implemented by Modflow
- name: Run tests with MODFLOW 6 release
run: |
pytest -vs --cov=xmipy --cov-report=xml tests/
pytest -v --cov=xmipy --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ docs = ["pdoc"]
Documentation = "https://deltares.github.io/xmipy/xmipy.html"
Source = "https://github.com/Deltares/xmipy"

[tool.hatch.build.targets.wheel]
packages = ["xmipy"]

[tool.hatch.version]
path = "xmipy/__init__.py"

Expand All @@ -50,6 +53,9 @@ disallow_untyped_defs = true
no_implicit_reexport = true
warn_return_any = true

[tool.pytest.ini_options]
testpaths = ["tests"]

[tool.ruff]
select = [
"ARG",
Expand Down
Empty file added tests/__init__.py
Empty file.
45 changes: 45 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Common functions for tests. For pytest fixtures, see conftest.py."""

import os
import platform
import shutil
from pathlib import Path


def get_libmf6() -> Path:
"""Get libmf6 based on environment variable or PATH with mf6.
To specify directly, set the environment variable `LIBMF6` to the path of
the library to test.
Otherwise, check for the environment variable `MODFLOW_BIN_PATH` as used by
modflowpy/install-modflow-action to find the mf6 executable.
Lastly, find the mf6 executable from the PATH environment, and
try to find the library in the same directory as the executable.
"""
if "LIBMF6" in os.environ:
lib_path = Path(os.environ["LIBMF6"])
else:
if "MODFLOW_BIN_PATH" in os.environ:
mf6 = shutil.which("mf6", path=os.environ["MODFLOW_BIN_PATH"])
else:
mf6 = shutil.which("mf6")
if mf6 is None:
raise FileNotFoundError("cannot find mf6 on PATH")
mf6 = Path(mf6)
sysinfo = platform.system()
if sysinfo == "Windows":
lib_path = mf6.parent / "libmf6.dll"
elif sysinfo == "Linux":
lib_path = mf6.parent / "libmf6.so"
elif sysinfo == "Darwin":
lib_path = mf6.parent / "libmf6.dylib"
else:
raise NotImplementedError(f"system not supported: {sysinfo}")
if lib_path.exists():
return lib_path
raise FileNotFoundError(f"{lib_path} not found")


libmf6_path = get_libmf6()
152 changes: 29 additions & 123 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import platform
"""Fixtures for pytest. Other common functions are in common.py."""

from dataclasses import dataclass
from typing import Any, List, Tuple

Expand All @@ -9,22 +10,20 @@

from xmipy import XmiWrapper

from .common import libmf6_path

@pytest.fixture(scope="session")
def modflow_lib_path(tmp_path_factory):
tmp_path = tmp_path_factory.getbasetemp()
sysinfo = platform.system()
if sysinfo == "Windows":
lib_path = tmp_path / "libmf6.dll"
elif sysinfo == "Linux":
lib_path = tmp_path / "libmf6.so"
elif sysinfo == "Darwin":
lib_path = tmp_path / "libmf6.dylib"
else:
raise RuntimeError(f"system not supported: {sysinfo}")

flopy.utils.get_modflow(bindir=str(tmp_path), repo="modflow6-nightly-build")
return str(lib_path)
def pytest_report_header(config, start_path, startdir):
"""Show report header at top of pytest output.
https://docs.pytest.org/reference/reference.html#pytest.hookspec.pytest_report_header
"""
return f"libmf6: {libmf6_path}"


@pytest.fixture(scope="session")
def modflow_lib_path():
return libmf6_path


@dataclass
Expand All @@ -40,7 +39,7 @@ class FlopyDis:


@pytest.fixture(scope="function")
def flopy_dis(tmp_path):
def flopy_dis(tmp_path, modflow_lib_path):
sim_path = str(tmp_path)
sim = flopy.mf6.MFSimulation(
sim_name="TEST_SIM_DIS",
Expand Down Expand Up @@ -97,7 +96,7 @@ def flopy_dis_mf6(flopy_dis, modflow_lib_path, request):


@pytest.fixture(scope="function")
def flopy_dis_idomain(tmp_path):
def flopy_dis_idomain(tmp_path, modflow_lib_path):
sim_path = str(tmp_path)
sim = flopy.mf6.MFSimulation(
sim_name="TEST_SIM_DIS", version="mf6", sim_ws=sim_path
Expand Down Expand Up @@ -167,6 +166,15 @@ def flopy_disu(tmp_path):
flopy.mf6.ModflowTdis(sim, time_units="DAYS", nper=2, perioddata=flopy_disu.tdis_rc)
flopy.mf6.ModflowIms(sim)
gwf = flopy.mf6.ModflowGwf(sim, modelname=flopy_disu.model_name, save_flows=True)
# fmt: off
ja = [0, 1, 3, 1, 0, 2, 4, 2, 1, 5, 3, 0, 4, 6, 4, 1, 3,
5, 7, 5, 2, 4, 8, 6, 3, 7, 7, 4, 6, 8, 8, 5, 7]
# fmt: on
zero_locs = [0, 3, 7, 10, 14, 19, 23, 26, 30]
hwva = np.full(33, 1.0)
hwva[zero_locs] = 0.0
cl12 = np.full(33, 0.5)
cl12[zero_locs] = 0.0
flopy.mf6.ModflowGwfdisu(
gwf,
nodes=9,
Expand All @@ -176,112 +184,10 @@ def flopy_disu(tmp_path):
bot=[-2.0],
area=np.full(9, 1.0),
iac=[3, 4, 3, 4, 5, 4, 3, 4, 3],
ja=[
0,
1,
3,
1,
0,
2,
4,
2,
1,
5,
3,
0,
4,
6,
4,
1,
3,
5,
7,
5,
2,
4,
8,
6,
3,
7,
7,
4,
6,
8,
8,
5,
7,
],
ja=ja,
ihc=[1],
cl12=[
0,
0.5,
0.5,
0,
0.5,
0.5,
0.5,
0,
0.5,
0.5,
0,
0.5,
0.5,
0.5,
0,
0.5,
0.5,
0.5,
0.5,
0,
0.5,
0.5,
0.5,
0,
0.5,
0.5,
0,
0.5,
0.5,
0.5,
0,
0.5,
0.5,
],
hwva=[
0,
1.0,
1.0,
0,
1.0,
1.0,
1.0,
0,
1.0,
1.0,
0,
1.0,
1.0,
1.0,
0,
1.0,
1.0,
1.0,
1.0,
0,
1.0,
1.0,
1.0,
0,
1.0,
1.0,
0,
1.0,
1.0,
1.0,
0,
1.0,
1.0,
],
cl12=cl12,
hwva=hwva,
vertices=[
[0, 0.0, 0.0],
[1, 0.0, 1.0],
Expand Down Expand Up @@ -326,7 +232,7 @@ class FlopyGwfSto:


@pytest.fixture(scope="function")
def flopy_gwf_sto(tmp_path):
def flopy_gwf_sto(tmp_path, modflow_lib_path):
sim_path = str(tmp_path)
sim = flopy.mf6.MFSimulation(
sim_name="TEST_SIM_DIS", version="mf6", sim_ws=sim_path
Expand Down

0 comments on commit 9424342

Please sign in to comment.