From 0ce571411b6b35bc62d4f333d1a961bd2f202784 Mon Sep 17 00:00:00 2001 From: wpbonelli Date: Tue, 12 Sep 2023 10:46:51 -0400 Subject: [PATCH] feat(fixtures): add --tabular pytest CLI arg and corresponding fixture (#116) --- modflow_devtools/fixtures.py | 19 +++++++++++++++- modflow_devtools/test/test_fixtures.py | 31 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/modflow_devtools/fixtures.py b/modflow_devtools/fixtures.py index 1fa695c..0700cf8 100644 --- a/modflow_devtools/fixtures.py +++ b/modflow_devtools/fixtures.py @@ -101,6 +101,14 @@ def use_pandas(request): raise ValueError(f"Unsupported value for --pandas: {pandas}") +@pytest.fixture +def tabular(request): + tab = request.config.option.TABULAR + if tab not in ["raw", "recarray", "dataframe"]: + raise ValueError(f"Unsupported value for --tabular: {tab}") + return tab + + # configuration hooks @@ -163,7 +171,16 @@ def pytest_addoption(parser): action="store", default="yes", dest="PANDAS", - help="Package input data can be provided as either pandas dataframes or numpy recarrays. By default, pandas dataframes are used. To test with numpy recarrays, use 'no'. To randomize selection (per test), use 'random'.", + help="Indicates whether to use pandas, where multiple approaches are available. Select 'yes', 'no', or 'random'.", + ) + + parser.addoption( + "-T", + "--tabular", + action="store", + default="raw", + dest="TABULAR", + help="Configure tabular data representation for model input. Select 'raw', 'recarray', or 'dataframe'.", ) diff --git a/modflow_devtools/test/test_fixtures.py b/modflow_devtools/test/test_fixtures.py index c5364e6..32d0f46 100644 --- a/modflow_devtools/test/test_fixtures.py +++ b/modflow_devtools/test/test_fixtures.py @@ -316,3 +316,34 @@ def test_pandas(pandas, arg, function_tmpdir): assert "True" in res elif pandas == "no": assert "False" in res + + +test_tabular_fname = "tabular.txt" + + +@pytest.mark.meta("test_tabular") +def test_tabular_inner(function_tmpdir, tabular): + with open(function_tmpdir / test_tabular_fname, "w") as f: + f.write(str(tabular)) + + +@pytest.mark.parametrize("tabular", ["raw", "recarray", "dataframe"]) +@pytest.mark.parametrize("arg", ["--tabular", "-T"]) +def test_tabular(tabular, arg, function_tmpdir): + inner_fn = test_tabular_inner.__name__ + args = [ + __file__, + "-v", + "-s", + "-k", + inner_fn, + arg, + tabular, + "--keep", + function_tmpdir, + "-M", + "test_tabular", + ] + assert pytest.main(args) == ExitCode.OK + res = open(next(function_tmpdir.rglob(test_tabular_fname))).readlines()[0] + assert tabular == res