Skip to content

Commit

Permalink
test: Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ketozhang committed Jun 28, 2024
1 parent 2260efe commit 57ec924
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
6 changes: 4 additions & 2 deletions asdf_pydantic/schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from pydantic.json_schema import GenerateJsonSchema

DEFAULT_ASDF_SCHEMA_REF_TEMPLATE = "#/definitions/{model}"
Expand All @@ -14,14 +16,14 @@ class GenerateAsdfSchema(GenerateJsonSchema):
"""

# HACK: When we can support tree models, then not all schema should have tag
tag: str | None
tag: Optional[str]
schema_dialect = "http://stsci.edu/schemas/asdf/asdf-schema-1.0.0"

def __init__(
self,
by_alias: bool = True,
ref_template: str = DEFAULT_ASDF_SCHEMA_REF_TEMPLATE,
tag: str | None = None,
tag: Optional[str] = None,
):
super().__init__(by_alias=by_alias, ref_template=ref_template)
self.tag = tag
Expand Down
12 changes: 8 additions & 4 deletions tests/examples/test_rectangle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asdf
import pytest
from asdf.extension import Extension
from asdf.schema import check_schema, load_schema
from yaml.scanner import ScannerError

from asdf_pydantic import AsdfPydanticConverter
from asdf_pydantic.examples.shapes import AsdfRectangle
Expand All @@ -23,17 +25,19 @@ class TestExtension(Extension):
asdf.get_config().add_resource_mapping(
{
"asdf://asdf-pydantic/shapes/schemas/rectangle-1.0.0": (
AsdfRectangle.schema_asdf().encode("utf-8")
AsdfRectangle.model_asdf_schema().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)
try:
schema = load_schema("asdf://asdf-pydantic/shapes/schemas/rectangle-1.0.0")
check_schema(schema)
except ScannerError as e:
pytest.fail(f"{e}\n{AsdfRectangle.model_asdf_schema()}")

assert schema["$schema"] == "http://stsci.edu/schemas/asdf/asdf-schema-1.0.0"
assert schema["title"] == "AsdfRectangle"
Expand Down
4 changes: 2 additions & 2 deletions tests/schema_validation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TestExtension(Extension):
asdf.get_config().add_resource_mapping(
{
"asdf://asdf-pydantic/shapes/schemas/rectangle-1.0.0": (
AsdfRectangle.schema_asdf().encode("utf-8")
AsdfRectangle.model_asdf_schema().encode("utf-8")
)
}
)
Expand Down Expand Up @@ -130,7 +130,7 @@ def test_validate_fail_on_bad_yaml_file():
def test_given_child_field_contains_asdf_object_then_schema_has_child_tag():
from asdf.schema import check_schema

schema = yaml.safe_load(AsdfNode.schema_asdf()) # type: ignore
schema = yaml.safe_load(AsdfNode.model_asdf_schema()) # type: ignore
check_schema(schema)

child_schema = schema["definitions"]["AsdfNode"]["properties"]["child"]
Expand Down

0 comments on commit 57ec924

Please sign in to comment.