| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Custom hypothesis strategies for testing.""" |
| 18 |
|
| 19 |
import datetime |
| 20 |
import re |
| 21 |
import string |
| 22 |
from functools import partial |
| 23 |
from typing import Final |
| 24 |
from urllib.parse import urlencode |
| 25 |
|
| 26 |
from hypothesis import strategies as st |
| 27 |
from hypothesis.extra.dateutil import timezones |
| 28 |
from pygeoif.hypothesis import strategies as geo_st |
| 29 |
|
| 30 |
import fastkml.enums |
| 31 |
import fastkml.gx |
| 32 |
import fastkml.links |
| 33 |
import fastkml.styles |
| 34 |
from fastkml.times import KmlDateTime |
| 35 |
from fastkml.views import LatLonAltBox |
| 36 |
from fastkml.views import Lod |
| 37 |
|
| 38 |
ID_TEXT: Final = string.ascii_letters + string.digits + ".-_" |
| 39 |
|
| 40 |
nc_name = partial( |
| 41 |
st.from_regex, |
| 42 |
regex=re.compile(r"^[A-Za-z_][\w.-]*$"), |
| 43 |
alphabet=ID_TEXT, |
| 44 |
fullmatch=True, |
| 45 |
) |
| 46 |
|
| 47 |
href_langs = partial( |
| 48 |
st.from_regex, |
| 49 |
regex=re.compile(r"^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})?$"), |
| 50 |
alphabet=f"{string.ascii_letters}-{string.digits}", |
| 51 |
fullmatch=True, |
| 52 |
) |
| 53 |
|
| 54 |
media_types = partial( |
| 55 |
st.from_regex, |
| 56 |
regex=re.compile(r"^[a-zA-Z0-9-]+/[a-zA-Z0-9-]+$"), |
| 57 |
alphabet=f"{string.ascii_letters}/-{string.digits}", |
| 58 |
fullmatch=True, |
| 59 |
) |
| 60 |
|
| 61 |
xml_text = partial( |
| 62 |
st.text, |
| 63 |
alphabet=st.characters(min_codepoint=1, blacklist_categories=("Cc", "Cs")), |
| 64 |
) |
| 65 |
|
| 66 |
uri_text = partial( |
| 67 |
st.from_regex, |
| 68 |
regex=re.compile(r"^[a-zA-Z][a-zA-Z0-9+.-]*://([^/?#]*)([^#]*)(#.*)?$"), |
| 69 |
alphabet=f"{string.ascii_letters}{string.digits}+-._~:/?#[]@!$&'()*+,;=%", |
| 70 |
fullmatch=True, |
| 71 |
) |
| 72 |
|
| 73 |
kml_datetimes = partial( |
| 74 |
st.builds, |
| 75 |
KmlDateTime, |
| 76 |
dt=st.one_of( |
| 77 |
st.dates( |
| 78 |
min_value=datetime.date(2000, 1, 1), |
| 79 |
max_value=datetime.date(2050, 1, 1), |
| 80 |
), |
| 81 |
st.datetimes( |
| 82 |
allow_imaginary=False, |
| 83 |
timezones=timezones(), |
| 84 |
min_value=datetime.datetime(2000, 1, 1), |
| 85 |
max_value=datetime.datetime(2050, 1, 1), |
| 86 |
), |
| 87 |
), |
| 88 |
resolution=st.one_of( |
| 89 |
st.none(), |
| 90 |
st.one_of(st.none(), st.sampled_from(fastkml.enums.DateTimeResolution)), |
| 91 |
), |
| 92 |
) |
| 93 |
|
| 94 |
geometries = partial( |
| 95 |
st.one_of, |
| 96 |
( |
| 97 |
geo_st.points(srs=geo_st.epsg4326), |
| 98 |
geo_st.line_strings(srs=geo_st.epsg4326), |
| 99 |
geo_st.linear_rings(srs=geo_st.epsg4326), |
| 100 |
geo_st.polygons(srs=geo_st.epsg4326), |
| 101 |
geo_st.multi_points(srs=geo_st.epsg4326), |
| 102 |
geo_st.multi_line_strings(srs=geo_st.epsg4326), |
| 103 |
geo_st.multi_polygons(srs=geo_st.epsg4326), |
| 104 |
), |
| 105 |
) |
| 106 |
lods = partial( |
| 107 |
st.builds, |
| 108 |
Lod, |
| 109 |
min_lod_pixels=st.integers().filter(lambda x: x != 256), |
| 110 |
max_lod_pixels=st.integers(), |
| 111 |
min_fade_extent=st.integers(), |
| 112 |
max_fade_extent=st.integers(), |
| 113 |
) |
| 114 |
|
| 115 |
lat_lon_alt_boxes = partial( |
| 116 |
st.builds, |
| 117 |
LatLonAltBox, |
| 118 |
north=st.floats(allow_nan=False, allow_infinity=False, min_value=0, max_value=90), |
| 119 |
south=st.floats(allow_nan=False, allow_infinity=False, min_value=0, max_value=90), |
| 120 |
east=st.floats(allow_nan=False, allow_infinity=False, min_value=0, max_value=180), |
| 121 |
west=st.floats(allow_nan=False, allow_infinity=False, min_value=0, max_value=180), |
| 122 |
min_altitude=st.floats(allow_nan=False, allow_infinity=False).filter( |
| 123 |
lambda x: x != 0, |
| 124 |
), |
| 125 |
max_altitude=st.floats(allow_nan=False, allow_infinity=False).filter( |
| 126 |
lambda x: x != 0, |
| 127 |
), |
| 128 |
altitude_mode=st.sampled_from(fastkml.enums.AltitudeMode), |
| 129 |
) |
| 130 |
|
| 131 |
kml_colors = partial( |
| 132 |
st.text, |
| 133 |
alphabet=string.hexdigits, |
| 134 |
min_size=8, |
| 135 |
max_size=8, |
| 136 |
) |
| 137 |
|
| 138 |
styles = partial( |
| 139 |
st.one_of, |
| 140 |
st.builds( |
| 141 |
fastkml.styles.LabelStyle, |
| 142 |
color=kml_colors().filter(lambda x: x != "ffffffff"), |
| 143 |
color_mode=st.sampled_from(fastkml.enums.ColorMode), |
| 144 |
scale=st.floats(allow_nan=False, allow_infinity=False), |
| 145 |
), |
| 146 |
st.builds( |
| 147 |
fastkml.styles.LineStyle, |
| 148 |
color=kml_colors().filter(lambda x: x != "ffffffff"), |
| 149 |
color_mode=st.sampled_from(fastkml.enums.ColorMode), |
| 150 |
width=st.floats(allow_nan=False, allow_infinity=False, min_value=0), |
| 151 |
), |
| 152 |
st.builds( |
| 153 |
fastkml.styles.PolyStyle, |
| 154 |
color=kml_colors().filter(lambda x: x != "ffffffff"), |
| 155 |
color_mode=st.sampled_from(fastkml.enums.ColorMode), |
| 156 |
fill=st.booleans(), |
| 157 |
outline=st.booleans(), |
| 158 |
), |
| 159 |
st.builds( |
| 160 |
fastkml.styles.BalloonStyle, |
| 161 |
bg_color=kml_colors(), |
| 162 |
text_color=kml_colors(), |
| 163 |
text=xml_text(min_size=1, max_size=256).filter(lambda x: x.strip() != ""), |
| 164 |
display_mode=st.sampled_from(fastkml.enums.DisplayMode), |
| 165 |
), |
| 166 |
) |
| 167 |
|
| 168 |
track_items = partial( |
| 169 |
st.builds, |
| 170 |
fastkml.gx.TrackItem, |
| 171 |
angle=st.builds( |
| 172 |
fastkml.gx.Angle, |
| 173 |
heading=st.floats(allow_nan=False, allow_infinity=False), |
| 174 |
roll=st.floats(allow_nan=False, allow_infinity=False), |
| 175 |
tilt=st.floats(allow_nan=False, allow_infinity=False), |
| 176 |
), |
| 177 |
coord=geo_st.points(srs=geo_st.epsg4326), |
| 178 |
when=kml_datetimes(), |
| 179 |
) |
| 180 |
|
| 181 |
xy = partial( |
| 182 |
st.builds, |
| 183 |
x=st.floats(allow_nan=False, allow_infinity=False), |
| 184 |
y=st.floats(allow_nan=False, allow_infinity=False), |
| 185 |
x_units=st.sampled_from(fastkml.enums.Units), |
| 186 |
y_units=st.sampled_from(fastkml.enums.Units), |
| 187 |
) |
| 188 |
|
| 189 |
|
| 190 |
@st.composite |
| 191 |
def query_strings(draw: st.DrawFn) -> str: |
| 192 |
params = draw( |
| 193 |
st.dictionaries( |
| 194 |
keys=st.text(alphabet=string.ascii_letters, min_size=1), |
| 195 |
values=st.text(alphabet=string.printable), |
| 196 |
), |
| 197 |
) |
| 198 |
return urlencode(params) |