Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Oct 19, 2024
1 parent 6c106d1 commit 18e6b5b
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 204 deletions.
3 changes: 0 additions & 3 deletions fastkml/kml.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,6 @@ def append(
kmlobj: kml_children,
) -> None:
"""Append a feature."""
if kmlobj is self:
msg = "Cannot append self"
raise ValueError(msg)
self.features.append(kmlobj)

@classmethod
Expand Down
8 changes: 4 additions & 4 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Base classes to run the tests both with the std library and lxml."""
import xml.etree.ElementTree
import xml.etree.ElementTree as ET

import pytest

Expand All @@ -33,8 +33,8 @@ class StdLibrary:
"""Configure test to run with the standard library."""

def setup_method(self) -> None:
"""Always test with the same parser."""
config.set_etree_implementation(xml.etree.ElementTree)
"""Ensure to always test with the standard library xml ElementTree parser."""
config.set_etree_implementation(ET)
config.set_default_namespaces()


Expand All @@ -47,6 +47,6 @@ class Lxml:
"""

def setup_method(self) -> None:
"""Always test with the same parser."""
"""Ensure to always test with the lxml parse."""
config.set_etree_implementation(lxml.etree)
config.set_default_namespaces()
2 changes: 1 addition & 1 deletion tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

"""Test the configuration options."""

import xml.etree.ElementTree as ET
from xml.etree import ElementTree as ET

import pytest

Expand Down
22 changes: 17 additions & 5 deletions tests/containers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
"""Test the kml classes."""


from fastkml import kml, containers, features
import pytest

from fastkml import containers
from fastkml import features
from fastkml import kml
from fastkml import styles
from tests.base import Lxml
from tests.base import StdLibrary
import pytest


class TestStdLibrary(StdLibrary):
Expand Down Expand Up @@ -71,7 +75,7 @@ def test_container_creation(self) -> None:
ns="ns",
id="id",
target_id="target_id",
name="name"
name="name",
)
assert container.ns == "ns"
assert container.name == "name"
Expand All @@ -81,7 +85,7 @@ def test_container_feature_append(self) -> None:
ns="ns",
id="id",
target_id="target_id",
name="name"
name="name",
)
feature = features._Feature(name="new_feature")
container.append(feature)
Expand All @@ -93,10 +97,18 @@ def test_document_container_get_style_url(self) -> None:
document = containers.Document(
name="Document",
ns="ns",
style_url="www.styleurl.com"
style_url=styles.StyleUrl(url="www.styleurl.com"),
)
assert document.get_style_by_url(style_url="www.styleurl.com") is None

def test_document_container_get_style_url_id(self) -> None:
style = styles.Style(id="style-0")
document = containers.Document(
name="Document",
ns="ns",
styles=[style],
)
assert document.get_style_by_url(style_url="#style-0") == style


class TestLxml(Lxml, TestStdLibrary):
Expand Down
7 changes: 5 additions & 2 deletions tests/features_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ def test_placemark_geometry_and_kml_geometry_parameter_set(self) -> None:
pt = geo.Point(10, 20)
point = geometry.Point(geometry=pt)

with pytest.raises(ValueError):
with pytest.raises(
ValueError,
match="^You can only specify one of kml_geometry or geometry$",
):
features.Placemark(geometry=pt, kml_geometry=point)

def test_network_link_with_link_parameter_only(self) -> None:
"""NetworkLink object with Link parameter only"""
"""Test NetworkLink object with Link parameter only."""
network_link = features.NetworkLink(
link=links.Link(href="http://example.com/kml_file.kml"),
)
Expand Down
20 changes: 13 additions & 7 deletions tests/geometries/boundaries_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

"""Test the Outer and Inner Boundary classes."""

import pytest
from fastkml.exceptions import GeometryError
from typing import Type
from typing import Union

import pygeoif.geometry as geo
import pytest

from fastkml.exceptions import GeometryError
from fastkml.geometry import Coordinates
from fastkml.geometry import InnerBoundaryIs
from fastkml.geometry import LinearRing
Expand Down Expand Up @@ -75,21 +78,24 @@ def test_inner_boundary(self) -> None:
"</kml:coordinates></kml:LinearRing></kml:innerBoundaryIs>"
)

def _test_boundary_geometry_error(self, boundary_class):
p = geo.Point(1, 2)
def _test_boundary_geometry_error(
self,
boundary_class: Union[Type[InnerBoundaryIs], Type[OuterBoundaryIs]],
) -> None:
p = geo.LinearRing(((1, 2), (2, 0)))
coords = ((1, 2), (2, 0), (0, 0), (1, 2))

with pytest.raises(GeometryError):
boundary_class(
kml_geometry=LinearRing(kml_coordinates=Coordinates(coords=coords)),
geometry=p
geometry=p,
)

def test_outer_boundary_geometry_error(self):
def test_outer_boundary_geometry_error(self) -> None:
"""Test that OuterBoundaryIs raises GeometryError with invalid geometry."""
self._test_boundary_geometry_error(OuterBoundaryIs)

def test_inner_boundary_geometry_error(self):
def test_inner_boundary_geometry_error(self) -> None:
"""Test that InnerBoundaryIs raises GeometryError with invalid geometry."""
self._test_boundary_geometry_error(InnerBoundaryIs)

Expand Down
103 changes: 60 additions & 43 deletions tests/geometries/functions_test.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,88 @@
from unittest.mock import Mock, patch
# Copyright (C) 2024 Rishit Chaudhary, Christian Ledermann
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# This library 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 Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Test the geometry error handling."""
from typing import Callable
from unittest.mock import Mock
from unittest.mock import patch

import pytest
from fastkml.exceptions import KMLParseError, KMLWriteError
from fastkml.geometry import handle_invalid_geometry_error

from fastkml.enums import Verbosity
from fastkml.exceptions import KMLParseError
from fastkml.exceptions import KMLWriteError
from fastkml.geometry import coordinates_subelement
from fastkml.geometry import handle_invalid_geometry_error
from tests.base import StdLibrary


class TestGeometryFunctions(StdLibrary):
"""Test functions in Geometry"""
"""Test functions in Geometry."""

@patch('fastkml.config.etree.tostring')
def test_handle_invalid_geometry_error_true(self, mock_to_string) -> None:
@patch("fastkml.config.etree.tostring", return_value=b"<kml:../>")
def test_handle_invalid_geometry_error_true(
self,
mock_to_string: Callable[..., str],
) -> None:
mock_element = Mock()
with pytest.raises(KMLParseError):
with pytest.raises(
KMLParseError,
match=mock_to_string.return_value.decode(), # type: ignore[attr-defined]
):
handle_invalid_geometry_error(
error=ValueError,
error=ValueError(),
element=mock_element,
strict=True
strict=True,
)
mock_to_string.assert_called_once_with( # type: ignore[attr-defined]
mock_element,
encoding="UTF-8",
)

@patch('fastkml.config.etree.tostring')
def test_handle_invalid_geometry_error_false(self, mock_to_string) -> None:
@patch("fastkml.config.etree.tostring", return_value=b"<kml:... />")
def test_handle_invalid_geometry_error_false(
self,
mock_to_string: Callable[..., str],
) -> None:
mock_element = Mock()
assert handle_invalid_geometry_error(
error=ValueError,
handle_invalid_geometry_error(
error=ValueError(),
element=mock_element,
strict=False
) is None
strict=False,
)
mock_to_string.assert_called_once_with( # type: ignore[attr-defined]
mock_element,
encoding="UTF-8",
)

def test_coordinates_subelement_exception(self) -> None:
obj = Mock()
setattr(obj,
'coordinates',
[(1.123456, 2.654321, 3.111111, 4.222222)]
)
obj.coordinates = [(1.123456, 2.654321, 3.111111, 4.222222)]

element = Mock()

precision = None
attr_name = 'coordinates'
precision = 9
attr_name = "coordinates"

with pytest.raises(KMLWriteError):
coordinates_subelement(
obj=obj,
attr_name=attr_name,
node_name=None,
node_name="",
element=element,
precision=precision,
verbosity=None,
default=None
verbosity=Verbosity.terse,
default=None,
)

def test_coordinates_subelement_getattr(self) -> None:
obj = Mock()
setattr(obj, 'coordinates', [(1.123456, 2.654321), (3.123456, 4.654321)])

element = Mock()

precision = 4
attr_name = 'coordinates'

assert coordinates_subelement(
obj=None,
attr_name=attr_name,
node_name=None,
element=element,
precision=precision,
verbosity=None,
default=None
) is None
9 changes: 5 additions & 4 deletions tests/geometries/linestring_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

from fastkml import exceptions
from fastkml.enums import Verbosity
from fastkml.exceptions import GeometryError, KMLParseError
from fastkml.geometry import Coordinates, LineString
from fastkml.exceptions import GeometryError
from fastkml.exceptions import KMLParseError
from fastkml.geometry import Coordinates
from fastkml.geometry import LineString
from tests.base import Lxml
from tests.base import StdLibrary

Expand All @@ -40,7 +42,7 @@ def test_init(self) -> None:

def test_geometry_error(self) -> None:
"""Test GeometryError."""
p = geo.Point(1, 2)
p = geo.LineString(((1, 2), (2, 0)))
q = Coordinates(ns="ns")

with pytest.raises(GeometryError):
Expand Down Expand Up @@ -75,7 +77,6 @@ def test_from_string(self) -> None:
)

def test_mixed_2d_3d_coordinates_from_string(self) -> None:

linestring = LineString.class_from_string(
'<LineString xmlns="http://www.opengis.net/kml/2.2">'
"<extrude>1</extrude>"
Expand Down
11 changes: 5 additions & 6 deletions tests/geometries/multigeometry_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Test the geometry classes."""
import pytest
from fastkml.exceptions import GeometryError
import pygeoif.geometry as geo
import pytest

from fastkml.enums import Verbosity
from fastkml.geometry import Coordinates, MultiGeometry
from fastkml.exceptions import GeometryError
from fastkml.geometry import MultiGeometry
from tests.base import Lxml
from tests.base import StdLibrary

Expand Down Expand Up @@ -299,11 +299,10 @@ def test_multi_geometries_verbose(self) -> None:

def test_geometry_error(self) -> None:
"""Test GeometryError."""
p = geo.Point(1, 2)
q = Coordinates(ns="ns")
p = geo.MultiPoint(((1.0, 2.0),))

with pytest.raises(GeometryError):
MultiGeometry(geometry=p, kml_geometries=q)
MultiGeometry(geometry=p, kml_geometries=(MultiGeometry(geometry=p),))

def test_multi_geometries_read(self) -> None:
xml = (
Expand Down
8 changes: 3 additions & 5 deletions tests/geometries/point_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
import pytest

from fastkml.enums import Verbosity
from fastkml.exceptions import GeometryError, KMLParseError
from fastkml.geometry import Point
from fastkml.exceptions import GeometryError
from fastkml.exceptions import KMLParseError
from fastkml.geometry import Coordinates
from fastkml.geometry import Point
from tests.base import Lxml
from tests.base import StdLibrary

Expand All @@ -48,7 +49,6 @@ def test_geometry_error(self) -> None:
with pytest.raises(GeometryError):
Point(geometry=p, kml_coordinates=q)


def test_to_string_2d(self) -> None:
"""Test the to_string method."""
p = geo.Point(1, 2)
Expand Down Expand Up @@ -240,7 +240,6 @@ def test_from_string_empty_coordinates(self) -> None:
assert point.geometry is None

def test_from_string_invalid_coordinates(self) -> None:

point = Point.class_from_string(
'<Point xmlns="http://www.opengis.net/kml/2.2">'
"<coordinates>1</coordinates></Point>",
Expand All @@ -249,7 +248,6 @@ def test_from_string_invalid_coordinates(self) -> None:
assert not point

def test_from_string_invalid_coordinates_4d(self) -> None:

point = Point.class_from_string(
'<Point xmlns="http://www.opengis.net/kml/2.2">'
"<coordinates>1,2,3,4</coordinates></Point>",
Expand Down
Loading

0 comments on commit 18e6b5b

Please sign in to comment.