Skip to content

Commit

Permalink
add hypothesis test for Link
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Oct 31, 2024
1 parent a7a1005 commit 2ce6a64
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 33 deletions.
67 changes: 34 additions & 33 deletions fastkml/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,28 +130,6 @@ def __bool__(self) -> bool:
set_element=text_subelement,
),
)
registry.register(
Link,
RegistryItem(
ns_ids=("kml",),
attr_name="view_format",
node_name="viewFormat",
classes=(str,),
get_kwarg=subelement_text_kwarg,
set_element=text_subelement,
),
)
registry.register(
Link,
RegistryItem(
ns_ids=("kml",),
attr_name="http_query",
node_name="httpQuery",
classes=(str,),
get_kwarg=subelement_text_kwarg,
set_element=text_subelement,
),
)
registry.register(
Link,
RegistryItem(
Expand All @@ -164,6 +142,18 @@ def __bool__(self) -> bool:
default=RefreshMode.on_change,
),
)
registry.register(
Link,
RegistryItem(
ns_ids=("kml",),
attr_name="refresh_interval",
node_name="refreshInterval",
classes=(float,),
get_kwarg=subelement_float_kwarg,
set_element=float_subelement,
default=4.0,
),
)
registry.register(
Link,
RegistryItem(
Expand All @@ -180,8 +170,8 @@ def __bool__(self) -> bool:
Link,
RegistryItem(
ns_ids=("kml",),
attr_name="refresh_interval",
node_name="refreshInterval",
attr_name="view_refresh_time",
node_name="viewRefreshTime",
classes=(float,),
get_kwarg=subelement_float_kwarg,
set_element=float_subelement,
Expand All @@ -192,24 +182,35 @@ def __bool__(self) -> bool:
Link,
RegistryItem(
ns_ids=("kml",),
attr_name="view_refresh_time",
node_name="viewRefreshTime",
attr_name="view_bound_scale",
node_name="viewBoundScale",
classes=(float,),
get_kwarg=subelement_float_kwarg,
set_element=float_subelement,
default=4.0,
default=1.0,
),
)
registry.register(
Link,
RegistryItem(
ns_ids=("kml",),
attr_name="view_bound_scale",
node_name="viewBoundScale",
classes=(float,),
get_kwarg=subelement_float_kwarg,
set_element=float_subelement,
default=1.0,
attr_name="view_format",
node_name="viewFormat",
classes=(str,),
get_kwarg=subelement_text_kwarg,
set_element=text_subelement,
default="BBOX=[bboxWest],[bboxSouth],[bboxEast],[bboxNorth]",
),
)
registry.register(
Link,
RegistryItem(
ns_ids=("kml",),
attr_name="http_query",
node_name="httpQuery",
classes=(str,),
get_kwarg=subelement_text_kwarg,
set_element=text_subelement,
),
)

Expand Down
12 changes: 12 additions & 0 deletions tests/hypothesis/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import re
import string
from functools import partial
from urllib.parse import urlencode

from hypothesis import strategies as st

Expand All @@ -28,3 +29,14 @@
regex=re.compile(r"^[A-Za-z_][\w.-]*$"),
alphabet=ID_TEXT,
)


@st.composite
def query_strings(draw: st.DrawFn) -> str:
params = draw(
st.dictionaries(
keys=st.text(alphabet=string.ascii_letters, min_size=1),
values=st.text(alphabet=string.printable),
),
)
return urlencode(params)
87 changes: 87 additions & 0 deletions tests/hypothesis/links_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright (C) 2024 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 Link and Icon."""
import string
import typing

from hypothesis import given
from hypothesis import settings
from hypothesis import strategies as st
from hypothesis.provisional import urls

import fastkml
import fastkml.enums
from fastkml.validate import validate
from tests.base import Lxml
from tests.hypothesis.common import nc_name
from tests.hypothesis.common import query_strings


class TestLxml(Lxml):
@given(
id=st.one_of(st.none(), nc_name()),
target_id=st.one_of(st.none(), nc_name()),
href=st.one_of(st.none(), urls()),
refresh_mode=st.one_of(st.none(), st.sampled_from(fastkml.enums.RefreshMode)),
refresh_interval=st.one_of(
st.none(),
st.floats(allow_infinity=False, allow_nan=False),
),
view_refresh_mode=st.one_of(
st.none(),
st.sampled_from(fastkml.enums.ViewRefreshMode),
),
view_refresh_time=st.one_of(
st.none(),
st.floats(allow_infinity=False, allow_nan=False),
),
view_bound_scale=st.one_of(
st.none(),
st.floats(allow_infinity=False, allow_nan=False),
),
view_format=st.one_of(
st.none(),
st.text(string.ascii_letters + string.punctuation),
),
http_query=st.one_of(st.none(), query_strings()),
)
@settings(deadline=None)
def test_fuzz_link(
self,
id: typing.Optional[str],
target_id: typing.Optional[str],
href: typing.Optional[str],
refresh_mode: typing.Optional[fastkml.enums.RefreshMode],
refresh_interval: typing.Optional[float],
view_refresh_mode: typing.Optional[fastkml.enums.ViewRefreshMode],
view_refresh_time: typing.Optional[float],
view_bound_scale: typing.Optional[float],
view_format: typing.Optional[str],
http_query: typing.Optional[str],
) -> None:
link = fastkml.Link(
id=id,
target_id=target_id,
href=href,
refresh_mode=refresh_mode,
refresh_interval=refresh_interval,
view_refresh_mode=view_refresh_mode,
view_refresh_time=view_refresh_time,
view_bound_scale=view_bound_scale,
view_format=view_format,
http_query=http_query,
)
assert validate(element=link.etree_element())

0 comments on commit 2ce6a64

Please sign in to comment.