Commit 32367f0a authored by Lukas Riedel's avatar Lukas Riedel

Add target for Python tests and test VTKReader

* Add 'pytest' to dependencies for Python module.
* Add test for VTKReader.
  - Add testing .vtu files.
* Define Python test target.
parent 93869a67
......@@ -8,3 +8,12 @@ dune_python_install_package(PATH dorie)
# set cache variable
set(DORIE_PYTHON_MODULES ${python_paths} CACHE PATH "Paths to the python modules")
# add Python tests
dune_python_add_test(NAME python_tests
COMMAND ${CMAKE_BINARY_DIR}/run-in-dune-env
python -m pytest -v
${CMAKE_CURRENT_SOURCE_DIR}/dorie/dorie/test/
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}/dorie/dorie/test/
)
import pytest
import numpy as np
from dorie.utilities.vtktools.vtkreader import VTKReader
VTK_CELL = "vtkreader-cell.vtu"
VTK_VERTEX = "vtkreader-vertex.vtu"
DEFAULT_ARRAY = "head"
ARRAYS = ["head", "flux", "K_0", "Theta", "theta_w"]
# Fixtures ---------------------------------------------------------------
@pytest.fixture
def vtk_cell ():
"""Create a VTKReader for the cell VTK test file"""
return VTKReader(VTK_CELL, DEFAULT_ARRAY)
@pytest.fixture
def vtk_vertex ():
"""Create a VTKReader for the vertex VTK test file"""
return VTKReader(VTK_VERTEX, DEFAULT_ARRAY)
# Tests ------------------------------------------------------------------
def test_init ():
"""Test if initialization works"""
VTKReader(VTK_CELL, DEFAULT_ARRAY)
VTKReader(VTK_VERTEX, DEFAULT_ARRAY)
def test_array_set (vtk_cell, vtk_vertex):
"""Test if all included arrays are found"""
for array in ARRAYS:
vtk_cell.set_data_array(array)
vtk_vertex.set_data_array(array)
with pytest.raises(RuntimeError):
vtk_cell.set_data_array("other")
def test_properties (vtk_cell):
"""Test the VTKReader properties"""
assert np.allclose(vtk_cell.bounds, [[0, 1],
[0, 1],
[0, 0]])
assert vtk_cell.number_of_components == 1
vtk_cell.set_data_array("flux")
assert vtk_cell.number_of_components == 3
def test_evaluate (vtk_cell, vtk_vertex):
"""Test the evaluation function"""
center = np.mean(vtk_cell.bounds, axis=1)
# should return scalar
with pytest.raises(TypeError):
len(vtk_cell.evaluate(center))
# check return of vector
vtk_vertex.set_data_array("flux")
assert len(vtk_vertex.evaluate(center)) == 3
# check values are equal
for array in ["head", "K_0", "flux"]:
vtk_cell.set_data_array(array)
vtk_vertex.set_data_array(array)
assert np.allclose(vtk_cell.evaluate(center),
vtk_vertex.evaluate(center))
# check evaluation errors
# position has wrong shape
with pytest.raises(ValueError):
vtk_vertex.evaluate([0.0, 0.0])
# position out of bounds
pos = vtk_vertex.bounds[:, 1]
pos = pos + [1.0, 0.0, 1.0]
with pytest.raises(ValueError):
vtk_vertex.evaluate(pos)
<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">
<UnstructuredGrid>
<Piece NumberOfCells="1" NumberOfPoints="4">
<CellData Scalars="head" Vectors="flux">
<DataArray type="Float32" Name="head" NumberOfComponents="1" format="binary">
BAAAAA==AAAAvw==
</DataArray>
<DataArray type="Float32" Name="flux" NumberOfComponents="3" format="binary">
DAAAAA==AAAAgAAAAIAAAAAA
</DataArray>
<DataArray type="Float32" Name="K_0" NumberOfComponents="1" format="binary">
BAAAAA==pIy4N4==
</DataArray>
<DataArray type="Float32" Name="theta_w" NumberOfComponents="1" format="binary">
BAAAAA==Jh4iPh==
</DataArray>
<DataArray type="Float32" Name="Theta" NumberOfComponents="1" format="binary">
BAAAAA==jKPqPq==
</DataArray>
</CellData>
<Points>
<DataArray type="Float32" Name="Coordinates" NumberOfComponents="3" format="binary">
MAAAAA==AAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAgD8AAAAAAACAPwAAgD8AAAAA
</DataArray>
</Points>
<Cells>
<DataArray type="Int32" Name="connectivity" NumberOfComponents="1" format="binary">
EAAAAA==AAAAAAEAAAADAAAAAgAAAA==
</DataArray>
<DataArray type="Int32" Name="offsets" NumberOfComponents="1" format="binary">
BAAAAA==BAAAAA==
</DataArray>
<DataArray type="UInt8" Name="types" NumberOfComponents="1" format="binary">
AQAAAA==CQ==
</DataArray>
</Cells>
</Piece>
</UnstructuredGrid>
</VTKFile>
<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">
<UnstructuredGrid>
<Piece NumberOfCells="1" NumberOfPoints="4">
<PointData Scalars="head" Vectors="flux">
<DataArray type="Float32" Name="head" NumberOfComponents="1" format="binary">
EAAAAA==AAAAAAAAAAAAAIC/AACAvw==
</DataArray>
<DataArray type="Float32" Name="flux" NumberOfComponents="3" format="binary">
MAAAAA==AAAAgAAAAIAAAAAAAAAAgAAAAIAAAAAAAAAAgAAAAIAAAAAAAAAAgAAAAIAAAAAA
</DataArray>
<DataArray type="Float32" Name="K_0" NumberOfComponents="1" format="binary">
EAAAAA==pIy4N6SMuDekjLg3pIy4N4==
</DataArray>
<DataArray type="Float32" Name="theta_w" NumberOfComponents="1" format="binary">
EAAAAA==UriePlK4nj6N0Uo9jdFKPd==
</DataArray>
<DataArray type="Float32" Name="Theta" NumberOfComponents="1" format="binary">
EAAAAA==AACAPwAAgD9Xv449V7+OPb==
</DataArray>
</PointData>
<Points>
<DataArray type="Float32" Name="Coordinates" NumberOfComponents="3" format="binary">
MAAAAA==AAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAgD8AAAAAAACAPwAAgD8AAAAA
</DataArray>
</Points>
<Cells>
<DataArray type="Int32" Name="connectivity" NumberOfComponents="1" format="binary">
EAAAAA==AAAAAAEAAAADAAAAAgAAAA==
</DataArray>
<DataArray type="Int32" Name="offsets" NumberOfComponents="1" format="binary">
BAAAAA==BAAAAA==
</DataArray>
<DataArray type="UInt8" Name="types" NumberOfComponents="1" format="binary">
AQAAAA==CQ==
</DataArray>
</Cells>
</Piece>
</UnstructuredGrid>
</VTKFile>
......@@ -9,7 +9,8 @@ setup(name='dorie',
author='Lukas Riedel',
author_email='dorieteam@iup.uni-heidelberg.de',
url='https://ts-gitlab.iup.uni-heidelberg.de/dorie/dorie',
packages=find_packages(),
packages=find_packages(exclude=["dorie.test"]),
test_suite='py.test',
install_requires= ['cycler',
'configparser',
'h5py',
......@@ -18,6 +19,7 @@ setup(name='dorie',
'Pillow',
'pygmsh',
'pyparsing',
'pytest',
'python-dateutil',
'pytz',
'pyyaml',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment