Debian Codegraph @

tests/hypothesis/views_test.py

raw symbols SCIP
1 # Copyright (C) 2024 Christian Ledermann
2 #
3 # This library is free software; you can redistribute it and/or modify it under
4 # the terms of the GNU Lesser General Public License as published by the Free
5 # Software Foundation; either version 2.1 of the License, or (at your option)
6 # any later version.
7 #
8 # This library is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Lesser General Public License
14 # along with this library; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 """Property-based tests for the views module."""
17
18 from typing import Optional
19
20 from hypothesis import given
21 from hypothesis import strategies as st
22
23 import fastkml
24 import fastkml.enums
25 import fastkml.views
26 from tests.base import Lxml
27 from tests.hypothesis.common import assert_repr_roundtrip
28 from tests.hypothesis.common import assert_str_roundtrip
29 from tests.hypothesis.common import assert_str_roundtrip_terse
30 from tests.hypothesis.common import assert_str_roundtrip_verbose
31 from tests.hypothesis.strategies import lat_lon_alt_boxes
32 from tests.hypothesis.strategies import lods
33 from tests.hypothesis.strategies import nc_name
34
35 common_view = {
36 "id": st.one_of(st.none(), nc_name()),
37 "target_id": st.one_of(st.none(), nc_name()),
38 "longitude": st.one_of(
39 st.none(),
40 st.floats(
41 allow_nan=False,
42 allow_infinity=False,
43 min_value=-180,
44 max_value=180,
45 ).filter(lambda x: x != 0),
46 ),
47 "latitude": st.one_of(
48 st.none(),
49 st.floats(
50 allow_nan=False,
51 allow_infinity=False,
52 min_value=-90,
53 max_value=90,
54 ).filter(lambda x: x != 0),
55 ),
56 "altitude": st.one_of(
57 st.none(),
58 st.floats(allow_nan=False, allow_infinity=False).filter(lambda x: x != 0),
59 ),
60 "heading": st.one_of(
61 st.none(),
62 st.floats(allow_nan=False, allow_infinity=False, min_value=0, max_value=360),
63 ),
64 "tilt": st.one_of(
65 st.none(),
66 st.floats(allow_nan=False, allow_infinity=False, min_value=0, max_value=180),
67 ),
68 "altitude_mode": st.one_of(st.none(), st.sampled_from(fastkml.enums.AltitudeMode)),
69 }
70
71
72 class TestLxml(Lxml):
73 @given(
74 id=st.one_of(st.none(), nc_name()),
75 target_id=st.one_of(st.none(), nc_name()),
76 min_lod_pixels=st.one_of(st.none(), st.integers()),
77 max_lod_pixels=st.one_of(st.none(), st.integers()),
78 min_fade_extent=st.one_of(st.none(), st.integers()),
79 max_fade_extent=st.one_of(st.none(), st.integers()),
80 )
81 def test_fuzz_lod(
82 self,
83 id: Optional[str],
84 target_id: Optional[str],
85 min_lod_pixels: Optional[int],
86 max_lod_pixels: Optional[int],
87 min_fade_extent: Optional[int],
88 max_fade_extent: Optional[int],
89 ) -> None:
90 lod = fastkml.views.Lod(
91 id=id,
92 target_id=target_id,
93 min_lod_pixels=min_lod_pixels,
94 max_lod_pixels=max_lod_pixels,
95 min_fade_extent=min_fade_extent,
96 max_fade_extent=max_fade_extent,
97 )
98
99 assert_repr_roundtrip(lod)
100 assert_str_roundtrip(lod)
101 assert_str_roundtrip_terse(lod)
102 assert_str_roundtrip_verbose(lod)
103
104 @given(
105 id=st.one_of(st.none(), nc_name()),
106 target_id=st.one_of(st.none(), nc_name()),
107 north=st.one_of(
108 st.none(),
109 st.floats(allow_nan=False, allow_infinity=False, min_value=0, max_value=90),
110 ),
111 south=st.one_of(
112 st.none(),
113 st.floats(allow_nan=False, allow_infinity=False, min_value=0, max_value=90),
114 ),
115 east=st.one_of(
116 st.none(),
117 st.floats(
118 allow_nan=False,
119 allow_infinity=False,
120 min_value=0,
121 max_value=180,
122 ),
123 ),
124 west=st.one_of(
125 st.none(),
126 st.floats(
127 allow_nan=False,
128 allow_infinity=False,
129 min_value=0,
130 max_value=180,
131 ),
132 ),
133 min_altitude=st.one_of(
134 st.none(),
135 st.floats(allow_nan=False, allow_infinity=False).filter(lambda x: x != 0),
136 ),
137 max_altitude=st.one_of(
138 st.none(),
139 st.floats(allow_nan=False, allow_infinity=False).filter(lambda x: x != 0),
140 ),
141 altitude_mode=st.one_of(st.none(), st.sampled_from(fastkml.enums.AltitudeMode)),
142 )
143 def test_fuzz_lat_lon_alt_box(
144 self,
145 id: Optional[str],
146 target_id: Optional[str],
147 north: Optional[float],
148 south: Optional[float],
149 east: Optional[float],
150 west: Optional[float],
151 min_altitude: Optional[float],
152 max_altitude: Optional[float],
153 altitude_mode: Optional[fastkml.enums.AltitudeMode],
154 ) -> None:
155 lat_lon_alt_box = fastkml.views.LatLonAltBox(
156 id=id,
157 target_id=target_id,
158 north=north,
159 south=south,
160 east=east,
161 west=west,
162 min_altitude=min_altitude,
163 max_altitude=max_altitude,
164 altitude_mode=altitude_mode,
165 )
166
167 assert_repr_roundtrip(lat_lon_alt_box)
168 assert_str_roundtrip(lat_lon_alt_box)
169 assert_str_roundtrip_terse(lat_lon_alt_box)
170 assert_str_roundtrip_verbose(lat_lon_alt_box)
171
172 @given(
173 id=st.one_of(st.none(), nc_name()),
174 target_id=st.one_of(st.none(), nc_name()),
175 lat_lon_alt_box=st.one_of(st.none(), lat_lon_alt_boxes()),
176 lod=st.one_of(st.none(), lods()),
177 )
178 def test_fuzz_region(
179 self,
180 id: Optional[str],
181 target_id: Optional[str],
182 lat_lon_alt_box: Optional[fastkml.views.LatLonAltBox],
183 lod: Optional[fastkml.views.Lod],
184 ) -> None:
185 region = fastkml.views.Region(
186 id=id,
187 target_id=target_id,
188 lat_lon_alt_box=lat_lon_alt_box,
189 lod=lod,
190 )
191
192 assert_repr_roundtrip(region)
193 assert_str_roundtrip(region)
194 assert_str_roundtrip_terse(region)
195 assert_str_roundtrip_verbose(region)
196
197 @given(
198 **common_view,
199 roll=st.one_of(
200 st.none(),
201 st.floats(
202 allow_nan=False,
203 allow_infinity=False,
204 min_value=-180,
205 max_value=180,
206 ).filter(lambda x: x != 0),
207 ),
208 )
209 def test_fuzz_camera(
210 self,
211 id: Optional[str],
212 target_id: Optional[str],
213 longitude: Optional[float],
214 latitude: Optional[float],
215 altitude: Optional[float],
216 heading: Optional[float],
217 tilt: Optional[float],
218 altitude_mode: Optional[fastkml.enums.AltitudeMode],
219 roll: Optional[float],
220 ) -> None:
221 camera = fastkml.Camera(
222 id=id,
223 target_id=target_id,
224 longitude=longitude,
225 latitude=latitude,
226 altitude=altitude,
227 heading=heading,
228 tilt=tilt,
229 roll=roll,
230 altitude_mode=altitude_mode,
231 )
232
233 assert_repr_roundtrip(camera)
234 assert_str_roundtrip(camera)
235 assert_str_roundtrip_terse(camera)
236 assert_str_roundtrip_verbose(camera)
237
238 @given(
239 **common_view,
240 range=st.one_of(
241 st.none(),
242 st.floats(allow_nan=False, allow_infinity=False).filter(lambda x: x != 0),
243 ),
244 )
245 def test_fuzz_look_at(
246 self,
247 id: Optional[str],
248 target_id: Optional[str],
249 longitude: Optional[float],
250 latitude: Optional[float],
251 altitude: Optional[float],
252 heading: Optional[float],
253 tilt: Optional[float],
254 altitude_mode: Optional[fastkml.enums.AltitudeMode],
255 range: Optional[float],
256 ) -> None:
257 look_at = fastkml.LookAt(
258 id=id,
259 target_id=target_id,
260 longitude=longitude,
261 latitude=latitude,
262 altitude=altitude,
263 heading=heading,
264 tilt=tilt,
265 range=range,
266 altitude_mode=altitude_mode,
267 )
268
269 assert_repr_roundtrip(look_at)
270 assert_str_roundtrip(look_at)
271 assert_str_roundtrip_terse(look_at)
272 assert_str_roundtrip_verbose(look_at)