-
Notifications
You must be signed in to change notification settings - Fork 56
/
test_json_schema.py
374 lines (272 loc) · 12.1 KB
/
test_json_schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"""
Test cases (valid and invalid ones) to test the JSON schema.
Run tests with `pytest test_json_schema.py`
Test cases are generated on the fly, but if you want to have them written
as .json files to inspect, run `python test_json_schema.py`
"""
import copy
import json
import pathlib
from jsonschema.validators import Draft7Validator
import pytest
HERE = pathlib.Path(__file__).parent
SCHEMA_SRC = HERE / ".." / "format-specs" / "schema.json"
SCHEMA = json.loads(SCHEMA_SRC.read_text())
# # Define test cases
valid_cases = {}
invalid_cases = {}
def get_version() -> str:
"""Read the version const from the schema.json file"""
with open(HERE / "../format-specs/schema.json") as f:
spec_schema = json.load(f)
return spec_schema["properties"]["version"]["const"]
metadata_template = {
"version": get_version(),
"primary_column": "geometry",
"columns": {
"geometry": {
"encoding": "WKB",
"geometry_types": []
}
},
}
# Minimum required metadata
metadata = copy.deepcopy(metadata_template)
valid_cases["minimal"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata.pop("version")
invalid_cases["missing_version"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["version"] = "bad-version"
invalid_cases["bad_version"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata.pop("primary_column")
invalid_cases["missing_primary_column"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata.pop("columns")
invalid_cases["missing_columns"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"] = {}
invalid_cases["missing_columns_entry"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"].pop("encoding")
invalid_cases["missing_geometry_encoding"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"].pop("geometry_types")
invalid_cases["missing_geometry_type"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["custom_key"] = "value"
valid_cases["custom_key"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["custom_key"] = "value"
valid_cases["custom_key_column"] = metadata
# Geometry columns
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["other_geom"] = copy.deepcopy(metadata["columns"]["geometry"])
valid_cases["geometry_columns_multiple"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["invalid_column_object"] = "foo"
invalid_cases["geometry_columns_invalid_object"] = metadata
# Geometry column name
metadata = copy.deepcopy(metadata_template)
metadata["primary_column"] = "geom"
metadata["columns"]["geom"] = metadata["columns"].pop("geometry")
valid_cases["geometry_column_name"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["primary_column"] = ""
invalid_cases["geometry_column_name_primary_empty"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"][""] = metadata["columns"]["geometry"]
invalid_cases["geometry_column_name_empty"] = metadata
# Encoding
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["encoding"] = "WKT"
invalid_cases["encoding"] = metadata
# Geometry type - non-empty list
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["geometry_types"] = ["Point"]
valid_cases["geometry_type_list"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["geometry_types"] = "Point"
invalid_cases["geometry_type_string"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["geometry_types"] = ["Curve"]
invalid_cases["geometry_type_nonexistent"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["geometry_types"] = ["Point", "Point"]
invalid_cases["geometry_type_uniqueness"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["geometry_types"] = ["PointZ"]
invalid_cases["geometry_type_z_missing_space"] = metadata
# CRS - explicit null
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["crs"] = None
valid_cases["crs_null"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["crs"] = "EPSG:4326"
invalid_cases["crs_string"] = metadata
# Bbox
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["bbox"] = [0, 0, 0, 0]
valid_cases["bbox_4_element"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["bbox"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
valid_cases["bbox_6_element"] = metadata
for n in [3, 5, 7]:
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["bbox"] = [0] * n
invalid_cases[f"bbox_{str(n)}_element"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["bbox"] = ["0", "0", "0", "0"]
invalid_cases["bbox_invalid_type"] = metadata
# Orientation
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["orientation"] = "counterclockwise"
valid_cases["orientation"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["orientation"] = "clockwise"
invalid_cases["orientation"] = metadata
# Edges
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["edges"] = "planar"
valid_cases["edges_planar"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["edges"] = "spherical"
valid_cases["edges_spherical"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["edges"] = "ellipsoid"
invalid_cases["edges"] = metadata
# Epoch
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["epoch"] = 2015.1
valid_cases["epoch"] = metadata
metadata = copy.deepcopy(metadata_template)
metadata["columns"]["geometry"]["epoch"] = "2015.1"
invalid_cases["epoch_string"] = metadata
# Geometry Bbox
metadata_covering_template = copy.deepcopy(metadata_template)
metadata_covering_template["columns"]["geometry"]["covering"] = {
"bbox": {
"xmin": ["bbox", "xmin"],
"ymin": ["bbox", "ymin"],
"xmax": ["bbox", "xmax"],
"ymax": ["bbox", "ymax"],
},
}
# Allow "any_column.xmin" etc.
metadata = copy.deepcopy(metadata_covering_template)
valid_cases["valid_default_bbox"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"] = {
"xmin": ["any_column", "xmin"],
"ymin": ["any_column", "ymin"],
"xmax": ["any_column", "xmax"],
"ymax": ["any_column", "ymax"],
}
valid_cases["valid_but_not_bbox_struct_name"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"] = {
"xmin": ["", "xmin"],
"ymin": ["", "ymin"],
"xmax": ["", "xmax"],
"ymax": ["", "ymax"],
}
invalid_cases["empty_column_name"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"]["xmin"] = []
invalid_cases["xmin_array_length_must_be_2_is_0"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"]["ymax"] = []
invalid_cases["ymax_array_length_must_be_2_is_0"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"]["ymin"] = ["column"]
invalid_cases["ymin_array_length_must_be_2_is_1"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"]["xmax"] = ["column"]
invalid_cases["xmax_array_length_must_be_2_is_1"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"]["xmin"] = ["xmin", "xmin", "xmin"]
invalid_cases["xmin_array_length_must_be_2_is_3"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"].pop("bbox")
invalid_cases["empty_geometry_bbox"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"] = {}
invalid_cases["empty_geometry_bbox_missing_fields"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"].pop("xmin")
invalid_cases["covering_bbox_missing_xmin"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"].pop("ymin")
invalid_cases["covering_bbox_missing_ymin"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"].pop("xmax")
invalid_cases["covering_bbox_missing_xmax"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"].pop("ymax")
invalid_cases["covering_bbox_missing_ymax"] = metadata
# Invalid bbox xmin/xmax/ymin/ymax values
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"]["xmin"] = ["bbox", "not_xmin"]
invalid_cases["covering_bbox_invalid_xmin"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"]["xmax"] = ["bbox", "not_xmax"]
invalid_cases["covering_bbox_invalid_xmax"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"]["ymin"] = ["bbox", "not_ymin"]
invalid_cases["covering_bbox_invalid_ymin"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"]["ymax"] = ["bbox", "not_ymax"]
invalid_cases["covering_bbox_invalid_ymax"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"]["xmin"] = ["bbox", "xmin", "invalid_extra"]
invalid_cases["covering_bbox_extra_xmin_elements"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"]["xmax"] = ["bbox", "xmax", "invalid_extra"]
invalid_cases["covering_bbox_extra_xmax_elements"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"]["ymin"] = ["bbox", "ymin", "invalid_extra"]
invalid_cases["covering_bbox_extra_ymin_elements"] = metadata
metadata = copy.deepcopy(metadata_covering_template)
metadata["columns"]["geometry"]["covering"]["bbox"]["ymax"] = ["bbox", "ymax", "invalid_extra"]
invalid_cases["covering_bbox_extra_ymax_elements"] = metadata
# # Tests
@pytest.mark.parametrize(
"metadata", valid_cases.values(), ids=valid_cases.keys()
)
def test_valid_schema(request, metadata):
errors = Draft7Validator(SCHEMA).iter_errors(metadata)
msgs = []
valid = True
for error in errors:
valid = False
msg = f"- {error.json_path}: {error.message}"
if "description" in error.schema:
msg += f". {error.schema['description']}"
msgs.append(msg)
if not valid:
raise AssertionError(
f"Error while validating '{request.node.callspec.id}':\n"
+ json.dumps({"geo": metadata}, indent=2, sort_keys=True)
+ "\n\nErrors:\n" + "\n".join(msgs)
)
@pytest.mark.parametrize(
"metadata", invalid_cases.values(), ids=invalid_cases.keys()
)
def test_invalid_schema(request, metadata):
errors = Draft7Validator(SCHEMA).iter_errors(metadata)
if not len(list(errors)):
raise AssertionError(
"This is an invalid GeoParquet file, but no validation error "
f"occurred for '{request.node.callspec.id}':\n"
+ json.dumps({"geo": metadata}, indent=2, sort_keys=True)
)
if __name__ == "__main__":
(HERE / "data").mkdir(exist_ok=True)
def write_metadata_json(metadata, name):
with open(HERE / "data" / ("metadata_" + name + ".json"), "w") as f:
json.dump({"geo": metadata}, f, indent=2, sort_keys=True)
for case, metadata in valid_cases.items():
write_metadata_json(metadata, "valid_" + case)
for case, metadata in invalid_cases.items():
write_metadata_json(metadata, "invalid_" + case)