From 87f9167ce37dc09198c7c71e6ed5dc880726859a Mon Sep 17 00:00:00 2001 From: Gertjan van Zwieten Date: Fri, 9 Jun 2023 11:54:35 +0200 Subject: [PATCH] add unit tests This patch installs matplotlib in Github coverage, and adds unit tests for all three supported spatial dimensions of export.triplot. --- .github/workflows/test.yaml | 2 +- tests/test_export.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index ed1388de7..aa2805a34 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -93,7 +93,7 @@ jobs: python -um pip install --upgrade --upgrade-strategy eager wheel python -um pip install --upgrade --upgrade-strategy eager coverage numpy$_numpy_version # Install Nutils from `dist` dir created in job `build-python-package`. - python -um pip install "$_wheel[import_gmsh]" + python -um pip install "$_wheel[import_gmsh,export_mpl]" - name: Install Scipy if: ${{ matrix.matrix-backend == 'scipy' }} run: python -um pip install --upgrade scipy diff --git a/tests/test_export.py b/tests/test_export.py index 981715d39..d95324812 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -27,6 +27,43 @@ def test_autodetect_imagetype(self): test(f.read()) +@testing.parametrize +class triplot(testing.TestCase): + + def setUp(self): + super().setUp() + self.outdir = pathlib.Path(self.enter_context(tempfile.TemporaryDirectory())) + self.enter_context(treelog.set(treelog.DataLog(str(self.outdir)))) + self.coords = numpy.zeros([self.ndims + 1, self.ndims]) + self.coords[1:] = numpy.eye(self.ndims) + self.tri = numpy.arange(self.ndims + 1)[numpy.newaxis] + self.hull = numpy.array([self.tri[0,~m] for m in numpy.eye(self.ndims+1, dtype=bool)]) + if self.ndims == 3: + self.tri = self.hull + self.hull = numpy.array([[i,j] for i in range(4) for j in range(i)]) + self.values = numpy.arange(self.ndims+1, dtype=float) * self.ndims + + @testing.requires('matplotlib', 'PIL') + def test_filename(self): + export.triplot('test.jpg', self.coords, self.values, tri=self.tri, hull=self.hull) + + @testing.requires('matplotlib', 'PIL') + def test_axesobj(self): + with export.mplfigure('test.jpg') as fig: + ax = fig.add_subplot(111, projection='3d' if self.ndims == 3 else None) + im = export.triplot(ax, self.coords, self.values, tri=self.tri, hull=self.hull) + if self.ndims == 1: + self.assertEqual(im, None) + elif self.ndims == 2: + self.assertAllEqual(im.get_array(), self.values) + elif self.ndims == 3: + self.assertAllEqual(im.get_array(), self.values[self.tri].mean(1)) + +triplot(ndims=1) +triplot(ndims=2) +triplot(ndims=3) + + @testing.parametrize class vtk(testing.TestCase):