forked from crs4/rocrate-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
Currently the profile files for ensuring that invalid crates are caught by the validator are created as duplicates of the original (valid) crate. This means that multiple, almost identical, files will have to be edited everytime the crate profile is changed.
Instead it makes sense to programatically modify the valid crate to create invalid crates for each test, saving the test file in pytest's temporary directory for ease of checking these.
ChatGPT suggests this style of structure.
Have a reusable fixture for a valid base input
# conftest.py or in your test file
import pytest
import copy
@pytest.fixture
def base_jsonld():
return {
"@context": [
"https://w3id.org/ro/crate/1.1/context",
"https://w3id.org/ro/terms/test"
],
"@graph": [
{
"@id": "ro-crate-metadata.json",
"@type": "CreativeWork",
"about": {"@id": "./"},
"conformsTo": [
{"@id": "https://w3id.org/ro/crate/1.1"},
{"@id": "https://w3id.org/workflowhub/workflow-ro-crate/1.0"}
]
}
]
}
Define error variants as transformations
def missing_context(data):
data = copy.deepcopy(data)
del data["@context"]
return data
def missing_graph_entry(data):
data = copy.deepcopy(data)
data["@graph"].clear()
return data
def malformed_conforms_to(data):
data = copy.deepcopy(data)
data["@graph"][0]["conformsTo"] = "not-a-list"
return data
Parameterize the tests
import pytest
import json
from mymodule import process_jsonld # your processing function
@pytest.mark.parametrize("modifier,expected_exception", [
(missing_context, KeyError),
(missing_graph_entry, ValueError),
(malformed_conforms_to, TypeError),
])
def test_jsonld_processing_errors(base_jsonld, modifier, expected_exception, tmp_path):
modified = modifier(base_jsonld)
input_file = tmp_path / "test.json"
input_file.write_text(json.dumps(modified, indent=2))
with pytest.raises(expected_exception):
process_jsonld(input_file)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels