diff --git a/0.4/examples/valid_if_not_strict/invalid_axis_units.json b/0.4/examples/valid/invalid_axis_units.json similarity index 100% rename from 0.4/examples/valid_if_not_strict/invalid_axis_units.json rename to 0.4/examples/valid/invalid_axis_units.json diff --git a/0.4/examples/valid_if_not_strict/mismatch_axes_units.json b/0.4/examples/valid/mismatch_axes_units.json similarity index 100% rename from 0.4/examples/valid_if_not_strict/mismatch_axes_units.json rename to 0.4/examples/valid/mismatch_axes_units.json diff --git a/0.4/examples/valid/image.json b/0.4/examples/valid_strict/image.json similarity index 81% rename from 0.4/examples/valid/image.json rename to 0.4/examples/valid_strict/image.json index fdd24a71..e6affa00 100644 --- a/0.4/examples/valid/image.json +++ b/0.4/examples/valid_strict/image.json @@ -24,7 +24,12 @@ ] } ], - "version": "0.4" + "version": "0.4", + "name": "simple_image", + "type": "foo", + "metadata": { + "key": "value" + } } ] } \ No newline at end of file diff --git a/0.4/examples/valid/image_metadata.json b/0.4/examples/valid_strict/image_metadata.json similarity index 100% rename from 0.4/examples/valid/image_metadata.json rename to 0.4/examples/valid_strict/image_metadata.json diff --git a/0.4/examples/valid/image_omero.json b/0.4/examples/valid_strict/image_omero.json similarity index 94% rename from 0.4/examples/valid/image_omero.json rename to 0.4/examples/valid_strict/image_omero.json index 2b209ba9..6bf2070b 100644 --- a/0.4/examples/valid/image_omero.json +++ b/0.4/examples/valid_strict/image_omero.json @@ -68,7 +68,12 @@ ] } ], - "version": "0.4" + "version": "0.4", + "name": "image_with_omero_metadata", + "type": "foo", + "metadata": { + "key": "value" + } } ], "omero": { diff --git a/0.4/examples/valid/multiscales_transformations.json b/0.4/examples/valid_strict/multiscales_transformations.json similarity index 84% rename from 0.4/examples/valid/multiscales_transformations.json rename to 0.4/examples/valid_strict/multiscales_transformations.json index f9c2af46..82836d0f 100644 --- a/0.4/examples/valid/multiscales_transformations.json +++ b/0.4/examples/valid_strict/multiscales_transformations.json @@ -36,7 +36,12 @@ "type": "scale" } ], - "version": "0.4" + "version": "0.4", + "name": "image_with_coordinateTransformations", + "type": "foo", + "metadata": { + "key": "value" + } } ] } \ No newline at end of file diff --git a/0.4/schemas/image.schema b/0.4/schemas/image.schema index 6250de66..59e8e28e 100644 --- a/0.4/schemas/image.schema +++ b/0.4/schemas/image.schema @@ -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", diff --git a/0.4/schemas/strict_image.schema b/0.4/schemas/strict_image.schema index d3758444..87d0d6b5 100644 --- a/0.4/schemas/strict_image.schema +++ b/0.4/schemas/strict_image.schema @@ -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": { diff --git a/0.4/tests/test_validation.py b/0.4/tests/test_validation.py index f00608f2..83683ac4 100644 --- a/0.4/tests/test_validation.py +++ b/0.4/tests/test_validation.py @@ -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, +} -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) + 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)