aiming at testing each individual statement

This commit is contained in:
Martin Diehl 2020-08-25 07:49:56 +02:00
parent 2d98325fa4
commit 51da632108
2 changed files with 53 additions and 15 deletions

View File

@ -131,17 +131,19 @@ class VTK:
"""
ext = Path(fname).suffix
if ext == '.vtk' or dataset_type:
if ext == '.vtk' or dataset_type is not None:
reader = vtk.vtkGenericDataObjectReader()
reader.SetFileName(str(fname))
reader.Update()
if dataset_type is None:
raise TypeError('Dataset type for *.vtk file not given.')
elif dataset_type.lower().endswith('rectilineargrid'):
reader.Update()
geom = reader.GetRectilinearGridOutput()
elif dataset_type.lower().endswith('unstructuredgrid'):
reader.Update()
geom = reader.GetUnstructuredGridOutput()
elif dataset_type.lower().endswith('polydata'):
reader.Update()
geom = reader.GetPolyDataOutput()
else:
raise TypeError(f'Unknown dataset type {dataset_type} for vtk file')
@ -259,15 +261,15 @@ class VTK:
Data label.
"""
celldata = self.geom.GetCellData()
for a in range(celldata.GetNumberOfArrays()):
if celldata.GetArrayName(a) == label:
return vtk_to_np(celldata.GetArray(a))
cell_data = self.geom.GetCellData()
for a in range(cell_data.GetNumberOfArrays()):
if cell_data.GetArrayName(a) == label:
return vtk_to_np(cell_data.GetArray(a))
pointdata = self.geom.GetPointData()
for a in range(celldata.GetNumberOfArrays()):
if pointdata.GetArrayName(a) == label:
return vtk_to_np(pointdata.GetArray(a))
point_data = self.geom.GetPointData()
for a in range(point_data.GetNumberOfArrays()):
if point_data.GetArrayName(a) == label:
return vtk_to_np(point_data.GetArray(a))
raise ValueError(f'array "{label}" not found')

View File

@ -13,8 +13,19 @@ def reference_dir(reference_dir_base):
"""Directory containing reference results."""
return reference_dir_base/'VTK'
@pytest.fixture
def default():
"""Simple VTK."""
grid = np.array([5,6,7],int)
size = np.array([.6,1.,.5])
return VTK.from_rectilinearGrid(grid,size)
class TestVTK:
@pytest.fixture(autouse=True)
def _execution_stamp(self, execution_stamp):
print('patched damask.util.execution_stamp')
def test_rectilinearGrid(self,tmp_path):
grid = np.random.randint(5,10,3)*2
size = np.random.random(3) + 1.0
@ -77,9 +88,33 @@ class TestVTK:
@pytest.mark.parametrize('name,dataset_type',[('this_file_does_not_exist.vtk', None),
('this_file_does_not_exist.vtk','vtk'),
('this_file_does_not_exist.vtx', None)])
def test_invalid_dataset_type(self,dataset_type,name):
def test_invalid_dataset_type(self,name,dataset_type):
with pytest.raises(TypeError):
VTK.from_file('this_file_does_not_exist.vtk',dataset_type)
VTK.from_file(name,dataset_type)
def test_invalid_extension_write(self,default):
with pytest.raises(ValueError):
default.write('default.txt')
def test_invalid_get(self,default):
with pytest.raises(ValueError):
default.get('does_not_exist')
@pytest.mark.parametrize('data,label',[(np.ones(3),'valid'),
(np.ones(3),None)])
def test_invalid_add(self,default,data,label):
with pytest.raises(ValueError):
default.add(np.ones(3),label)
def test_invalid_add_type(self,default):
with pytest.raises(TypeError):
default.add('invalid_type','label')
def test_comments(self,tmp_path,default):
default.add_comments(['this is a comment'])
default.write(tmp_path/'with_comments',parallel=False)
new = VTK.from_file(tmp_path/'with_comments.vtr')
assert new.get_comments() == ['this is a comment']
def test_compare_reference_polyData(self,update,reference_dir,tmp_path):
@ -90,7 +125,8 @@ class TestVTK:
polyData.write(reference_dir/'polyData')
else:
reference = VTK.from_file(reference_dir/'polyData.vtp')
assert polyData.__repr__() == reference.__repr__()
assert polyData.__repr__() == reference.__repr__() and \
np.allclose(polyData.get('coordinates'),points)
def test_compare_reference_rectilinearGrid(self,update,reference_dir,tmp_path):
grid = np.array([5,6,7],int)
@ -104,5 +140,5 @@ class TestVTK:
rectilinearGrid.write(reference_dir/'rectilinearGrid')
else:
reference = VTK.from_file(reference_dir/'rectilinearGrid.vtr')
assert rectilinearGrid.__repr__() == reference.__repr__()
assert rectilinearGrid.__repr__() == reference.__repr__() and \
np.allclose(rectilinearGrid.get('cell'),c)