| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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(), |
| 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( |
| 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( |
| 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 |
) |