From e3bf1d76fad72591dc02ed881a8dbf3a1d593338 Mon Sep 17 00:00:00 2001 From: Quentin BEY Date: Tue, 11 Mar 2025 10:10:59 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85(json)=20add=20a=20test=20for=20declar?= =?UTF-8?q?ed=20schema?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This checks all the defined schema are properly defined. --- src/backend/core/tests/jsonschema/__init__.py | 1 + .../core/tests/jsonschema/test_schema.py | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/backend/core/tests/jsonschema/__init__.py create mode 100644 src/backend/core/tests/jsonschema/test_schema.py diff --git a/src/backend/core/tests/jsonschema/__init__.py b/src/backend/core/tests/jsonschema/__init__.py new file mode 100644 index 0000000..0d20d73 --- /dev/null +++ b/src/backend/core/tests/jsonschema/__init__.py @@ -0,0 +1 @@ +"""Test module for the JSON schema validation.""" diff --git a/src/backend/core/tests/jsonschema/test_schema.py b/src/backend/core/tests/jsonschema/test_schema.py new file mode 100644 index 0000000..2ea4359 --- /dev/null +++ b/src/backend/core/tests/jsonschema/test_schema.py @@ -0,0 +1,53 @@ +"""Test module for the JSON schema validation.""" + +import json +import os + +import pytest + + +def test_all_json_schemas_load_correctly(settings): + """Test that all JSON schema files in the jsonschema directory load correctly.""" + # Get the base directory for jsonschema files + schema_dir = os.path.join(settings.BASE_DIR, "core", "jsonschema") + + # List to store any errors encountered + errors = [] + loaded_schemas = 0 + + # Walk through the jsonschema directory and its subdirectories + for root, _, files in os.walk(schema_dir): + for file in files: + if file.endswith(".json"): + schema_path = os.path.join(root, file) + rel_path = os.path.relpath(schema_path, schema_dir) + + try: + # Try to load the schema + with open(schema_path, "r", encoding="utf-8") as schema_file: + schema = json.load(schema_file) + + # Verify it's a dictionary (basic schema validation) + assert isinstance(schema, dict), ( + f"Schema in {rel_path} is not a dictionary" + ) + + # Check for common schema properties + if "$schema" not in schema: + errors.append( + f"Warning: {rel_path} does not contain a $schema property" + ) + + loaded_schemas += 1 + + except json.JSONDecodeError as e: + errors.append(f"Failed to decode {rel_path}: {e}") + except Exception as e: # noqa: BLE001 pylint: disable=broad-except + errors.append(f"Error loading {rel_path}: {e}") + + # Ensure we found and loaded at least one schema + assert loaded_schemas > 0, "No JSON schema files were found" + + # If any errors were encountered, fail the test + if errors: + pytest.fail("\n".join(errors))