Skip to content

Commit

Permalink
ignore test typing issues related to JSON manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
fmigneault committed Nov 7, 2024
1 parent bcfb82a commit 7afb188
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

import pystac
import pytest
from jsonschema.exceptions import ValidationError
from pystac.validation.stac_validator import STACValidator

from stac_model.base import JSON
from stac_model.schema import SCHEMA_URI

from conftest import get_all_stac_item_examples

# ignore typing errors introduced by generic JSON manipulation errors
# mypy: disable_error_code="arg-type,call-overload,index,union-attr"


@pytest.mark.parametrize(
"mlm_example", # value passed to 'mlm_example' fixture
Expand All @@ -34,15 +38,15 @@ def test_mlm_schema(
)
def test_mlm_no_undefined_prefixed_field_item_properties(
mlm_validator: STACValidator,
mlm_example: Dict[str, JSON],
mlm_example: dict[str, JSON],
) -> None:
mlm_data = copy.deepcopy(mlm_example)
mlm_item = pystac.Item.from_dict(mlm_data)
pystac.validation.validate(mlm_item, validator=mlm_validator) # ensure original is valid

# undefined property anywhere in the schema
mlm_data = copy.deepcopy(mlm_example)
mlm_data["properties"]["mlm:unknown"] = "random" # type: ignore
mlm_data["properties"]["mlm:unknown"] = "random"
with pytest.raises(pystac.errors.STACValidationError) as exc:
mlm_item = pystac.Item.from_dict(mlm_data)
pystac.validation.validate(mlm_item, validator=mlm_validator)
Expand All @@ -53,12 +57,13 @@ def test_mlm_no_undefined_prefixed_field_item_properties(

# defined property only allowed at the Asset level
mlm_data = copy.deepcopy(mlm_example)
mlm_data["properties"]["mlm:artifact_type"] = "torch.save" # type: ignore
mlm_data["properties"]["mlm:artifact_type"] = "torch.save"
with pytest.raises(pystac.errors.STACValidationError) as exc:
mlm_item = pystac.Item.from_dict(mlm_data)
pystac.validation.validate(mlm_item, validator=mlm_validator)
assert "mlm:artifact_type" in str(exc.value.source[0].validator_value)
assert exc.value.source[0].schema["description"] == "Fields that are disallowed under the Item properties."
errors = cast(list[ValidationError], exc.value.source)
assert "mlm:artifact_type" in str(errors[0].validator_value)
assert errors[0].schema["description"] == "Fields that are disallowed under the Item properties."


@pytest.mark.parametrize(
Expand All @@ -85,17 +90,17 @@ def test_mlm_no_undefined_prefixed_field_asset_properties(
mlm_data = copy.deepcopy(mlm_example)
mlm_item = pystac.Item.from_dict(mlm_data)
pystac.validation.validate(mlm_item, validator=mlm_validator) # ensure original is valid
assert mlm_data["assets"]["weights"] # type: ignore
assert mlm_data["assets"]["weights"]

mlm_data = copy.deepcopy(mlm_example)
mlm_data["assets"]["weights"][test_field] = test_value # type: ignore
mlm_data["assets"]["weights"][test_field] = test_value
with pytest.raises(pystac.errors.STACValidationError) as exc:
mlm_item = pystac.Item.from_dict(mlm_data)
pystac.validation.validate(mlm_item, validator=mlm_validator)
assert len(exc.value.source) == 1 # type: ignore
schema_error = exc.value.source[0] # type: ignore
assert test_field in schema_error.instance
assert schema_error.schema["description"] in [
assert len(exc.value.source) == 1
errors = cast(list[ValidationError], exc.value.source)
assert test_field in errors[0].instance
assert errors[0].schema["description"] in [
"All possible MLM fields regardless of the level they apply (Collection, Item, Asset, Link).",
"Fields that are disallowed under the Asset properties."
]
Expand All @@ -112,7 +117,7 @@ def test_mlm_allowed_field_asset_properties_override(
) -> None:
# defined property allowed both at the Item at the Asset level
mlm_data = copy.deepcopy(mlm_example)
mlm_data["assets"]["weights"]["mlm:accelerator"] = "cuda" # type: ignore
mlm_data["assets"]["weights"]["mlm:accelerator"] = "cuda"
mlm_item = pystac.Item.from_dict(mlm_data)
pystac.validation.validate(mlm_item, validator=mlm_validator)

Expand All @@ -130,7 +135,7 @@ def test_mlm_missing_bands_invalid_if_mlm_input_lists_bands(
pystac.validation.validate(mlm_item, validator=mlm_validator) # ensure original is valid

mlm_bands_bad_data = copy.deepcopy(mlm_example)
mlm_bands_bad_data["assets"]["weights"].pop("raster:bands") # type: ignore # no 'None' to raise in case modified
mlm_bands_bad_data["assets"]["weights"].pop("raster:bands") # no 'None' to raise in case missing
with pytest.raises(pystac.errors.STACValidationError):
mlm_bands_bad_item = pystac.Item.from_dict(mlm_bands_bad_data)
pystac.validation.validate(mlm_bands_bad_item, validator=mlm_validator)
Expand All @@ -149,7 +154,7 @@ def test_mlm_eo_bands_invalid_only_in_item_properties(
pystac.validation.validate(mlm_item, validator=mlm_validator) # ensure original is valid

mlm_eo_bands_bad_data = copy.deepcopy(mlm_example)
mlm_eo_bands_bad_data["assets"]["weights"].pop("eo:bands") # type: ignore # no 'None' to raise in case modified
mlm_eo_bands_bad_data["assets"]["weights"].pop("eo:bands") # no 'None' to raise in case missing
with pytest.raises(pystac.errors.STACValidationError):
mlm_eo_bands_bad_item = pystac.Item.from_dict(mlm_eo_bands_bad_data)
pystac.validation.validate(mlm_eo_bands_bad_item, validator=mlm_validator)
Expand All @@ -165,12 +170,12 @@ def test_mlm_no_input_allowed_but_explicit_empty_array_required(
mlm_example: Dict[str, JSON],
) -> None:
mlm_data = copy.deepcopy(mlm_example)
mlm_data["properties"]["mlm:input"] = [] # type: ignore
mlm_data["properties"]["mlm:input"] = []
mlm_item = pystac.Item.from_dict(mlm_data)
pystac.validation.validate(mlm_item, validator=mlm_validator)

with pytest.raises(pystac.errors.STACValidationError):
mlm_data["properties"].pop("mlm:input") # type: ignore # no 'None' to raise in case modified
mlm_data["properties"].pop("mlm:input") # no 'None' to raise in case missing
mlm_item = pystac.Item.from_dict(mlm_data)
pystac.validation.validate(mlm_item, validator=mlm_validator)

Expand Down Expand Up @@ -230,13 +235,13 @@ def test_mlm_other_non_mlm_assets_allowed(
mlm_item = pystac.Item.from_dict(mlm_data)
pystac.validation.validate(mlm_item, validator=mlm_validator) # self-check valid beforehand

mlm_data["assets"]["sample"] = { # type: ignore
mlm_data["assets"]["sample"] = {
"type": "image/jpeg",
"href": "https://example.com/sample/output.jpg",
"roles": ["preview"],
"title": "Model Output Predictions Sample",
}
mlm_data["assets"]["model-cart"] = { # type: ignore
mlm_data["assets"]["model-cart"] = {
"type": "text/markdown",
"href": "https://example.com/sample/model.md",
"roles": ["metadata"],
Expand Down Expand Up @@ -285,7 +290,8 @@ def test_mlm_at_least_one_asset_model(
else:
with pytest.raises(pystac.errors.STACValidationError) as exc:
pystac.validation.validate(mlm_item, validator=mlm_validator)
assert exc.value.source[0].schema["$comment"] in [ # type: ignore
errors = cast(list[ValidationError], exc.value.source)
assert errors[0].schema["$comment"] in [
"At least one Asset must provide the model definition indicated by the 'mlm:model' role.",
"Used to check the artifact type property that is required by a Model Asset annotated by 'mlm:model' role."
]
Expand Down

0 comments on commit 7afb188

Please sign in to comment.