-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic valid and invalid tests for the json schema #141
Merged
jorisvandenbossche
merged 16 commits into
opengeospatial:main
from
jorisvandenbossche:schema-tests
Dec 7, 2022
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9d86b54
Add basis valid and invalid tests for the json schema
jorisvandenbossche 0cbe5ed
skip missing_columns_entry - not yet covered by json schema
jorisvandenbossche b27506d
lint
jorisvandenbossche 2c0754a
Merge remote-tracking branch 'upstream/main' into schema-tests
jorisvandenbossche 3a03100
update test data for changes
jorisvandenbossche 5d1af74
convert tests to use pytest
jorisvandenbossche a0777a4
don't include test data
jorisvandenbossche fdfaca7
Merge remote-tracking branch 'upstream/main' into schema-tests
jorisvandenbossche 954622d
fixup
jorisvandenbossche 33ef4b6
simplify
jorisvandenbossche f15c041
undo changes in precommit
jorisvandenbossche 84d0d42
additional test for allowing custom keys
jorisvandenbossche 8aa554a
Merge remote-tracking branch 'upstream/main' into schema-tests
jorisvandenbossche 560ad41
add more cases
jorisvandenbossche 5175bbf
Merge remote-tracking branch 'upstream/main' into schema-tests
jorisvandenbossche 4297cfb
remove xfail
jorisvandenbossche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# Ignore GeoPackage file used in conversion to GeoParquet | ||
*.gpkg* | ||
tests/data/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
""" | ||
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 = {} | ||
|
||
|
||
metadata_template = { | ||
"version": "0.5.0-dev", | ||
"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.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 | ||
|
||
|
||
# # 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): | ||
if "missing_columns_entry" in request.node.callspec.id: | ||
request.node.add_marker( | ||
pytest.mark.xfail(reason="Not yet working", strict=True) | ||
) | ||
|
||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is still failing (it is xfailed for now to have the tests passing):
But I suppose that should be easy to solve to require a minimum length of one for
columns
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. We can fix that, sure. Just add
"minProperties": 1,
in the columns schema.By the way, the written spec doesn't forbid an empty column object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or use/merge #158.