Skip to content
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

0.4 schemas: add testing infrastructure for strict and base validation #92

Merged
merged 10 commits into from
Feb 7, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
]
}
],
"version": "0.4"
"version": "0.4",
"name": "simple_image",
"type": "foo",
"metadata": {
"key": "value"
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@
]
}
],
"version": "0.4"
"version": "0.4",
"name": "image_with_omero_metadata",
"type": "foo",
"metadata": {
"key": "value"
}
}
],
"omero": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
"type": "scale"
}
],
"version": "0.4"
"version": "0.4",
"name": "image_with_coordinateTransformations",
"type": "foo",
"metadata": {
"key": "value"
}
}
]
}
2 changes: 1 addition & 1 deletion 0.4/schemas/image.schema
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://ngff.openmicroscopy.org/0.3/schemas/image.schema",
"$id": "https://ngff.openmicroscopy.org/0.4/schemas/image.schema",
"title": "NGFF Image",
"description": "JSON from OME-NGFF .zattrs",
"type": "object",
Expand Down
3 changes: 2 additions & 1 deletion 0.4/schemas/strict_image.schema
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"$id": "https://ngff.openmicroscopy.org/0.4/schemas/strict_image.schema",
"allOf": [
{
"$ref": "https://ngff.openmicroscopy.org/0.3/schemas/image.schema"
"$ref": "https://ngff.openmicroscopy.org/0.4/schemas/image.schema"
},
{
"properties": {
Expand Down
87 changes: 59 additions & 28 deletions 0.4/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,70 @@

import pytest

from jsonschema import validate
from jsonschema import RefResolver, Draft202012Validator
from jsonschema.exceptions import ValidationError


def files():
return list(glob.glob(f"examples/valid/*.json")) + \
list(glob.glob(f"examples/valid_if_not_strict/*.json")) + \
list(glob.glob(f"examples/invalid/*.json"))
with open('schemas/image.schema') as f:
image_schema = json.load(f)
with open('schemas/strict_image.schema') as f:
strict_image_schema = json.load(f)
schema_store = {
image_schema['$id']: image_schema,
strict_image_schema['$id']: strict_image_schema,
sbesson marked this conversation as resolved.
Show resolved Hide resolved
}

def ids():
return [str(x).split("/")[-1][0:-5] for x in files()]
resolver = RefResolver.from_schema(image_schema, store=schema_store)
validator = Draft202012Validator(image_schema, resolver=resolver)
strict_validator = Draft202012Validator(strict_image_schema, resolver=resolver)

valid_strict_files = list(glob.glob("examples/valid_strict/*.json"))
valid_files = list(glob.glob("examples/valid/*.json"))
invalid_files = list(glob.glob("examples/invalid/*.json"))
invalid_but_dont_fail_files = list(
glob.glob("examples/invalid_but_dont_fail/*.json"))

@pytest.mark.parametrize("testfile", files(), ids=ids())
def test_json(testfile):

if "examples/invalid/" in testfile:
def ids(files):
return [str(x).split("/")[-1][0:-5] for x in files]


@pytest.mark.parametrize(
"testfile", valid_strict_files, ids=ids(valid_strict_files))
def test_valid_strict(testfile):
with open(testfile) as f:
json_file = json.load(f)
validator.validate(json_file)
strict_validator.validate(json_file)


@pytest.mark.parametrize("testfile", valid_files, ids=ids(valid_files))
def test_valid_files(testfile):
with open(testfile) as f:
json_file = json.load(f)
validator.validate(json_file)
with pytest.raises(ValidationError):
strict_validator.validate(json_file)


@pytest.mark.parametrize("testfile", invalid_files, ids=ids(invalid_files))
def test_invalid(testfile):
with open(testfile) as f:
json_file = json.load(f)
with pytest.raises(ValidationError):
validator.validate(json_file)
sbesson marked this conversation as resolved.
Show resolved Hide resolved
with pytest.raises(ValidationError):
strict_validator.validate(json_file)


@pytest.mark.xfail
@pytest.mark.parametrize(
"testfile", invalid_but_dont_fail_files,
ids=ids(invalid_but_dont_fail_files))
def test_invalid_but_dontfail(testfile):
with open(testfile) as f:
json_file = json.load(f)
with pytest.raises(ValidationError):
validator.validate(json_file)
with pytest.raises(ValidationError):
json_schema(testfile)
else:
json_schema(testfile)


def json_schema(path):
# Load the correct schema
with open(path) as f:
test_json = json.loads(f.read())
# we don't have @type in this version
if "multiscales" in test_json:
schema_name = "image.schema"
else:
raise Exception("No schema found")

with open('schemas/' + schema_name) as f:
schema = json.loads(f.read())
validate(instance=test_json, schema=schema)
strict_validator.validate(json_file)