diff --git a/Makefile b/Makefile index aa2682ee5f..00c1fb21f4 100644 --- a/Makefile +++ b/Makefile @@ -61,7 +61,7 @@ clean: clean-import: clean import @echo "✓ Clean import complete!" -test: setup +test: setup fetch-schemas @echo "Running unit tests..." @$(PYTHON) -m unittest discover tests -v diff --git a/data/wash-media/generic/water.yaml b/data/wash-media/generic/water.yaml new file mode 100644 index 0000000000..9450b0176e --- /dev/null +++ b/data/wash-media/generic/water.yaml @@ -0,0 +1,7 @@ +slug: water +uuid: 78c9e93b-94dc-4c63-af42-4bce6304142f +brand: + slug: generic +name: Water +density: 1 +wash_capacity: 10000 diff --git a/schema_version.conf b/schema_version.conf index a8b7cf3f2c..e6ba1798a4 100644 --- a/schema_version.conf +++ b/schema_version.conf @@ -1,5 +1,5 @@ # OpenPrintTag Schema Configuration SCHEMA_REPO_URL="https://github.com/OpenPrintTag/openprinttag-architecture.git" -SCHEMA_COMMIT="438a81fb5ae1c3c21a1c23ba00c309db0a337da0" +SCHEMA_COMMIT="328e859465f9a3c499e69f141a7bc3c16273e911" SCHEMA_SPARSE_PATH="schema/generated/opt_db_schema" SCHEMA_TARGET_DIR="./openprinttag" diff --git a/scripts/validate_json_schema.py b/scripts/validate_json_schema.py index 84ef3b013e..355c140b1c 100755 --- a/scripts/validate_json_schema.py +++ b/scripts/validate_json_schema.py @@ -46,6 +46,7 @@ class JsonSchemaValidator: 'materials': 'material.schema.json', 'material-packages': 'material_package.schema.json', 'material-containers': 'material_container.schema.json', + 'wash-media': 'wash_medium.schema.json', } # Foreign key definitions: entity -> [(field_path, target_entity, target_field, is_array, condition)] @@ -68,6 +69,9 @@ class JsonSchemaValidator: 'brands': [ (['countries_of_origin'], 'countries', 'code', True, None), ], + 'wash-media': [ + (['brand', 'slug'], 'brands', 'slug', False, None), + ], } def __init__(self, base_path: Path): diff --git a/tests/test_validate_wash_media.py b/tests/test_validate_wash_media.py new file mode 100644 index 0000000000..945a9c5c8a --- /dev/null +++ b/tests/test_validate_wash_media.py @@ -0,0 +1,64 @@ +""" +Tests that wash-media entities are actually validated by validate_json_schema.py. +""" + +import shutil +import sys +import tempfile +import unittest +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).parent.parent)) +sys.path.insert(0, str(Path(__file__).parent.parent / "scripts")) + +from scripts.validate_json_schema import JsonSchemaValidator + +REPO_ROOT = Path(__file__).parent.parent + + +class TestWashMediaValidation(unittest.TestCase): + """Confirm the wash-media data directory is wired into the validator.""" + + def test_generic_water_is_validated_without_errors(self): + """The real data/wash-media tree should be picked up and pass validation.""" + validator = JsonSchemaValidator(REPO_ROOT) + count = validator.validate_entity_directory('wash-media', validator.ENTITY_SCHEMA_MAPPING['wash-media']) + + self.assertGreaterEqual(count, 1, "no wash-media files were found/validated") + wash_media_errors = [e for e in validator.errors if e.entity == 'wash-media'] + self.assertEqual( + [str(e) for e in wash_media_errors], [], + "wash-media data failed schema validation" + ) + + def test_invalid_wash_medium_is_rejected(self): + """A wash medium with a non-numeric density must be caught by the validator.""" + temp_dir = Path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, temp_dir, ignore_errors=True) + + wash_media_dir = temp_dir / "wash-media" / "generic" + wash_media_dir.mkdir(parents=True) + (wash_media_dir / "bad-water.yaml").write_text( + "slug: bad-water\n" + "uuid: 78c9e93b-94dc-4c63-af42-4bce6304142f\n" + "brand:\n" + " slug: generic\n" + "name: Bad Water\n" + "density: not-a-number\n" + "wash_capacity: 10000\n" + ) + + validator = JsonSchemaValidator(REPO_ROOT) + validator.data_dir = temp_dir # schema_dir stays pointed at the real, current schema + + validator.validate_entity_directory('wash-media', validator.ENTITY_SCHEMA_MAPPING['wash-media']) + + schema_errors = [e for e in validator.errors if e.rule == 'schema_validation'] + self.assertTrue( + schema_errors, + "validator did not flag a wash medium with a non-numeric density" + ) + + +if __name__ == '__main__': + unittest.main()