Skip to content

Commit

Permalink
additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Nov 13, 2024
1 parent 2de6210 commit b6e5a52
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 138 deletions.
36 changes: 2 additions & 34 deletions fastkml/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
from fastkml.views import LookAt
from fastkml.views import Region

__all__ = ["KmlGeometry", "NetworkLink", "Placemark", "Snippet"]

logger = logging.getLogger(__name__)

KmlGeometry = Union[
Expand Down Expand Up @@ -303,40 +305,6 @@ def __init__(
self.extended_data = extended_data
self.times = times

def __repr__(self) -> str:
"""
Return a string representation of the _Feature object.
Returns
-------
str: String representation of the _Feature object.
"""
return (
f"{self.__class__.__module__}.{self.__class__.__name__}("
f"ns={self.ns!r}, "
f"name_spaces={self.name_spaces!r}, "
f"id={self.id!r}, "
f"target_id={self.target_id!r}, "
f"name={self.name!r}, "
f"visibility={self.visibility!r}, "
f"isopen={self.isopen!r}, "
f"atom_link={self.atom_link!r}, "
f"atom_author={self.atom_author!r}, "
f"address={self.address!r}, "
f"phone_number={self.phone_number!r}, "
f"snippet={self.snippet!r}, "
f"description={self.description!r}, "
f"view={self.view!r}, "
f"times={self.times!r}, "
f"style_url={self.style_url!r}, "
f"styles={self.styles!r}, "
f"region={self.region!r}, "
f"extended_data={self.extended_data!r}, "
f"**{self._get_splat()!r},"
")"
)


registry.register(
_Feature,
Expand Down
13 changes: 0 additions & 13 deletions fastkml/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,19 +359,6 @@ def __init__(
)
self.altitude_mode = altitude_mode

def __repr__(self) -> str:
"""Create a string (c)representation for _Geometry."""
return (
f"{self.__class__.__module__}.{self.__class__.__name__}("
f"ns={self.ns!r}, "
f"name_spaces={self.name_spaces!r}, "
f"id={self.id!r}, "
f"target_id={self.target_id!r}, "
f"altitude_mode={self.altitude_mode}, "
f"**{self._get_splat()!r},"
")"
)


class Point(_Geometry):
"""
Expand Down
38 changes: 9 additions & 29 deletions fastkml/overlays.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@
from fastkml.views import LookAt
from fastkml.views import Region

__all__ = [
"GroundOverlay",
"ImagePyramid",
"KmlGeometry",
"LatLonBox",
"PhotoOverlay",
"ViewVolume",
]

logger = logging.getLogger(__name__)

KmlGeometry = Union[
Expand Down Expand Up @@ -207,35 +216,6 @@ def __init__(
self.color = clean_string(color)
self.draw_order = draw_order

def __repr__(self) -> str:
"""Create a string (c)representation for _Overlay."""
return (
f"{self.__class__.__module__}.{self.__class__.__name__}("
f"ns={self.ns!r}, "
f"name_spaces={self.name_spaces!r}, "
f"id={self.id!r}, "
f"target_id={self.target_id!r}, "
f"name={self.name!r}, "
f"visibility={self.visibility!r}, "
f"isopen={self.isopen!r}, "
f"atom_link={self.atom_link!r}, "
f"atom_author={self.atom_author!r}, "
f"address={self.address!r}, "
f"phone_number={self.phone_number!r}, "
f"snippet={self.snippet!r}, "
f"description={self.description!r}, "
f"view={self.view!r}, "
f"times={self.times!r}, "
f"style_url={self.style_url!r}, "
f"styles={self.styles!r}, "
f"region={self.region!r}, "
f"extended_data={self.extended_data!r}, "
f"color={self.color!r}, "
f"draw_order={self.draw_order!r}, "
f"icon={self.icon!r}, "
")"
)


registry.register(
_Overlay,
Expand Down
14 changes: 0 additions & 14 deletions fastkml/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,6 @@ def __init__(
self.color = clean_string(color)
self.color_mode = color_mode

def __repr__(self) -> str:
"""Create a string (c)representation for _ColorStyle."""
return (
f"{self.__class__.__module__}.{self.__class__.__name__}("
f"ns={self.ns!r}, "
f"name_spaces={self.name_spaces!r}, "
f"id={self.id!r}, "
f"target_id={self.target_id!r}, "
f"color={self.color!r}, "
f"color_mode={self.color_mode}, "
f"**{self._get_splat()!r},"
")"
)


registry.register(
_ColorStyle,
Expand Down
37 changes: 7 additions & 30 deletions fastkml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def has_attribute_values(obj: object, **kwargs: Any) -> bool:
return False


def find_in(
def find_all(
obj: object,
*,
of_type: Optional[Union[Type[object], Tuple[Type[object], ...]]] = None,
**kwargs: Any,
) -> Generator[object, None, None]:
"""
Find all instances of type in the attributes of an object.
Find all instances of a given type with attributes matching the kwargs.
Args:
----
Expand All @@ -50,6 +50,11 @@ def find_in(
An iterable of all instances of the given type in the given object.
"""
if (of_type is None or isinstance(obj, of_type)) and has_attribute_values(
obj,
**kwargs,
):
yield obj
try:
attrs = (attr for attr in obj.__dict__ if not attr.startswith("_"))
except AttributeError:
Expand All @@ -63,34 +68,6 @@ def find_in(
yield from find_all(attr, of_type=of_type, **kwargs)


def find_all(
obj: object,
*,
of_type: Optional[Union[Type[object], Tuple[Type[object], ...]]] = None,
**kwargs: Any,
) -> Generator[object, None, None]:
"""
Find all instances of a given type with attributes matching the kwargs.
Args:
----
obj: The object to search.
of_type: The type(s) to search for or None for any type.
**kwargs: Attributes of the object to match.
Returns:
-------
An iterable of all instances of the given type in the given object.
"""
if (of_type is None or isinstance(obj, of_type)) and has_attribute_values(
obj,
**kwargs,
):
yield obj
yield from find_in(obj, of_type=of_type, **kwargs)


def find(
obj: object,
*,
Expand Down
18 changes: 0 additions & 18 deletions fastkml/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,6 @@ def __init__(
self.tilt = tilt
self.altitude_mode = altitude_mode

def __repr__(self) -> str:
"""Create a string (c)representation for _AbstractView."""
return (
f"{self.__class__.__module__}.{self.__class__.__name__}("
f"ns={self.ns!r}, "
f"name_spaces={self.name_spaces!r}, "
f"id={self.id!r}, "
f"target_id={self.target_id!r}, "
f"longitude={self.longitude!r}, "
f"latitude={self.latitude!r}, "
f"altitude={self.altitude!r}, "
f"heading={self.heading!r}, "
f"tilt={self.tilt!r}, "
f"altitude_mode={self.altitude_mode}, "
f"**{self._get_splat()!r},"
")"
)


registry.register(
_AbstractView,
Expand Down
11 changes: 11 additions & 0 deletions tests/geometries/coordinates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ def test_coordinates_from_string_with_whitespace(self) -> None:
(-123.940449937288, 49.16927524669021, 17.0),
]

def test_coordinates_from_string_parse_error_relaxed(self) -> None:
"""Test the from_string method with a parse error."""
coordinates = Coordinates.from_string(
'<kml:coordinates xmlns:kml="http://www.opengis.net/kml/2.2">'
"0.a,b.000000 1.0,1.0"
"</kml:coordinates>",
strict=False,
)

assert not coordinates.coords


class TestCoordinatesLxml(Lxml, TestCoordinates):
pass
6 changes: 6 additions & 0 deletions tests/geometries/linestring_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def test_init(self) -> None:
assert line_string.altitude_mode is None
assert line_string.extrude is None

def test_unequal_coordinates(self) -> None:
coords = Coordinates()
lines = LineString()

assert lines != coords

def test_geometry_error(self) -> None:
"""Test GeometryError."""
p = geo.LineString(((1, 2), (2, 0)))
Expand Down
7 changes: 7 additions & 0 deletions tests/geometries/point_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ def test_to_string_3d_precision_10(self) -> None:
in point.to_string(precision=10)
)

def test_point_unequal(self) -> None:
"""Test the __eq__ method."""
p = geo.Point(1, 2)
point = Point(geometry=p)

assert point != p

def test_to_string_empty_geometry(self) -> None:
"""Test the to_string method."""
point = Point(geometry=geo.Point(None, None)) # type: ignore[arg-type]
Expand Down
16 changes: 16 additions & 0 deletions tests/geometries/polygon_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@
class TestStdLibrary(StdLibrary):
"""Test with the standard library."""

def test_equal(self) -> None:
"""Test equality."""
poly = geo.Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])

polygon1 = Polygon(ns="", geometry=poly)
polygon2 = Polygon(ns="", geometry=poly)

assert polygon1 == polygon2

def test_not_equal(self) -> None:
"""Test inequality."""
polygon = Polygon(ns="")
boundary = OuterBoundaryIs(ns="")

assert polygon != boundary

def test_exterior_only(self) -> None:
"""Test exterior only."""
poly = geo.Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
Expand Down
16 changes: 16 additions & 0 deletions tests/validator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import pytest

from fastkml import atom
from fastkml import config
from fastkml.validator import get_schema_parser
from fastkml.validator import validate
from tests.base import Lxml
Expand Down Expand Up @@ -105,3 +106,18 @@ def test_validate_invalid_element(self) -> None:

with pytest.raises(AssertionError):
validate(element=link.etree_element())

def test_get_schema_parser(self) -> None:
path = TEST_DIR / "ogc_conformance" / "data" / "atom-author-link.xsd"
assert get_schema_parser(path)

def test_validate_empty_element(self) -> None:
element = config.etree.Element("kml")
with pytest.raises(
AssertionError,
match=(
"^Element 'kml': "
"No matching global declaration available for the validation root.$"
),
):
assert validate(element=element)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ per-file-ignores =
examples/*.py: DALL
fastkml/gx.py: LIT002
fastkml/views.py: LIT002
fastkml/utils.py: CCR001
fastkml/registry.py: E704
docs/conf.py: E402
enable-extensions=G
Expand Down

0 comments on commit b6e5a52

Please sign in to comment.