From f8f4d4efc7cce1f56e22b3cdb3171d29578e062f Mon Sep 17 00:00:00 2001 From: Wes Bonelli Date: Tue, 12 Sep 2023 10:13:10 -0400 Subject: [PATCH] feat(fixtures): add --tabular pytest CLI arg and corresponding fixture --- 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 1fa695c8..0700cf86 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 c5364e68..32d0f46e 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