Debian Codegraph @

tests/hypothesis/data_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 collections.abc import Iterable
19 from functools import partial
20 from typing import Optional
21 from typing import Union
22
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.data
29 import fastkml.enums
30 import fastkml.gx.data
31 from tests.base import Lxml
32 from tests.hypothesis.common import assert_repr_roundtrip
33 from tests.hypothesis.common import assert_str_roundtrip
34 from tests.hypothesis.common import assert_str_roundtrip_terse
35 from tests.hypothesis.common import assert_str_roundtrip_verbose
36 from tests.hypothesis.strategies import nc_name
37 from tests.hypothesis.strategies import xml_text
38
39 simple_fields = partial(
40 st.builds,
41 fastkml.data.SimpleField,
42 name=xml_text().filter(lambda x: x.strip() != ""),
43 type_=st.sampled_from(fastkml.enums.DataType),
44 display_name=xml_text().filter(lambda x: x.strip() != ""),
45 )
46 simple_array_fields = partial(
47 st.builds,
48 fastkml.gx.data.SimpleArrayField,
49 name=xml_text().filter(lambda x: x.strip() != ""),
50 type_=st.sampled_from(fastkml.enums.DataType),
51 display_name=xml_text().filter(lambda x: x.strip() != ""),
52 )
53 simple_data = partial(
54 st.builds,
55 fastkml.data.SimpleData,
56 name=xml_text().filter(lambda x: x.strip() != ""),
57 value=xml_text().filter(lambda x: x.strip() != ""),
58 )
59 simple_array_data = partial(
60 st.builds,
61 fastkml.gx.data.SimpleArrayData,
62 name=xml_text().filter(lambda x: x.strip() != ""),
63 data=st.lists(xml_text().filter(lambda x: x.strip() != ""), min_size=1),
64 )
65
66
67 class TestLxml(Lxml):
68 @given(
69 name=st.one_of(st.none(), xml_text()),
70 type_=st.one_of(st.none(), st.sampled_from(fastkml.enums.DataType)),
71 display_name=st.one_of(st.none(), xml_text()),
72 )
73 def test_fuzz_simple_field(
74 self,
75 name: Optional[str],
76 type_: Optional[fastkml.enums.DataType],
77 display_name: Optional[str],
78 ) -> None:
79 simple_field = fastkml.data.SimpleField(
80 name=name,
81 type_=type_,
82 display_name=display_name,
83 )
84
85 assert_str_roundtrip(simple_field)
86 assert_repr_roundtrip(simple_field)
87 assert_str_roundtrip_terse(simple_field)
88 assert_str_roundtrip_verbose(simple_field)
89
90 @given(
91 id=nc_name(),
92 name=st.one_of(st.none(), xml_text()),
93 fields=st.one_of(st.none(), st.lists(simple_fields())),
94 array_fields=st.one_of(st.none(), st.lists(simple_array_fields())),
95 )
96 def test_fuzz_schema(
97 self,
98 id: Optional[str],
99 name: Optional[str],
100 fields: Optional[Iterable[fastkml.data.SimpleField]],
101 array_fields: Optional[Iterable[fastkml.gx.data.SimpleArrayField]],
102 ) -> None:
103 schema = fastkml.Schema(
104 id=id,
105 name=name,
106 fields=fields,
107 array_fields=array_fields,
108 )
109
110 assert_str_roundtrip(schema)
111 assert_repr_roundtrip(schema)
112 assert_str_roundtrip_terse(schema)
113 assert_str_roundtrip_verbose(schema)
114
115 @given(
116 id=st.one_of(st.none(), nc_name()),
117 target_id=st.one_of(st.none(), nc_name()),
118 name=st.one_of(st.none(), xml_text()),
119 value=xml_text().filter(lambda x: x.strip() != ""),
120 display_name=st.one_of(st.none(), xml_text()),
121 )
122 def test_fuzz_data(
123 self,
124 id: Optional[str],
125 target_id: Optional[str],
126 name: Optional[str],
127 value: Optional[str],
128 display_name: Optional[str],
129 ) -> None:
130 data = fastkml.Data(
131 id=id,
132 target_id=target_id,
133 name=name,
134 value=value,
135 display_name=display_name,
136 )
137
138 assert_str_roundtrip(data)
139 assert_repr_roundtrip(data)
140 assert_str_roundtrip_terse(data)
141 assert_str_roundtrip_verbose(data)
142
143 @given(
144 name=xml_text().filter(lambda x: x.strip() != ""),
145 value=st.one_of(st.none(), xml_text()),
146 )
147 def test_fuzz_simple_data(
148 self,
149 name: Optional[str],
150 value: Optional[str],
151 ) -> None:
152 simple_data = fastkml.data.SimpleData(
153 name=name,
154 value=value,
155 )
156
157 assert_str_roundtrip(simple_data)
158 assert_repr_roundtrip(simple_data)
159 assert_str_roundtrip_terse(simple_data)
160 assert_str_roundtrip_verbose(simple_data)
161
162 @given(
163 id=st.one_of(st.none(), nc_name()),
164 target_id=st.one_of(st.none(), nc_name()),
165 schema_url=st.one_of(st.none(), urls()),
166 data=st.one_of(st.none(), st.lists(simple_data())),
167 array_data=st.one_of(st.none(), st.lists(simple_array_data())),
168 )
169 def test_fuzz_schema_data(
170 self,
171 id: Optional[str],
172 target_id: Optional[str],
173 schema_url: Optional[str],
174 data: Optional[Iterable[fastkml.data.SimpleData]],
175 array_data: Optional[Iterable[fastkml.gx.data.SimpleArrayData]],
176 ) -> None:
177 schema_data = fastkml.SchemaData(
178 id=id,
179 target_id=target_id,
180 schema_url=schema_url,
181 data=data,
182 array_data=array_data,
183 )
184
185 assert_str_roundtrip(schema_data)
186 assert_repr_roundtrip(schema_data)
187 assert_str_roundtrip_terse(schema_data)
188 assert_str_roundtrip_verbose(schema_data)
189
190 @given(
191 elements=st.one_of(
192 st.none(),
193 st.lists(
194 st.one_of(
195 st.builds(
196 fastkml.data.Data,
197 name=xml_text().filter(lambda x: x.strip() != ""),
198 value=xml_text().filter(lambda x: x.strip() != ""),
199 display_name=st.one_of(st.none(), xml_text()),
200 ),
201 st.builds(
202 fastkml.SchemaData,
203 schema_url=st.one_of(st.none(), urls()),
204 data=st.one_of(st.none(), st.lists(simple_data())),
205 array_data=st.one_of(st.none(), st.lists(simple_array_data())),
206 ),
207 ),
208 ),
209 ),
210 )
211 def test_fuzz_extended_data(
212 self,
213 elements: Optional[Iterable[Union[fastkml.Data, fastkml.SchemaData]]],
214 ) -> None:
215 extended_data = fastkml.ExtendedData(
216 elements=(
217 sorted(elements, key=lambda t: t.__class__.__name__)
218 if elements
219 else None
220 ),
221 )
222
223 assert_repr_roundtrip(extended_data)
224 assert_str_roundtrip(extended_data)
225 assert_str_roundtrip_terse(extended_data)
226 assert_str_roundtrip_verbose(extended_data)