Skip to content

Commit

Permalink
Start test suite restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
maroba committed Dec 21, 2024
1 parent 73903eb commit 53f3134
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 13 deletions.
9 changes: 9 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 0 additions & 13 deletions tests/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
65 changes: 65 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 53f3134

Please sign in to comment.