From 85d9627289878abb36e1c8e415dea8f780cbec13 Mon Sep 17 00:00:00 2001 From: jh-RLI Date: Thu, 9 Jan 2025 16:10:52 +0100 Subject: [PATCH] update test for json ld context #232 : - remove nulls from metadata document first as it would break the test. --- tests/metadata/v2/v20/test_context.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/metadata/v2/v20/test_context.py b/tests/metadata/v2/v20/test_context.py index 1d9bc329..8aff3941 100644 --- a/tests/metadata/v2/v20/test_context.py +++ b/tests/metadata/v2/v20/test_context.py @@ -54,6 +54,19 @@ def is_valid_type(value): return False +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 + + def test_jsonld_combination(load_files): """Test combining example.json with context.json and validating JSON-LD.""" # try: @@ -67,10 +80,12 @@ def test_jsonld_combination(load_files): 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 example_data["resources"]: + for resource in cleaned_example_data["resources"]: expanded = jsonld.expand(resource) - assert expanded, f"Validation failed for resource: {resource['@id']}" - - # except Exception as e: - # pytest.fail(f"JSON-LD combination and validation failed: {e}") + assert ( + expanded + ), f"Validation failed for resource: {resource.get('@id', 'unknown')}"