Tests#
Test - Load File#
test_FILE_should_load
Warning
"Cannot open FILE!"
Solutions: - Check if the file path or name is correct - Check if the test path or name is correct
Test - Validate Schema#
Validate OEMetadata Schema with JSON Schema#
test_jsonschema_should_validate_oemetadata_schema
Warning
"Cannot validate OEMetadata Schema with JSON Schema (VERSION)!"
Solutions: - Check if correct JSON Schema has been used - Check if OEMetadata Schema is valid
Validate FILE with OEMetadata Schema#
test_oemetadata_schema_should_validate_FILE
Warning
"Cannot validate FILE with OEMetadata Schema (VERSION)!"
Solutions: - Check if correct OEMetadata Schema has been used - Check if OEMetadata Schema is valid - Check if FILE is valid
Test - Correct path#
test_FILE_should_have_correct_path
Warning
"Wrong path in FILE (VERSION)!"
Solutions: - Check if path is correct - Check if test path is correct
Unit Tests#
About the example
Below you see the rendered version of the example test
test/oemetadata/latest
test_context
#
clean_context(context)
#
Remove invalid entries (placeholders or invalid @type).
Source code in test\oemetadata\latest\test_context.py
def clean_context(context):
"""Remove invalid entries (placeholders or invalid @type)."""
cleaned_context = {}
for key, value in context.items():
if isinstance(value, dict):
# Remove entries with invalid @id or @type
if value.get("@id") == "xx" or value.get("@type") == "xx":
continue
# Keep only valid entries
cleaned_context[key] = {
k: v
for k, v in value.items()
if v != "xx" and (k != "@type" or is_valid_type(v))
}
elif value != "xx":
cleaned_context[key] = value
return cleaned_context
clean_null_ids(data)
#
Recursively remove @id entries with null values.
Source code in test\oemetadata\latest\test_context.py
def clean_null_ids(data):
"""Recursively remove @id entries with null values."""
if isinstance(data, dict):
return {
key: clean_null_ids(value)
for key, value in data.items()
if not (key == "@id" and value is None)
}
elif isinstance(data, list):
return [clean_null_ids(item) for item in data]
return data
is_valid_type(value)
#
Check if @type value is a valid absolute or compact IRI.
Source code in test\oemetadata\latest\test_context.py
def is_valid_type(value):
"""Check if @type value is a valid absolute or compact IRI."""
# Valid if it's a known compact IRI (e.g., xsd:string)
if ":" in value and not value.startswith("@"):
return True
# Invalid if it contains spaces or invalid characters
return False
load_files()
#
Load the example.json and context.json files.
Source code in test\oemetadata\latest\test_context.py
@pytest.fixture
def load_files():
"""Load the example.json and context.json files."""
base_path = "oemetadata/latest/"
example_file = os.path.join(base_path, "example.json")
context_file = os.path.join(base_path, "context.json")
# Load example.json
with open(example_file) as ef:
example_data = json.load(ef)
# Load context.json
with open(context_file) as cf:
context_data = json.load(cf)
return example_data, context_data
test_jsonld_combination(load_files)
#
Test combining example.json with context.json and validating JSON-LD.
Source code in test\oemetadata\latest\test_context.py
def test_jsonld_combination(load_files):
"""Test combining example.json with context.json and validating JSON-LD."""
# try:
# Get the example and context data
example_data, context_data = load_files
# Clean the @context from placeholders and invalid entries
cleaned_context = clean_context(context_data["@context"])
# Update the @context for each resource in the resources array
for resource in example_data.get("resources", []):
resource["@context"] = cleaned_context
# Remove @id with null values from resources
cleaned_example_data = clean_null_ids(example_data)
# Validate each resource as JSON-LD
for resource in cleaned_example_data["resources"]:
expanded = jsonld.expand(resource)
assert (
expanded
), f"Validation failed for resource: {resource.get('@id', 'unknown')}"