Commit eef360a8 authored by Lukas Riedel's avatar Lukas Riedel

Update unit tests for new VTKReader

Add __init__ to pytest directory to enable package-local includes.
parent ff81d71f
import pytest
import numpy as np
from dorie.utilities.vtktools.vtkreader import VTKReader
from ..utilities.vtktools.vtkreader import VTKReader, VTKDataArray
VTK_CELL = "vtkreader-cell.vtu"
VTK_VERTEX = "vtkreader-vertex.vtu"
......@@ -12,65 +12,78 @@ ARRAYS = ["head", "flux", "K_0", "Theta", "theta_w"]
@pytest.fixture
def vtk_cell ():
"""Create a VTKReader for the cell VTK test file"""
return VTKReader(VTK_CELL, DEFAULT_ARRAY)
return VTKReader(VTK_CELL)
@pytest.fixture
def vtk_vertex ():
"""Create a VTKReader for the vertex VTK test file"""
return VTKReader(VTK_VERTEX, DEFAULT_ARRAY)
return VTKReader(VTK_VERTEX)
# Tests ------------------------------------------------------------------
def test_init ():
"""Test if initialization works"""
VTKReader(VTK_CELL, DEFAULT_ARRAY)
VTKReader(VTK_VERTEX, DEFAULT_ARRAY)
reader = VTKReader(VTK_CELL)
isinstance(reader[DEFAULT_ARRAY], VTKDataArray)
reader = VTKReader(VTK_VERTEX)
isinstance(reader.get(DEFAULT_ARRAY), VTKDataArray)
def test_array_set (vtk_cell, vtk_vertex):
def test_dict_properties (vtk_cell, vtk_vertex):
"""Test if all included arrays are found"""
assert len(vtk_cell) == len(ARRAYS)
for array in ARRAYS:
vtk_cell.set_data_array(array)
vtk_vertex.set_data_array(array)
assert array in vtk_cell
assert array in vtk_vertex.keys()
for key in vtk_cell:
assert key in ARRAYS
for key, array in vtk_cell.items():
assert key in ARRAYS
with pytest.raises(RuntimeError):
vtk_cell.set_data_array("other")
assert not "other" in vtk_cell
with pytest.raises(KeyError):
array = vtk_vertex["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
def test_array_properties (vtk_cell):
"""Test the VTKArray properties"""
array = vtk_cell[DEFAULT_ARRAY]
assert array.number_of_components == 1
assert np.allclose(array.bounds, [[0, 1],
[0, 1],
[0, 0]])
vtk_cell.set_data_array("flux")
assert vtk_cell.number_of_components == 3
assert vtk_cell["flux"].number_of_components == 3
def test_evaluate (vtk_cell, vtk_vertex):
"""Test the evaluation function"""
center = np.mean(vtk_cell.bounds, axis=1)
array_cell = vtk_cell[DEFAULT_ARRAY]
array_vertex = vtk_vertex[DEFAULT_ARRAY]
center = np.mean(array_cell.bounds, axis=1)
# should return scalar
with pytest.raises(TypeError):
len(vtk_cell.evaluate(center))
len(array_cell.evaluate(center))
# check return of vector
vtk_vertex.set_data_array("flux")
assert len(vtk_vertex.evaluate(center)) == 3
array_vertex = vtk_vertex["flux"]
assert len(array_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))
array_cell = vtk_cell[array]
array_vertex = vtk_vertex[array]
assert np.allclose(array_cell.evaluate(center),
array_vertex.evaluate(center))
# check evaluation errors
# position has wrong shape
with pytest.raises(ValueError):
vtk_vertex.evaluate([0.0, 0.0])
array_vertex.evaluate([0.0, 0.0])
# position out of bounds
pos = vtk_vertex.bounds[:, 1]
pos = array_vertex.bounds[:, 1]
pos = pos + [1.0, 0.0, 1.0]
with pytest.raises(ValueError):
vtk_vertex.evaluate(pos)
array_vertex.evaluate(pos)
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