# Copyright (C) Stichting Deltares 2021. All rights reserved. # # This file is part of the Dam Python Interface. # # The Dam Python Interface is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # # All names, logos, and references to "Deltares" are registered trademarks of # Stichting Deltares and remain full property of Stichting Deltares at all times. # All rights reserved. from typing import List from dampythoninterface.surface_line import Point, PointTypeEnum, SurfaceLine from .utils import TestUtils import pytest from pathlib import Path from lxml import etree class TestSurfaceLine: @pytest.mark.unittest def test_surface_line_error_raised(self): # set up test data list_points = [ Point(X=i, Z=2 * i, PointType=PointTypeEnum.NONE) for i in range(10) ] # run test expected_mssg = "Set Characteristic points Test surface line is not complete." with pytest.raises(ValueError, match=expected_mssg): SurfaceLine(Points=list_points, Name="Test surface line") @pytest.mark.unittest @pytest.mark.parametrize( "points_available, error_message", [ ( [ PointTypeEnum.DikeToeAtRiver, PointTypeEnum.DikeTopAtRiver, PointTypeEnum.DikeTopAtPolder, PointTypeEnum.DikeToeAtPolder, PointTypeEnum.SurfaceLevelInside, ], "SurfaceLevelOutside", ), ( [ PointTypeEnum.SurfaceLevelOutside, PointTypeEnum.DikeTopAtRiver, PointTypeEnum.DikeTopAtPolder, PointTypeEnum.DikeToeAtPolder, PointTypeEnum.SurfaceLevelInside, ], "DikeToeAtRiver", ), ( [ PointTypeEnum.SurfaceLevelOutside, PointTypeEnum.DikeToeAtRiver, PointTypeEnum.DikeTopAtPolder, PointTypeEnum.DikeToeAtPolder, PointTypeEnum.SurfaceLevelInside, ], "DikeTopAtRiver", ), ( [ PointTypeEnum.SurfaceLevelOutside, PointTypeEnum.DikeToeAtRiver, PointTypeEnum.DikeTopAtRiver, PointTypeEnum.DikeToeAtPolder, PointTypeEnum.SurfaceLevelInside, ], "DikeTopAtPolder", ), ( [ PointTypeEnum.SurfaceLevelOutside, PointTypeEnum.DikeToeAtRiver, PointTypeEnum.DikeTopAtRiver, PointTypeEnum.DikeTopAtPolder, PointTypeEnum.SurfaceLevelInside, ], "DikeToeAtPolder", ), ( [ PointTypeEnum.SurfaceLevelOutside, PointTypeEnum.DikeToeAtRiver, PointTypeEnum.DikeTopAtRiver, PointTypeEnum.DikeTopAtPolder, PointTypeEnum.DikeToeAtPolder, ], "SurfaceLevelInside", ), ], ) def test_surface_line_error_raised_per_point( self, points_available: List, error_message: str ): list_of_points = [ Point(X=1, Z=2, PointType=point_type) for point_type in points_available ] with pytest.raises(ValueError) as e_info: SurfaceLine(Points=list_of_points, Name="Test surface line") generated_error_message = str(e_info.value) assert error_message in generated_error_message @pytest.mark.unittest def test_create_surface_line(self): points_available = [ PointTypeEnum.SurfaceLevelOutside, PointTypeEnum.DikeToeAtRiver, PointTypeEnum.DikeTopAtRiver, PointTypeEnum.DikeTopAtPolder, PointTypeEnum.DikeToeAtPolder, PointTypeEnum.SurfaceLevelInside, ] list_of_points = [ Point(X=1 * counter, Z=2 * counter, PointType=point_type) for counter, point_type in enumerate(points_available) ] surfaceline = SurfaceLine(Points=list_of_points, Name="Test surface line") assert surfaceline.Name == "Test surface line" @pytest.mark.integrationtest def test_serialize_surface_line(self): # Initialize class to be tested # Create surface lines X = [0, 10, 19, 20.5, 23, 23, 35, 100] Z = [1, 1, 4, 4, 4, 4, 0, 0] PointTypeList = [1, 5, 8, 10, 11, 12, 15, 25] list_of_points = [ Point( X=X[counter], Z=Z[counter], PointType=PointTypeEnum(PointTypeList[counter]), ) for counter, point_type in enumerate(X) ] surfaceline = SurfaceLine(Points=list_of_points, Name="Test surface line") # check initial expectations assert surfaceline.Name == "Test surface line" # run test root = surfaceline.serialize() # test output xml_output = Path( TestUtils.get_output_test_data_dir(""), "test_serialize_surface_line.xml" ) tree = etree.ElementTree(root) tree.write(str(xml_output), pretty_print=True) # get template file xml_test_data = Path( TestUtils.get_test_data_dir("", "test_data"), "test_serialize_surface_line.xml", ) # Line by line comparison for output_file, test_file in zip(open(xml_output), open(xml_test_data)): assert output_file.strip() == test_file.strip()