Skip to content

Commit

Permalink
feat(snapshots): add snapshot-disable cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed May 21, 2024
1 parent 87c8fe6 commit f1ede2e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion autotest/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pathlib import Path

pytest_plugins = ["modflow_devtools.fixtures"]
pytest_plugins = ["modflow_devtools.fixtures", "modflow_devtools.snapshots"]
project_root_path = Path(__file__).parent
25 changes: 24 additions & 1 deletion autotest/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from pathlib import Path

import numpy as np
import pytest
from _pytest.config import ExitCode

proj_root = Path(__file__).parents[1]
module_path = Path(inspect.getmodulename(__file__))
pytest_plugins = ["modflow_devtools.snapshots"] # activate snapshot fixtures
snapshot_array = np.array([1.1, 2.2, 3.3])
snapshots_path = proj_root / "autotest" / "__snapshots__"

Expand Down Expand Up @@ -61,3 +62,25 @@ def test_readable_text_array_snapshot(readable_array_snapshot):
),
snapshot_array,
)


@pytest.mark.meta("test_snapshot_disable")
def test_snapshot_disable_inner(array_snapshot):
assert array_snapshot == "match this!"


@pytest.mark.parametrize("disable", [True, False])
def test_snapshot_disable(disable):
inner_fn = test_snapshot_disable_inner.__name__
args = [
__file__,
"-v",
"-s",
"-k",
inner_fn,
"-M",
"test_snapshot_disable",
]
if disable:
args.append("--snapshot-disable")
assert pytest.main(args) == (ExitCode.OK if disable else ExitCode.TESTS_FAILED)
46 changes: 40 additions & 6 deletions modflow_devtools/snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,53 @@ def serialize(
return np.array2string(data, threshold=np.inf)


class MatchAnything:
def __eq__(self, _):
return True


# fixtures


@pytest.fixture(scope="session")
def snapshot_disable(pytestconfig) -> bool:
return pytestconfig.getoption("--snapshot-disable")


@pytest.fixture
def array_snapshot(snapshot):
return snapshot.use_extension(BinaryArrayExtension)
def array_snapshot(snapshot, snapshot_disable):
return (
MatchAnything()
if snapshot_disable
else snapshot.use_extension(BinaryArrayExtension)
)


@pytest.fixture
def text_array_snapshot(snapshot):
return snapshot.use_extension(TextArrayExtension)
def text_array_snapshot(snapshot, snapshot_disable):
return (
MatchAnything()
if snapshot_disable
else snapshot.use_extension(TextArrayExtension)
)


@pytest.fixture
def readable_array_snapshot(snapshot):
return snapshot.use_extension(ReadableArrayExtension)
def readable_array_snapshot(snapshot, snapshot_disable):
return (
MatchAnything()
if snapshot_disable
else snapshot.use_extension(ReadableArrayExtension)
)


# pytest config hooks


def pytest_addoption(parser):
parser.addoption(
"--snapshot-disable",
action="store_true",
default=False,
help="Disable snapshot comparisons.",
)

0 comments on commit f1ede2e

Please sign in to comment.