Debian Codegraph @

tests/hypothesis/model_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 """Hypothesis tests for the fastkml.model module."""
17
18 from collections.abc import Iterable
19 from typing import Optional
20
21 from hypothesis import given
22 from hypothesis import strategies as st
23 from hypothesis.provisional import urls
24
25 import fastkml
26 import fastkml.enums
27 import fastkml.links
28 import fastkml.model
29 from tests.base import Lxml
30 from tests.hypothesis.common import assert_repr_roundtrip
31 from tests.hypothesis.common import assert_str_roundtrip
32 from tests.hypothesis.common import assert_str_roundtrip_terse
33 from tests.hypothesis.common import assert_str_roundtrip_verbose
34 from tests.hypothesis.strategies import nc_name
35
36
37 class TestLxml(Lxml):
38 @given(
39 id=st.one_of(st.none(), nc_name()),
40 target_id=st.one_of(st.none(), nc_name()),
41 altitude=st.one_of(
42 st.none(),
43 st.just(0.0),
44 st.floats(allow_nan=False, allow_infinity=False).filter(lambda x: x != 0),
45 ),
46 latitude=st.one_of(
47 st.none(),
48 st.just(0.0),
49 st.floats(
50 allow_nan=False,
51 allow_infinity=False,
52 min_value=-90,
53 max_value=90,
54 ),
55 ),
56 longitude=st.one_of(
57 st.none(),
58 st.just(0.0),
59 st.floats(
60 allow_nan=False,
61 allow_infinity=False,
62 min_value=-180,
63 max_value=180,
64 ),
65 ),
66 )
67 def test_fuzz_location(
68 self,
69 id: Optional[str],
70 target_id: Optional[str],
71 altitude: Optional[float],
72 latitude: Optional[float],
73 longitude: Optional[float],
74 ) -> None:
75 location = fastkml.model.Location(
76 id=id,
77 target_id=target_id,
78 altitude=altitude,
79 latitude=latitude,
80 longitude=longitude,
81 )
82
83 assert_repr_roundtrip(location)
84 assert_str_roundtrip(location)
85 assert_str_roundtrip_terse(location)
86 assert_str_roundtrip_verbose(location)
87
88 @given(
89 id=st.one_of(st.none(), nc_name()),
90 target_id=st.one_of(st.none(), nc_name()),
91 heading=st.one_of(
92 st.none(),
93 st.just(0.0),
94 st.floats(
95 allow_nan=False,
96 allow_infinity=False,
97 min_value=-360,
98 max_value=360,
99 ).filter(lambda x: x != 0),
100 ),
101 tilt=st.one_of(
102 st.none(),
103 st.just(0.0),
104 st.floats(
105 allow_nan=False,
106 allow_infinity=False,
107 min_value=0,
108 max_value=180,
109 ).filter(lambda x: x != 0),
110 ),
111 roll=st.one_of(
112 st.none(),
113 st.just(0.0),
114 st.floats(
115 allow_nan=False,
116 allow_infinity=False,
117 min_value=-180,
118 max_value=180,
119 ).filter(lambda x: x != 0),
120 ),
121 )
122 def test_fuzz_orientation(
123 self,
124 id: Optional[str],
125 target_id: Optional[str],
126 heading: Optional[float],
127 tilt: Optional[float],
128 roll: Optional[float],
129 ) -> None:
130 orientation = fastkml.model.Orientation(
131 id=id,
132 target_id=target_id,
133 heading=heading,
134 tilt=tilt,
135 roll=roll,
136 )
137
138 assert_repr_roundtrip(orientation)
139 assert_str_roundtrip(orientation)
140 assert_str_roundtrip_terse(orientation)
141 assert_str_roundtrip_verbose(orientation)
142
143 @given(
144 id=st.one_of(st.none(), nc_name()),
145 target_id=st.one_of(st.none(), nc_name()),
146 x=st.one_of(st.none(), st.floats(allow_nan=False, allow_infinity=False)),
147 y=st.one_of(st.none(), st.floats(allow_nan=False, allow_infinity=False)),
148 z=st.one_of(st.none(), st.floats(allow_nan=False, allow_infinity=False)),
149 )
150 def test_fuzz_scale(
151 self,
152 id: Optional[str],
153 target_id: Optional[str],
154 x: Optional[float],
155 y: Optional[float],
156 z: Optional[float],
157 ) -> None:
158 scale = fastkml.model.Scale(id=id, target_id=target_id, x=x, y=y, z=z)
159
160 assert_repr_roundtrip(scale)
161 assert_str_roundtrip(scale)
162 assert_str_roundtrip_terse(scale)
163 assert_str_roundtrip_verbose(scale)
164
165 @given(
166 id=st.one_of(st.none(), nc_name()),
167 target_id=st.one_of(st.none(), nc_name()),
168 target_href=st.one_of(st.none(), urls()),
169 source_href=st.one_of(st.none(), urls()),
170 )
171 def test_fuzz_alias(
172 self,
173 id: Optional[str],
174 target_id: Optional[str],
175 target_href: Optional[str],
176 source_href: Optional[str],
177 ) -> None:
178 alias = fastkml.model.Alias(
179 id=id,
180 target_id=target_id,
181 target_href=target_href,
182 source_href=source_href,
183 )
184
185 assert_repr_roundtrip(alias)
186 assert_str_roundtrip(alias)
187 assert_str_roundtrip_terse(alias)
188 assert_str_roundtrip_verbose(alias)
189
190 @given(
191 id=st.one_of(st.none(), nc_name()),
192 target_id=st.one_of(st.none(), nc_name()),
193 aliases=st.one_of(
194 st.none(),
195 st.lists(
196 st.builds(
197 fastkml.model.Alias,
198 source_href=urls(),
199 target_href=urls(),
200 ),
201 ),
202 ),
203 )
204 def test_fuzz_resource_map(
205 self,
206 id: Optional[str],
207 target_id: Optional[str],
208 aliases: Optional[Iterable[fastkml.model.Alias]],
209 ) -> None:
210 resource_map = fastkml.model.ResourceMap(
211 id=id,
212 target_id=target_id,
213 aliases=aliases,
214 )
215
216 assert_repr_roundtrip(resource_map)
217 assert_str_roundtrip(resource_map)
218 assert_str_roundtrip_terse(resource_map)
219 assert_str_roundtrip_verbose(resource_map)
220
221 @given(
222 id=st.one_of(st.none(), nc_name()),
223 target_id=st.one_of(st.none(), nc_name()),
224 altitude_mode=st.one_of(st.none(), st.sampled_from(fastkml.enums.AltitudeMode)),
225 location=st.one_of(
226 st.none(),
227 st.builds(
228 fastkml.model.Location,
229 altitude=st.floats(allow_nan=False, allow_infinity=False).filter(
230 lambda x: x != 0,
231 ),
232 latitude=st.floats(
233 allow_nan=False,
234 allow_infinity=False,
235 min_value=-90,
236 max_value=90,
237 ),
238 longitude=st.floats(
239 allow_nan=False,
240 allow_infinity=False,
241 min_value=-180,
242 max_value=180,
243 ),
244 ),
245 ),
246 orientation=st.one_of(
247 st.none(),
248 st.builds(
249 fastkml.model.Orientation,
250 heading=st.floats(
251 allow_nan=False,
252 allow_infinity=False,
253 min_value=-360,
254 max_value=360,
255 ).filter(lambda x: x != 0),
256 tilt=st.floats(
257 allow_nan=False,
258 allow_infinity=False,
259 min_value=0,
260 max_value=180,
261 ).filter(lambda x: x != 0),
262 roll=st.floats(
263 allow_nan=False,
264 allow_infinity=False,
265 min_value=-180,
266 max_value=180,
267 ).filter(lambda x: x != 0),
268 ),
269 ),
270 scale=st.one_of(
271 st.none(),
272 st.builds(
273 fastkml.model.Scale,
274 x=st.floats(allow_nan=False, allow_infinity=False).filter(
275 lambda x: x != 1.0,
276 ),
277 y=st.floats(allow_nan=False, allow_infinity=False).filter(
278 lambda x: x != 1.0,
279 ),
280 z=st.floats(allow_nan=False, allow_infinity=False).filter(
281 lambda x: x != 1.0,
282 ),
283 ),
284 ),
285 link=st.one_of(st.none(), st.builds(fastkml.Link, href=urls())),
286 resource_map=st.one_of(
287 st.none(),
288 st.builds(
289 fastkml.model.ResourceMap,
290 aliases=st.lists(
291 st.builds(
292 fastkml.model.Alias,
293 source_href=urls(),
294 target_href=urls(),
295 ),
296 min_size=1,
297 ),
298 ),
299 ),
300 )
301 def test_fuzz_model(
302 self,
303 id: Optional[str],
304 target_id: Optional[str],
305 altitude_mode: Optional[fastkml.enums.AltitudeMode],
306 location: Optional[fastkml.model.Location],
307 orientation: Optional[fastkml.model.Orientation],
308 scale: Optional[fastkml.model.Scale],
309 link: Optional[fastkml.Link],
310 resource_map: Optional[fastkml.model.ResourceMap],
311 ) -> None:
312 model = fastkml.model.Model(
313 id=id,
314 target_id=target_id,
315 altitude_mode=altitude_mode,
316 location=location,
317 orientation=orientation,
318 scale=scale,
319 link=link,
320 resource_map=resource_map,
321 )
322
323 assert_repr_roundtrip(model)
324 assert_str_roundtrip(model)
325 assert_str_roundtrip_terse(model)
326 assert_str_roundtrip_verbose(model)