Skip to content

Commit

Permalink
Add test for Enum values to raise exception for strict parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Apr 7, 2024
1 parent 7b45d07 commit 12473d1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
12 changes: 11 additions & 1 deletion fastkml/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,17 @@ def attribute_float_kwarg(
classes: Tuple[known_types, ...],
strict: bool,
) -> Dict[str, float]:
return {kwarg: float(element.get(node_name))} if element.get(node_name) else {}
try:
return {kwarg: float(element.get(node_name))} if element.get(node_name) else {}
except ValueError as exc:
handle_error(
error=exc,
strict=strict,
element=element,
node=element,
expected="Float",
)
return {}


def subelement_enum_kwarg(
Expand Down
28 changes: 28 additions & 0 deletions tests/styles_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Test the styles classes."""
import pytest

from fastkml import styles
from fastkml.enums import ColorMode
from fastkml.enums import DisplayMode
from fastkml.enums import PairKey
from fastkml.enums import Units
from fastkml.exceptions import KMLParseError
from tests.base import Lxml
from tests.base import StdLibrary

Expand Down Expand Up @@ -130,6 +132,32 @@ def test_icon_style_read(self) -> None:
assert icons.hot_spot.xunits.value == "fraction"
assert icons.hot_spot.yunits.value == "insetPixels"

def test_icon_style_with_hot_spot_enum_relaxed(self) -> None:
icons = styles.IconStyle.class_from_string(
'<kml:IconStyle xmlns:kml="http://www.opengis.net/kml/2.2" '
'id="id-1" targetId="target-1">'
"<kml:color>ff2200ff</kml:color><kml:colorMode>random</kml:colorMode>"
"<kml:scale>5</kml:scale><kml:heading>20</kml:heading><kml:Icon>"
"<kml:href>http://example.com/icon.png</kml:href></kml:Icon>"
'<kml:hotSpot x="0.5" y="0.7" xunits="Fraction" yunits="Insetpixels"/>'
"</kml:IconStyle>",
strict=False,
)
assert icons.hot_spot.xunits.value == "fraction"
assert icons.hot_spot.yunits.value == "insetPixels"

def test_icon_style_with_hot_spot_enum_strict(self) -> None:
with pytest.raises(KMLParseError):
styles.IconStyle.class_from_string(
'<kml:IconStyle xmlns:kml="http://www.opengis.net/kml/2.2" '
'id="id-1" targetId="target-1">'
"<kml:color>ff2200ff</kml:color><kml:colorMode>random</kml:colorMode>"
"<kml:scale>5</kml:scale><kml:heading>20</kml:heading><kml:Icon>"
"<kml:href>http://example.com/icon.png</kml:href></kml:Icon>"
'<kml:hotSpot x="0.5" y="0.7" xunits="Fraction" yunits="Insetpixels"/>'
"</kml:IconStyle>"
)

def test_line_style(self) -> None:
lines = styles.LineStyle(
id="id-0",
Expand Down

0 comments on commit 12473d1

Please sign in to comment.