Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests, adding a common module #138

Merged
merged 7 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ jobs:
uses: prefix-dev/[email protected]
with:
manifest-path: pyproject.toml
- name: Install MODFLOW 6
uses: modflowpy/install-modflow-action@v1
with:
repo: modflow6-nightly-build
- name: Test with pytest
run: pixi run tests-ci
env:
Expand Down
510 changes: 260 additions & 250 deletions pixi.lock

Large diffs are not rendered by default.

13 changes: 10 additions & 3 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 @@ -56,8 +59,8 @@ twine = "*"
# Tests
# The flag -s stops pytest from capturing output
# This is necessary until proper error reporting is implemented by Modflow
tests-ci = "pytest -vs --cov=xmipy --cov-report=xml tests/"
tests = "pytest tests/"
tests-ci = "pytest --cov=xmipy --cov-report=xml"
tests = "pytest"

# Docs
docs = "pdoc -o docs/ xmipy"
Expand Down Expand Up @@ -90,6 +93,10 @@ py311 = ["py311"]
py310 = ["py310"]
py39 = ["py39"]

[tool.pytest.ini_options]
addopts = "-vs"
testpaths = ["tests"]

[tool.mypy]
exclude = ["tests"]
warn_unused_configs = true
Expand All @@ -106,7 +113,7 @@ disallow_untyped_defs = true
no_implicit_reexport = true
warn_return_any = true

[tool.ruff]
[tool.ruff.lint]
select = [
"ARG",
"B",
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 := os.environ.get("MODFLOW_BIN_PATH"):
mf6 = shutil.which("mf6", path=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()
145 changes: 25 additions & 120 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,19 @@

from xmipy import XmiWrapper

from .common import libmf6_path


def pytest_report_header(**kwargs): # noqa: ARG001
"""Show report header at top of pytest output.
https://docs.pytest.org/en/stable/reference/reference.html#pytest.hookspec.pytest_report_header
"""
return f"libmf6: {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)
@pytest.fixture(scope="session")
def modflow_lib_path():
return libmf6_path


@dataclass
Expand Down Expand Up @@ -167,6 +165,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 +183,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
2 changes: 1 addition & 1 deletion tests/test_mf6_dis_bmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def test_get_value_int_scalar(flopy_dis_idomain_mf6):
mf6.initialize()

# get scalar variable:
id_tag = next(var for var in mf6.get_output_var_names() if var.endswith("/ID"))
id_tag = next(var for var in mf6.get_output_var_names() if var.endswith("/NROW"))
assert mf6.get_var_rank(id_tag) == 0

# compare with value in MODFLOW memory:
Expand Down
Loading