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 8c23efe commit 104aa6d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
19 changes: 9 additions & 10 deletions fastkml/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1100,16 +1100,15 @@ def datetime_subelement_kwarg(
return {}
node_text = node.text.strip() if node.text else ""
if node_text:
try:
return {kwarg: cls.parse(node_text)} # type: ignore[attr-defined]
except ValueError as exc:
handle_error(
error=exc,
strict=strict,
element=element,
node=node,
expected="DateTime",
)
if kdt := cls.parse(node_text): # type: ignore[attr-defined]
return {kwarg: kdt}
handle_error(
error=ValueError(f"Invalid DateTime value: {node_text}"),
strict=strict,
element=element,
node=node,
expected="DateTime",
)
return {}


Expand Down
19 changes: 2 additions & 17 deletions tests/repr_eq_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

"""Test the __repr__ and __eq__ methods."""

import difflib
from textwrap import wrap
from typing import Final

from pygeoif.geometry import LinearRing
Expand Down Expand Up @@ -1903,19 +1901,6 @@ class TestRepr(StdLibrary):
],
)

def diff_compare(self, a: str, b: str) -> None:
"""Compare two strings and print the differences."""
differ = difflib.Differ()
for line, d in enumerate(differ.compare(a.split(), b.split())):
if d[0] in ("+", "-"):
print(line, d) # noqa: T201

for i, chunk in enumerate(zip(wrap(a, 100), wrap(b, 100))):
if chunk[0] != chunk[1]:
print(i * 100) # noqa: T201
print(chunk[0]) # noqa: T201
print(chunk[1]) # noqa: T201

def test_repr(self) -> None:
"""Test the __repr__ method."""
new_doc = eval(repr(self.clean_doc), {}, eval_locals) # noqa: S307
Expand All @@ -1929,10 +1914,10 @@ def test_str(self) -> None:

def test_eq_str_round_trip(self) -> None:
"""Test the equality of the original and the round-tripped document."""
new_doc = fastkml.KML.from_string(self.clean_doc.to_string(precision=15))
new_doc = fastkml.KML.from_string(self.clean_doc.to_string())

assert str(self.clean_doc) == str(new_doc)
assert repr(new_doc) == repr(self.clean_doc)
assert new_doc == self.clean_doc
# srict equality is not a given new_doc == self.clean_doc


Expand Down
25 changes: 25 additions & 0 deletions tests/styles_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def test_icon_style(self) -> None:

serialized = icons.to_string()

assert icons.icon_href == "http://example.com/icon.png"
assert '<kml:IconStyle xmlns:kml="http://www.opengis.net/kml/2.2"' in serialized
assert 'id="id-0"' in serialized
assert 'targetId="target-0"' in serialized
Expand All @@ -75,6 +76,23 @@ def test_icon_style(self) -> None:
assert "</kml:Icon>" in serialized
assert "</kml:IconStyle>" in serialized

def test_icon_style_icon_href(self) -> None:
icons = styles.IconStyle(
icon_href="http://example.com/icon.png",
)

assert icons.icon
assert icons.icon.href == "http://example.com/icon.png"

def test_icon_style_icon_and_href(self) -> None:
icons = styles.IconStyle(
icon=links.Icon(href="http://example1.com/icon.png"),
icon_href="http://example2.com/icon.png",
)

assert icons.icon
assert icons.icon.href == "http://example1.com/icon.png"

def test_icon_style_with_hot_spot(self) -> None:
icon_style = styles.IconStyle(
ns="{http://www.opengis.net/kml/2.2}",
Expand Down Expand Up @@ -131,6 +149,8 @@ def test_icon_style_read(self) -> None:
assert icons.hot_spot
assert icons.hot_spot.x == 0.5
assert icons.hot_spot.y == 0.7
assert icons.hot_spot.xunits
assert icons.hot_spot.yunits
assert icons.hot_spot.xunits.value == "fraction"
assert icons.hot_spot.yunits.value == "insetPixels"

Expand All @@ -145,6 +165,10 @@ def test_icon_style_with_hot_spot_enum_relaxed(self) -> None:
"</kml:IconStyle>",
strict=False,
)

assert icons.hot_spot
assert icons.hot_spot.xunits
assert icons.hot_spot.yunits
assert icons.hot_spot.xunits.value == "fraction"
assert icons.hot_spot.yunits.value == "insetPixels"

Expand Down Expand Up @@ -422,6 +446,7 @@ def test_style_read(self) -> None:
assert style.styles[0].color_mode == ColorMode.random
assert style.styles[0].scale == 1.0
assert style.styles[0].heading == 0
assert style.styles[0].icon
assert style.styles[0].icon.href == "http://example.com/icon.png"

assert isinstance(style.styles[1], styles.LabelStyle)
Expand Down
11 changes: 11 additions & 0 deletions tests/times_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@ def test_read_timestamp_ymd(self) -> None:
assert ts.timestamp.resolution == DateTimeResolution.date
assert ts.timestamp.dt == datetime.date(1997, 7, 16)

def test_read_timestamp_invalid(self) -> None:
doc = """
<TimeStamp>
<when>jan1997</when>
</TimeStamp>
"""

ts = kml.TimeStamp.from_string(doc, ns="", strict=False)

assert ts.timestamp is None

def test_read_timestamp_utc(self) -> None:
# dateTime (YYYY-MM-DDThh:mm:ssZ)
# Here, T is the separator between the calendar and the hourly notation
Expand Down

0 comments on commit 104aa6d

Please sign in to comment.