From 51da63210876708198495fb8de79a3e53232a619 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Tue, 25 Aug 2020 07:49:56 +0200 Subject: [PATCH] aiming at testing each individual statement --- python/damask/_vtk.py | 22 ++++++++++--------- python/tests/test_VTK.py | 46 +++++++++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/python/damask/_vtk.py b/python/damask/_vtk.py index cccfc5524..60e556f84 100644 --- a/python/damask/_vtk.py +++ b/python/damask/_vtk.py @@ -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') diff --git a/python/tests/test_VTK.py b/python/tests/test_VTK.py index 91ff4033c..35155caa2 100644 --- a/python/tests/test_VTK.py +++ b/python/tests/test_VTK.py @@ -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)