From 94610cbd4e4e7007f62ee1d76c8e6ecd6ad07679 Mon Sep 17 00:00:00 2001 From: "Keto D. Zhang" Date: Fri, 28 Jun 2024 02:52:08 -0700 Subject: [PATCH] test: add tests for examples --- tests/examples/__init__.py | 0 tests/examples/test_rectangle.py | 54 ++++++++++++++++++++++++++++++++ tests/schema_validation_test.py | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/examples/__init__.py create mode 100644 tests/examples/test_rectangle.py diff --git a/tests/examples/__init__.py b/tests/examples/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/examples/test_rectangle.py b/tests/examples/test_rectangle.py new file mode 100644 index 0000000..b52ea49 --- /dev/null +++ b/tests/examples/test_rectangle.py @@ -0,0 +1,54 @@ +import asdf +from asdf.extension import Extension +from asdf.schema import check_schema, load_schema + +from asdf_pydantic import AsdfPydanticConverter +from asdf_pydantic.examples.shapes import AsdfRectangle + + +def setup_module(): + AsdfPydanticConverter.add_models(AsdfRectangle) + + class TestExtension(Extension): + extension_uri = "asdf://asdf-pydantic/examples/extensions/test-1.0.0" # type: ignore + + tags = [*AsdfPydanticConverter().tags] # type: ignore + converters = [AsdfPydanticConverter()] # type: ignore + + # HACK: The schema URI should be referenced from `AsdfRectangle._schema`. + # Then there should be a way to automatically add the schema to ASDF + # resources perhaps during AsdfPydanticConverter.add_models(). Further + # abstracting can be done later, perhaps defining a + # AsdfPydanticExtension. + asdf.get_config().add_resource_mapping( + { + "asdf://asdf-pydantic/shapes/schemas/rectangle-1.0.0": ( + AsdfRectangle.schema_asdf().encode("utf-8") + ) + } + ) + asdf.get_config().add_extension(TestExtension()) + + +def test_schema(): + schema = load_schema("asdf://asdf-pydantic/shapes/schemas/rectangle-1.0.0") + + check_schema(schema) + + assert schema["$schema"] == "http://stsci.edu/schemas/asdf/asdf-schema-1.0.0" + assert schema["title"] == "AsdfRectangle" + assert schema["id"] == "asdf://asdf-pydantic/examples/tags/rectangle-1.0.0" + assert schema["tag"] == "tag:asdf-pydantic/examples/tags/rectangle-1.0.0" + assert schema["type"] == "object" + assert schema["properties"] == { + "width": { + "type": "number", + "title": "Width", + }, + "height": { + "type": "number", + "title": "Height", + }, + } + + assert schema["required"] == ["width", "height"] diff --git a/tests/schema_validation_test.py b/tests/schema_validation_test.py index 041dfe2..f62ac0d 100644 --- a/tests/schema_validation_test.py +++ b/tests/schema_validation_test.py @@ -5,8 +5,8 @@ import pytest import yaml from asdf.extension import Extension -from asdf_pydantic import AsdfPydanticConverter +from asdf_pydantic import AsdfPydanticConverter from asdf_pydantic.examples.shapes import AsdfRectangle from asdf_pydantic.examples.tree import AsdfNode