Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
This patch installs matplotlib in Github coverage, and adds unit tests for all
three supported spatial dimensions of export.triplot.
  • Loading branch information
gertjanvanzwieten committed Jun 9, 2023
1 parent 971fb53 commit 87f9167
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down

0 comments on commit 87f9167

Please sign in to comment.