From c84652f5013e8c6f1c378b5d8a5b54bf924c3aa3 Mon Sep 17 00:00:00 2001 From: "Keto D. Zhang" Date: Fri, 15 Nov 2024 11:09:29 -0800 Subject: [PATCH] feat!: migrate example models to tests/ and remove example extension from package fixes #37 --- asdf_pydantic/examples/__init__.py | 0 asdf_pydantic/examples/extensions.py | 19 ----------- pyproject.toml | 3 -- tests/convert_to_asdf_yaml_tree_test.py | 2 +- tests/entry_point_test.py | 11 ------- {asdf_pydantic => tests}/examples/shapes.py | 0 tests/examples/test_rectangle.py | 9 ++---- {asdf_pydantic => tests}/examples/tree.py | 0 tests/schema_validation_test.py | 35 +++++++++++++++++++-- 9 files changed, 36 insertions(+), 43 deletions(-) delete mode 100644 asdf_pydantic/examples/__init__.py delete mode 100644 asdf_pydantic/examples/extensions.py delete mode 100644 tests/entry_point_test.py rename {asdf_pydantic => tests}/examples/shapes.py (100%) rename {asdf_pydantic => tests}/examples/tree.py (100%) diff --git a/asdf_pydantic/examples/__init__.py b/asdf_pydantic/examples/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/asdf_pydantic/examples/extensions.py b/asdf_pydantic/examples/extensions.py deleted file mode 100644 index b79d4d9..0000000 --- a/asdf_pydantic/examples/extensions.py +++ /dev/null @@ -1,19 +0,0 @@ -from __future__ import annotations - -from asdf.extension import Extension - -from asdf_pydantic.converter import AsdfPydanticConverter -from asdf_pydantic.examples.shapes import AsdfRectangle -from asdf_pydantic.examples.tree import AsdfTreeNode - -AsdfPydanticConverter.add_models(AsdfRectangle, AsdfTreeNode) - - -class ExampleExtension(Extension): - extension_uri = "asdf://asdf-pydantic/examples/extensions/examples-1.0.0" - converters = [AsdfPydanticConverter()] # type: ignore - tags = [*AsdfPydanticConverter().tags] # type: ignore - - -def get_extensions() -> list[Extension]: - return [ExampleExtension()] diff --git a/pyproject.toml b/pyproject.toml index a5393e3..34148d8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,9 +33,6 @@ Documentation = "https://asdf-pydantic.readthedocs.io" Issues = "https://github.com/ketozhang/asdf-pydantic/issues" Source = "https://github.com/ketozhang/asdf-pydantic" -[project.entry-points] -'asdf.extensions' = { asdf_pydantic_extension = 'asdf_pydantic.examples.extensions:get_extensions' } - [tool.hatch.version] source = "vcs" diff --git a/tests/convert_to_asdf_yaml_tree_test.py b/tests/convert_to_asdf_yaml_tree_test.py index 37825e1..2f9510f 100644 --- a/tests/convert_to_asdf_yaml_tree_test.py +++ b/tests/convert_to_asdf_yaml_tree_test.py @@ -1,6 +1,6 @@ from __future__ import annotations -from asdf_pydantic.examples.tree import AsdfTreeNode, Node +from tests.examples.tree import AsdfTreeNode, Node def test_sanity(): diff --git a/tests/entry_point_test.py b/tests/entry_point_test.py deleted file mode 100644 index efccb39..0000000 --- a/tests/entry_point_test.py +++ /dev/null @@ -1,11 +0,0 @@ -from tempfile import NamedTemporaryFile - -import asdf - -from asdf_pydantic.examples.shapes import AsdfRectangle - - -def test_create_asdf_file(): - with NamedTemporaryFile() as tempfile: - af = asdf.AsdfFile({"rect": AsdfRectangle(width=42, height=10)}) - af.write_to(tempfile.name) diff --git a/asdf_pydantic/examples/shapes.py b/tests/examples/shapes.py similarity index 100% rename from asdf_pydantic/examples/shapes.py rename to tests/examples/shapes.py diff --git a/tests/examples/test_rectangle.py b/tests/examples/test_rectangle.py index a95a2ba..9d414d7 100644 --- a/tests/examples/test_rectangle.py +++ b/tests/examples/test_rectangle.py @@ -7,13 +7,8 @@ import yaml from asdf.extension import Extension -from asdf_pydantic import AsdfPydanticConverter, AsdfPydanticModel - - -class AsdfRectangle(AsdfPydanticModel): - _tag = "asdf://asdf-pydantic/examples/tags/rectangle-1.0.0" - width: float - height: float +from asdf_pydantic import AsdfPydanticConverter +from tests.examples.shapes import AsdfRectangle @pytest.fixture() diff --git a/asdf_pydantic/examples/tree.py b/tests/examples/tree.py similarity index 100% rename from asdf_pydantic/examples/tree.py rename to tests/examples/tree.py diff --git a/tests/schema_validation_test.py b/tests/schema_validation_test.py index a5365b5..60a3f09 100644 --- a/tests/schema_validation_test.py +++ b/tests/schema_validation_test.py @@ -1,4 +1,5 @@ from tempfile import NamedTemporaryFile +from typing import Annotated import asdf import pydantic @@ -7,8 +8,9 @@ from asdf.extension import Extension from asdf_pydantic import AsdfPydanticConverter -from asdf_pydantic.examples.shapes import AsdfRectangle -from asdf_pydantic.examples.tree import AsdfTreeNode +from asdf_pydantic.model import AsdfPydanticModel +from tests.examples.shapes import AsdfRectangle +from tests.examples.tree import AsdfTreeNode def setup_module(): @@ -136,3 +138,32 @@ def test_given_child_field_contains_asdf_object_then_schema_has_child_tag(): child_schema = schema["definitions"]["AsdfNode"]["properties"]["child"] assert {"tag": AsdfTreeNode._tag} in child_schema["anyOf"] + + +######################################################################################## +# AsdfTag +######################################################################################## +from asdf_pydantic.schema import AsdfTag # noqa: E402 + + +@pytest.mark.parametrize( + "asdf_tag_str, mode, expected_ref_key", + [ + ("http://stsci.edu/schemas/asdf/unit/quantity-1.2.0", "auto", "$ref"), + ("http://stsci.edu/schemas/asdf/unit/quantity-1.2.0", "ref", "$ref"), + ("tag:stsci.edu:asdf/table/table-1.1.0", "auto", "tag"), + ("tag:stsci.edu:asdf/table/table-1.1.0", "tag", "tag"), + ], +) +def test_tag_mode(asdf_tag_str: str, mode, expected_ref_key): + """Test that schema correctly has ``$ref:`` or ``tag:`` depending on the + selected mode. + """ + from astropy.table import Table + + class TestModel(AsdfPydanticModel): + _tag = "asdf://asdf-pydantic/examples/tags/test-model-1.0.0" + table: Annotated[Table, AsdfTag(asdf_tag_str, mode=mode)] + + schema = yaml.safe_load(TestModel.model_asdf_schema()) + assert expected_ref_key in schema["properties"]["table"]