| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
"""Property-based tests for the views module.""" |
| 17 |
|
| 18 |
from collections.abc import Iterable |
| 19 |
from typing import Optional |
| 20 |
from typing import Union |
| 21 |
|
| 22 |
import pygeoif.types |
| 23 |
from hypothesis import given |
| 24 |
from hypothesis import strategies as st |
| 25 |
from hypothesis.provisional import urls |
| 26 |
|
| 27 |
import fastkml |
| 28 |
import fastkml.atom |
| 29 |
import fastkml.data |
| 30 |
import fastkml.enums |
| 31 |
import fastkml.features |
| 32 |
import fastkml.gx |
| 33 |
import fastkml.links |
| 34 |
import fastkml.model |
| 35 |
import fastkml.styles |
| 36 |
import fastkml.views |
| 37 |
from tests.base import Lxml |
| 38 |
from tests.hypothesis.common import assert_repr_roundtrip |
| 39 |
from tests.hypothesis.common import assert_str_roundtrip |
| 40 |
from tests.hypothesis.common import assert_str_roundtrip_terse |
| 41 |
from tests.hypothesis.common import assert_str_roundtrip_verbose |
| 42 |
from tests.hypothesis.strategies import geometries |
| 43 |
from tests.hypothesis.strategies import kml_datetimes |
| 44 |
from tests.hypothesis.strategies import lat_lon_alt_boxes |
| 45 |
from tests.hypothesis.strategies import lods |
| 46 |
from tests.hypothesis.strategies import nc_name |
| 47 |
from tests.hypothesis.strategies import styles |
| 48 |
from tests.hypothesis.strategies import track_items |
| 49 |
from tests.hypothesis.strategies import xml_text |
| 50 |
|
| 51 |
|
| 52 |
class TestLxml(Lxml): |
| 53 |
@given( |
| 54 |
text=st.one_of(st.none(), xml_text(min_size=1)), |
| 55 |
max_lines=st.one_of( |
| 56 |
st.none(), |
| 57 |
st.integers(min_value=0, max_value=2_147_483_647), |
| 58 |
), |
| 59 |
) |
| 60 |
def test_fuzz_snippet( |
| 61 |
self, |
| 62 |
text: Optional[str], |
| 63 |
max_lines: Optional[int], |
| 64 |
) -> None: |
| 65 |
snippet = fastkml.features.Snippet(text=text, max_lines=max_lines) |
| 66 |
|
| 67 |
assert_repr_roundtrip(snippet) |
| 68 |
assert_str_roundtrip(snippet) |
| 69 |
assert_str_roundtrip_terse(snippet) |
| 70 |
assert_str_roundtrip_verbose(snippet) |
| 71 |
|
| 72 |
@given( |
| 73 |
geometry=st.one_of( |
| 74 |
st.none(), |
| 75 |
geometries(), |
| 76 |
), |
| 77 |
) |
| 78 |
def test_fuzz_placemark_geometry_only( |
| 79 |
self, |
| 80 |
geometry: Union[ |
| 81 |
pygeoif.types.GeoType, |
| 82 |
pygeoif.types.GeoCollectionType, |
| 83 |
None, |
| 84 |
], |
| 85 |
) -> None: |
| 86 |
placemark = fastkml.Placemark( |
| 87 |
geometry=geometry, |
| 88 |
) |
| 89 |
|
| 90 |
assert_repr_roundtrip(placemark) |
| 91 |
assert_str_roundtrip(placemark) |
| 92 |
assert_str_roundtrip_terse(placemark) |
| 93 |
assert_str_roundtrip_verbose(placemark) |
| 94 |
|
| 95 |
@given( |
| 96 |
view=st.one_of( |
| 97 |
st.builds( |
| 98 |
fastkml.views.Camera, |
| 99 |
longitude=st.floats( |
| 100 |
allow_nan=False, |
| 101 |
allow_infinity=False, |
| 102 |
min_value=-180, |
| 103 |
max_value=180, |
| 104 |
).filter(lambda x: x != 0), |
| 105 |
latitude=st.floats( |
| 106 |
allow_nan=False, |
| 107 |
allow_infinity=False, |
| 108 |
min_value=-90, |
| 109 |
max_value=90, |
| 110 |
).filter(lambda x: x != 0), |
| 111 |
altitude=st.floats(allow_nan=False, allow_infinity=False).filter( |
| 112 |
lambda x: x != 0, |
| 113 |
), |
| 114 |
), |
| 115 |
st.builds( |
| 116 |
fastkml.views.LookAt, |
| 117 |
longitude=st.floats( |
| 118 |
allow_nan=False, |
| 119 |
allow_infinity=False, |
| 120 |
min_value=-180, |
| 121 |
max_value=180, |
| 122 |
).filter(lambda x: x != 0), |
| 123 |
latitude=st.floats( |
| 124 |
allow_nan=False, |
| 125 |
allow_infinity=False, |
| 126 |
min_value=-90, |
| 127 |
max_value=90, |
| 128 |
).filter(lambda x: x != 0), |
| 129 |
altitude=st.floats(allow_nan=False, allow_infinity=False).filter( |
| 130 |
lambda x: x != 0, |
| 131 |
), |
| 132 |
), |
| 133 |
), |
| 134 |
times=st.one_of( |
| 135 |
st.builds( |
| 136 |
fastkml.TimeStamp, |
| 137 |
timestamp=kml_datetimes(), |
| 138 |
), |
| 139 |
st.builds( |
| 140 |
fastkml.TimeSpan, |
| 141 |
begin=kml_datetimes(), |
| 142 |
end=kml_datetimes(), |
| 143 |
), |
| 144 |
), |
| 145 |
region=st.one_of( |
| 146 |
st.none(), |
| 147 |
st.builds( |
| 148 |
fastkml.views.Region, |
| 149 |
lat_lon_alt_box=lat_lon_alt_boxes(), |
| 150 |
lod=lods(), |
| 151 |
), |
| 152 |
), |
| 153 |
) |
| 154 |
def test_fuzz_placemark_view_times( |
| 155 |
self, |
| 156 |
view: Union[fastkml.Camera, fastkml.LookAt, None], |
| 157 |
times: Union[fastkml.TimeSpan, fastkml.TimeStamp, None], |
| 158 |
region: Optional[fastkml.views.Region], |
| 159 |
) -> None: |
| 160 |
placemark = fastkml.Placemark( |
| 161 |
view=view, |
| 162 |
times=times, |
| 163 |
region=region, |
| 164 |
) |
| 165 |
|
| 166 |
assert_repr_roundtrip(placemark) |
| 167 |
assert_str_roundtrip(placemark) |
| 168 |
assert_str_roundtrip_terse(placemark) |
| 169 |
assert_str_roundtrip_verbose(placemark) |
| 170 |
|
| 171 |
@given( |
| 172 |
address=st.one_of( |
| 173 |
st.none(), |
| 174 |
xml_text(min_size=1).filter(lambda x: x.strip() != ""), |
| 175 |
), |
| 176 |
phone_number=st.one_of( |
| 177 |
st.none(), |
| 178 |
xml_text(min_size=1).filter(lambda x: x.strip() != ""), |
| 179 |
), |
| 180 |
snippet=st.one_of( |
| 181 |
st.none(), |
| 182 |
st.builds( |
| 183 |
fastkml.features.Snippet, |
| 184 |
text=xml_text(min_size=1, max_size=256).filter( |
| 185 |
lambda x: x.strip() != "", |
| 186 |
), |
| 187 |
max_lines=st.integers(min_value=1, max_value=20), |
| 188 |
), |
| 189 |
), |
| 190 |
description=st.one_of(st.none(), xml_text()), |
| 191 |
) |
| 192 |
def test_fuzz_placemark_str( |
| 193 |
self, |
| 194 |
address: Optional[str], |
| 195 |
phone_number: Optional[str], |
| 196 |
snippet: Optional[fastkml.features.Snippet], |
| 197 |
description: Optional[str], |
| 198 |
) -> None: |
| 199 |
placemark = fastkml.Placemark( |
| 200 |
address=address, |
| 201 |
phone_number=phone_number, |
| 202 |
snippet=snippet, |
| 203 |
description=description, |
| 204 |
) |
| 205 |
|
| 206 |
assert_repr_roundtrip(placemark) |
| 207 |
assert_str_roundtrip(placemark) |
| 208 |
assert_str_roundtrip_terse(placemark) |
| 209 |
assert_str_roundtrip_verbose(placemark) |
| 210 |
|
| 211 |
@given( |
| 212 |
id=st.one_of(st.none(), nc_name()), |
| 213 |
target_id=st.one_of(st.none(), nc_name()), |
| 214 |
name=st.one_of(st.none(), xml_text()), |
| 215 |
visibility=st.one_of(st.none(), st.booleans()), |
| 216 |
isopen=st.one_of(st.none(), st.booleans()), |
| 217 |
atom_link=st.one_of( |
| 218 |
st.none(), |
| 219 |
st.builds( |
| 220 |
fastkml.atom.Link, |
| 221 |
href=urls(), |
| 222 |
), |
| 223 |
), |
| 224 |
atom_author=st.one_of( |
| 225 |
st.none(), |
| 226 |
st.builds( |
| 227 |
fastkml.atom.Author, |
| 228 |
name=xml_text(min_size=1, max_size=256).filter( |
| 229 |
lambda x: x.strip() != "", |
| 230 |
), |
| 231 |
), |
| 232 |
), |
| 233 |
) |
| 234 |
def test_fuzz_placemark_atom( |
| 235 |
self, |
| 236 |
id: Optional[str], |
| 237 |
target_id: Optional[str], |
| 238 |
name: Optional[str], |
| 239 |
visibility: Optional[bool], |
| 240 |
isopen: Optional[bool], |
| 241 |
atom_link: Optional[fastkml.atom.Link], |
| 242 |
atom_author: Optional[fastkml.atom.Author], |
| 243 |
) -> None: |
| 244 |
placemark = fastkml.Placemark( |
| 245 |
id=id, |
| 246 |
target_id=target_id, |
| 247 |
name=name, |
| 248 |
visibility=visibility, |
| 249 |
isopen=isopen, |
| 250 |
atom_link=atom_link, |
| 251 |
atom_author=atom_author, |
| 252 |
) |
| 253 |
|
| 254 |
assert_repr_roundtrip(placemark) |
| 255 |
assert_str_roundtrip(placemark) |
| 256 |
assert_str_roundtrip_terse(placemark) |
| 257 |
assert_str_roundtrip_verbose(placemark) |
| 258 |
|
| 259 |
@given( |
| 260 |
kml_geometry=st.one_of( |
| 261 |
st.builds( |
| 262 |
fastkml.gx.Track, |
| 263 |
altitude_mode=st.sampled_from(fastkml.enums.AltitudeMode), |
| 264 |
track_items=st.lists( |
| 265 |
track_items(), |
| 266 |
min_size=1, |
| 267 |
max_size=5, |
| 268 |
), |
| 269 |
), |
| 270 |
st.builds( |
| 271 |
fastkml.gx.MultiTrack, |
| 272 |
altitude_mode=st.one_of( |
| 273 |
st.sampled_from(fastkml.enums.AltitudeMode), |
| 274 |
), |
| 275 |
tracks=st.lists( |
| 276 |
st.builds( |
| 277 |
fastkml.gx.Track, |
| 278 |
track_items=st.lists( |
| 279 |
track_items(), |
| 280 |
min_size=1, |
| 281 |
max_size=3, |
| 282 |
), |
| 283 |
), |
| 284 |
min_size=1, |
| 285 |
max_size=3, |
| 286 |
), |
| 287 |
), |
| 288 |
), |
| 289 |
) |
| 290 |
def test_fuzz_placemark_gx_track( |
| 291 |
self, |
| 292 |
kml_geometry: Union[ |
| 293 |
fastkml.gx.Track, |
| 294 |
fastkml.gx.MultiTrack, |
| 295 |
], |
| 296 |
) -> None: |
| 297 |
placemark = fastkml.Placemark( |
| 298 |
kml_geometry=kml_geometry, |
| 299 |
) |
| 300 |
|
| 301 |
assert_repr_roundtrip(placemark) |
| 302 |
assert_str_roundtrip(placemark) |
| 303 |
assert_str_roundtrip_terse(placemark) |
| 304 |
assert_str_roundtrip_verbose(placemark) |
| 305 |
|
| 306 |
@given( |
| 307 |
extended_data=st.builds( |
| 308 |
fastkml.ExtendedData, |
| 309 |
elements=st.tuples( |
| 310 |
st.builds( |
| 311 |
fastkml.data.Data, |
| 312 |
name=xml_text().filter(lambda x: x.strip() != ""), |
| 313 |
value=xml_text().filter(lambda x: x.strip() != ""), |
| 314 |
display_name=st.one_of(st.none(), xml_text()), |
| 315 |
), |
| 316 |
st.builds( |
| 317 |
fastkml.SchemaData, |
| 318 |
schema_url=urls(), |
| 319 |
data=st.lists( |
| 320 |
st.builds( |
| 321 |
fastkml.data.SimpleData, |
| 322 |
name=xml_text().filter(lambda x: x.strip() != ""), |
| 323 |
value=xml_text().filter(lambda x: x.strip() != ""), |
| 324 |
), |
| 325 |
min_size=1, |
| 326 |
max_size=3, |
| 327 |
), |
| 328 |
), |
| 329 |
), |
| 330 |
), |
| 331 |
) |
| 332 |
def test_fuzz_placemark_extended_data( |
| 333 |
self, |
| 334 |
extended_data: Optional[fastkml.ExtendedData], |
| 335 |
) -> None: |
| 336 |
placemark = fastkml.Placemark( |
| 337 |
extended_data=extended_data, |
| 338 |
) |
| 339 |
|
| 340 |
assert_repr_roundtrip(placemark) |
| 341 |
assert_str_roundtrip(placemark) |
| 342 |
assert_str_roundtrip_terse(placemark) |
| 343 |
assert_str_roundtrip_verbose(placemark) |
| 344 |
|
| 345 |
@given( |
| 346 |
style_url=st.one_of( |
| 347 |
st.none(), |
| 348 |
st.builds( |
| 349 |
fastkml.StyleUrl, |
| 350 |
url=urls(), |
| 351 |
), |
| 352 |
), |
| 353 |
styles=st.one_of( |
| 354 |
st.none(), |
| 355 |
st.tuples( |
| 356 |
st.builds( |
| 357 |
fastkml.Style, |
| 358 |
styles=st.lists( |
| 359 |
styles(), |
| 360 |
min_size=1, |
| 361 |
max_size=1, |
| 362 |
), |
| 363 |
), |
| 364 |
st.builds( |
| 365 |
fastkml.styles.StyleMap, |
| 366 |
pairs=st.tuples( |
| 367 |
st.builds( |
| 368 |
fastkml.styles.Pair, |
| 369 |
key=st.just(fastkml.enums.PairKey.normal), |
| 370 |
style=st.one_of( |
| 371 |
st.builds( |
| 372 |
fastkml.StyleUrl, |
| 373 |
url=urls(), |
| 374 |
), |
| 375 |
st.builds( |
| 376 |
fastkml.Style, |
| 377 |
styles=st.lists( |
| 378 |
styles(), |
| 379 |
min_size=1, |
| 380 |
max_size=1, |
| 381 |
), |
| 382 |
), |
| 383 |
), |
| 384 |
), |
| 385 |
st.builds( |
| 386 |
fastkml.styles.Pair, |
| 387 |
key=st.just(fastkml.enums.PairKey.highlight), |
| 388 |
style=st.one_of( |
| 389 |
st.builds( |
| 390 |
fastkml.StyleUrl, |
| 391 |
url=urls(), |
| 392 |
), |
| 393 |
st.builds( |
| 394 |
fastkml.Style, |
| 395 |
styles=st.lists( |
| 396 |
styles(), |
| 397 |
min_size=1, |
| 398 |
max_size=1, |
| 399 |
), |
| 400 |
), |
| 401 |
), |
| 402 |
), |
| 403 |
), |
| 404 |
), |
| 405 |
), |
| 406 |
), |
| 407 |
) |
| 408 |
def test_fuzz_placemark_styles( |
| 409 |
self, |
| 410 |
style_url: Optional[fastkml.StyleUrl], |
| 411 |
styles: Optional[Iterable[Union[fastkml.Style, fastkml.StyleMap]]], |
| 412 |
) -> None: |
| 413 |
placemark = fastkml.Placemark( |
| 414 |
style_url=style_url, |
| 415 |
styles=styles, |
| 416 |
) |
| 417 |
|
| 418 |
assert_repr_roundtrip(placemark) |
| 419 |
assert_str_roundtrip(placemark) |
| 420 |
assert_str_roundtrip_terse(placemark) |
| 421 |
assert_str_roundtrip_verbose(placemark) |
| 422 |
|
| 423 |
@given( |
| 424 |
model=st.builds( |
| 425 |
fastkml.model.Model, |
| 426 |
altitude_mode=st.sampled_from(fastkml.enums.AltitudeMode), |
| 427 |
location=st.builds( |
| 428 |
fastkml.model.Location, |
| 429 |
latitude=st.floats( |
| 430 |
allow_nan=False, |
| 431 |
allow_infinity=False, |
| 432 |
min_value=-90, |
| 433 |
max_value=90, |
| 434 |
).filter(lambda x: x != 0), |
| 435 |
longitude=st.floats( |
| 436 |
allow_nan=False, |
| 437 |
allow_infinity=False, |
| 438 |
min_value=-180, |
| 439 |
max_value=180, |
| 440 |
).filter(lambda x: x != 0), |
| 441 |
altitude=st.floats(allow_nan=False, allow_infinity=False).filter( |
| 442 |
lambda x: x != 0, |
| 443 |
), |
| 444 |
), |
| 445 |
link=st.builds(fastkml.Link, href=urls()), |
| 446 |
), |
| 447 |
) |
| 448 |
def test_fuzz_placemark_model( |
| 449 |
self, |
| 450 |
model: fastkml.model.Model, |
| 451 |
) -> None: |
| 452 |
placemark = fastkml.Placemark( |
| 453 |
kml_geometry=model, |
| 454 |
) |
| 455 |
|
| 456 |
assert_repr_roundtrip(placemark) |
| 457 |
assert_str_roundtrip(placemark) |
| 458 |
assert_str_roundtrip_terse(placemark) |
| 459 |
assert_str_roundtrip_verbose(placemark) |
| 460 |
|
| 461 |
@given( |
| 462 |
refresh_visibility=st.one_of(st.none(), st.booleans()), |
| 463 |
fly_to_view=st.one_of(st.none(), st.booleans()), |
| 464 |
link=st.one_of( |
| 465 |
st.none(), |
| 466 |
st.builds( |
| 467 |
fastkml.links.Link, |
| 468 |
href=urls(), |
| 469 |
), |
| 470 |
), |
| 471 |
) |
| 472 |
def test_network_link( |
| 473 |
self, |
| 474 |
refresh_visibility: Optional[bool], |
| 475 |
fly_to_view: Optional[bool], |
| 476 |
link: Optional[fastkml.links.Link], |
| 477 |
) -> None: |
| 478 |
"""Test NetworkLink object with optional parameters.""" |
| 479 |
network_link = fastkml.features.NetworkLink( |
| 480 |
refresh_visibility=refresh_visibility, |
| 481 |
fly_to_view=fly_to_view, |
| 482 |
link=link, |
| 483 |
) |
| 484 |
|
| 485 |
assert network_link.refresh_visibility == refresh_visibility |
| 486 |
assert network_link.fly_to_view == fly_to_view |
| 487 |
assert network_link.link == link |
| 488 |
|
| 489 |
assert_repr_roundtrip(network_link) |
| 490 |
assert_str_roundtrip(network_link) |
| 491 |
assert_str_roundtrip_terse(network_link) |
| 492 |
assert_str_roundtrip_verbose(network_link) |