From 104aa6d584c922642ec0ae55191b6d37a1319637 Mon Sep 17 00:00:00 2001 From: Christian Ledermann Date: Wed, 13 Nov 2024 18:56:22 +0000 Subject: [PATCH] additional tests --- fastkml/helpers.py | 19 +++++++++---------- tests/repr_eq_test.py | 19 ++----------------- tests/styles_test.py | 25 +++++++++++++++++++++++++ tests/times_test.py | 11 +++++++++++ 4 files changed, 47 insertions(+), 27 deletions(-) diff --git a/fastkml/helpers.py b/fastkml/helpers.py index 2f121b66..91fdd273 100644 --- a/fastkml/helpers.py +++ b/fastkml/helpers.py @@ -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 {} diff --git a/tests/repr_eq_test.py b/tests/repr_eq_test.py index 3f56a6db..8297258f 100644 --- a/tests/repr_eq_test.py +++ b/tests/repr_eq_test.py @@ -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 @@ -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 @@ -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 diff --git a/tests/styles_test.py b/tests/styles_test.py index 67c73be1..14ae387d 100644 --- a/tests/styles_test.py +++ b/tests/styles_test.py @@ -63,6 +63,7 @@ def test_icon_style(self) -> None: serialized = icons.to_string() + assert icons.icon_href == "http://example.com/icon.png" assert ' None: assert "" in serialized assert "" 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}", @@ -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" @@ -145,6 +165,10 @@ def test_icon_style_with_hot_spot_enum_relaxed(self) -> None: "", 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" @@ -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) diff --git a/tests/times_test.py b/tests/times_test.py index 53306819..402aa2ef 100644 --- a/tests/times_test.py +++ b/tests/times_test.py @@ -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 = """ + + jan1997 + + """ + + 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