Debian Codegraph @

tests/geometries/functions_test.py

raw symbols SCIP
1 # Copyright (C) 2024 Rishit Chaudhary, Christian Ledermann
2 #
3 # This library is free software; you can redistribute it and/or modify it under
4 # the terms of the GNU Lesser General Public License as published by the Free
5 # Software Foundation; either version 2.1 of the License, or (at your option)
6 # any later version.
7 #
8 # This library is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Lesser General Public License
14 # along with this library; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 """Test the geometry error handling."""
17
18 from typing import Callable
19 from unittest.mock import Mock
20 from unittest.mock import patch
21
22 import pytest
23
24 from fastkml.enums import Verbosity
25 from fastkml.exceptions import KMLParseError
26 from fastkml.exceptions import KMLWriteError
27 from fastkml.geometry import coordinates_subelement
28 from fastkml.geometry import handle_invalid_geometry_error
29 from tests.base import StdLibrary
30
31
32 class TestGeometryFunctions(StdLibrary):
33 """Test functions in Geometry."""
34
35 @patch("fastkml.config.etree.tostring", return_value=b"<kml:../>")
36 def test_handle_invalid_geometry_error_true(
37 self,
38 mock_to_string: Callable[..., str],
39 ) -> None:
40 mock_element = Mock()
41 with pytest.raises(
42 KMLParseError,
43 match=mock_to_string.return_value.decode(), # type: ignore[attr-defined]
44 ):
45 handle_invalid_geometry_error(
46 error=ValueError(),
47 element=mock_element,
48 strict=True,
49 )
50 mock_to_string.assert_called_once_with( # type: ignore[attr-defined]
51 mock_element,
52 encoding="UTF-8",
53 )
54
55 @patch("fastkml.config.etree.tostring", return_value=b"<kml:... />")
56 def test_handle_invalid_geometry_error_false(
57 self,
58 mock_to_string: Callable[..., str],
59 ) -> None:
60 mock_element = Mock()
61 handle_invalid_geometry_error(
62 error=ValueError(),
63 element=mock_element,
64 strict=False,
65 )
66 mock_to_string.assert_called_once_with( # type: ignore[attr-defined]
67 mock_element,
68 encoding="UTF-8",
69 )
70
71 def test_coordinates_subelement_exception(self) -> None:
72 obj = Mock()
73 obj.coordinates = [(1.123456, 2.654321, 3.111111, 4.222222)]
74
75 element = Mock()
76
77 precision = 9
78 attr_name = "coordinates"
79
80 with pytest.raises(KMLWriteError):
81 coordinates_subelement(
82 obj=obj,
83 attr_name=attr_name,
84 node_name="",
85 element=element,
86 precision=precision,
87 verbosity=Verbosity.terse,
88 default=None,
89 )