diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..3b4c488 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,9 @@ +[pytest] +markers = + functional: Functional tests for stuff that the user would do + one_dimensional: Tests in 1D + invalid_args: Tests for invalid argument handling + default_args: Tests for default argument handling + grid_spec: Marker to construct grid data by fixtures +addopts = + --strict-markers diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..1f889d0 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,23 @@ +import numpy as np +import pytest + + +@pytest.fixture() +def grid_data(request): + m = request.node.get_closest_marker("grid_spec") + if m is None: + raise ValueError("Grid data marker not defined") + shape = m.kwargs["shape"] + edges = m.kwargs["edges"] + + if len(shape) == 1: + x = np.linspace(*edges, shape[0]) + dx = x[1] - x[0] + return x, dx + + axes = tuple( + [np.linspace(edges[k][0], edges[k][1], shape[k]) for k in range(len(shape))] + ) + coords = np.meshgrid(*axes, indexing="ij") + spacings = [axes[k][1] - axes[k][0] for k in range(len(shape))] + return axes, spacings, coords diff --git a/tests/test_diff.py b/tests/test_diff.py index 40cb64b..329bcf4 100644 --- a/tests/test_diff.py +++ b/tests/test_diff.py @@ -7,19 +7,6 @@ from findiff import Diff, Identity -def test_partial_d_dx(): - shape = (101,) - x, dx = make_grid(shape, (0, 1)) - - u = x**2 - expected = 2 * x - - fd = Diff(0, dx) - actual = fd(u) - - assert_array_almost_equal(expected, actual) - - def test_partial_d_dx_periodic(): x = np.linspace(0, 2 * np.pi, 200, endpoint=False) dx = x[1] - x[0] diff --git a/tests/test_functional.py b/tests/test_functional.py new file mode 100644 index 0000000..e9be248 --- /dev/null +++ b/tests/test_functional.py @@ -0,0 +1,65 @@ +import numpy as np +import pytest +from numpy.testing import assert_array_almost_equal + +from findiff import Diff + +pytestmark = pytest.mark.functional + + +@pytest.mark.one_dimensional +class TestDiff_1D: + + @pytest.mark.grid_spec(shape=(101,), edges=(0, 1)) + def test_diff_1d(self, grid_data): + x, dx = grid_data + + u = x**2 + expected = 2 * x + + fd = Diff(0, dx) + actual = fd(u) + + assert_array_almost_equal(expected, actual) + + @pytest.mark.grid_spec(shape=(101,), edges=(0, 1)) + def test_diff_1d_deferred(self, grid_data): + x, dx = grid_data + + u = x**2 + expected = 2 * x + + fd = Diff(0) + fd.set_grid({0: dx}) + actual = fd(u) + + assert_array_almost_equal(expected, actual) + + def test_diff_1d_deferred_called_too_early(self): + u = np.zeros(10) + fd = Diff(0) + with pytest.raises(TypeError, match="Unknown axis type."): + fd(u) + + @pytest.mark.default_args + @pytest.mark.grid_spec(shape=(101,), edges=(0, 1)) + def test_diff_1d_defaults(self, grid_data): + x, dx = grid_data + + u = x**2 + expected = 2 * x + + fd = Diff() + fd.set_grid({0: dx}) + actual = fd(u) + + assert_array_almost_equal(expected, actual) + + @pytest.mark.invalid_args + def test_diff_1d_invalid_args(self): + + with pytest.raises(ValueError, match="Dimension must be >= 0"): + Diff(-1, 1) + + with pytest.raises(ValueError, match="Spacing must be > 0."): + Diff(0, -1)