Skip to content

Commit

Permalink
remove _tests._helpers yaml_to_asdf
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Jan 31, 2024
1 parent 44fe69f commit 3410660
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 124 deletions.
56 changes: 0 additions & 56 deletions asdf/_tests/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@
import numpy as np

import asdf
from asdf import versioning
from asdf._asdf import AsdfFile, _get_asdf_library_info
from asdf.exceptions import AsdfConversionWarning
from asdf.tags.core import AsdfObject
from asdf.versioning import (
AsdfVersion,
)

from .httpserver import RangeHTTPServer

Expand All @@ -42,7 +38,6 @@
"get_test_data_path",
"assert_tree_match",
"assert_roundtrip_tree",
"yaml_to_asdf",
"get_file_sizes",
"display_warnings",
]
Expand Down Expand Up @@ -280,57 +275,6 @@ def _assert_roundtrip_tree(
asdf_check_func(ff)


def yaml_to_asdf(yaml_content, yaml_headers=True, standard_version=None):
"""
Given a string of YAML content, adds the extra pre-
and post-amble to make it an ASDF file.
Parameters
----------
yaml_content : string
yaml_headers : bool, optional
When True (default) add the standard ASDF YAML headers.
Returns
-------
buff : io.BytesIO()
A file-like object containing the ASDF-like content.
"""
if isinstance(yaml_content, str):
yaml_content = yaml_content.encode("utf-8")

buff = io.BytesIO()

if standard_version is None:
standard_version = versioning.default_version

standard_version = AsdfVersion(standard_version)

yaml_version = ".".join([str(v) for v in versioning._YAML_VERSION])
af = asdf.AsdfFile(version=standard_version)
tree_converter = af.extension_manager.get_converter_for_type(asdf.tags.core.AsdfObject)
tree_version = asdf.versioning.split_tag_version(tree_converter.tags[0])[1]

if yaml_headers:
buff.write(
f"""#ASDF {versioning._FILE_FORMAT_VERSION}
#ASDF_STANDARD {standard_version}
%YAML {yaml_version}
%TAG ! tag:stsci.edu:asdf/
--- !core/asdf-{tree_version}
""".encode(
"ascii",
),
)
buff.write(yaml_content)
if yaml_headers:
buff.write(b"\n...\n")

buff.seek(0)
return buff


def get_file_sizes(dirname):
"""
Get the file sizes in a directory.
Expand Down
3 changes: 2 additions & 1 deletion asdf/_tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
from asdf import config_context, get_config, treeutil, versioning
from asdf.exceptions import AsdfDeprecationWarning, AsdfWarning, ValidationError
from asdf.extension import ExtensionProxy
from asdf.testing.helpers import yaml_to_asdf

from ._helpers import assert_no_warnings, assert_roundtrip_tree, assert_tree_match, yaml_to_asdf
from ._helpers import assert_no_warnings, assert_roundtrip_tree, assert_tree_match

RNG = np.random.default_rng(97)

Expand Down
103 changes: 52 additions & 51 deletions asdf/_tests/test_asdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from asdf import config_context
from asdf._asdf import AsdfFile, open_asdf
from asdf._entry_points import get_extensions
from asdf._tests._helpers import assert_no_warnings, assert_tree_match, yaml_to_asdf
from asdf._tests._helpers import assert_no_warnings, assert_tree_match
from asdf.exceptions import AsdfDeprecationWarning, AsdfWarning
from asdf.extension import ExtensionProxy
from asdf.testing.helpers import yaml_to_asdf
from asdf.versioning import AsdfVersion


Expand Down Expand Up @@ -215,43 +216,43 @@ def test_reading_extension_metadata():

# Test missing history:
content = """
foo: bar
foo: bar
"""
buff = yaml_to_asdf(content)
with assert_no_warnings():
open_asdf(buff)

# Test the old history format:
content = """
history:
- !core/history_entry-1.0.0
description: Once upon a time, there was a carnivorous panda.
- !core/history_entry-1.0.0
description: This entry intentionally left blank.
foo: bar
history:
- !core/history_entry-1.0.0
description: Once upon a time, there was a carnivorous panda.
- !core/history_entry-1.0.0
description: This entry intentionally left blank.
foo: bar
"""
buff = yaml_to_asdf(content, standard_version="1.0.0")
buff = yaml_to_asdf(content, version="1.0.0")
with assert_no_warnings():
open_asdf(buff)

# Test matching by URI:
content = """
history:
extensions:
- !core/extension_metadata-1.0.0
extension_uri: asdf://somewhere.org/extensions/foo-1.0
extension_class: some.unrecognized.extension.class.Name
history:
extensions:
- !core/extension_metadata-1.0.0
extension_uri: asdf://somewhere.org/extensions/foo-1.0
extension_class: some.unrecognized.extension.class.Name
"""
buff = yaml_to_asdf(content)
with assert_no_warnings():
open_asdf(buff)

# Test matching by legacy class name:
content = """
history:
extensions:
- !core/extension_metadata-1.0.0
extension_class: some.legacy.class.Name
history:
extensions:
- !core/extension_metadata-1.0.0
extension_class: some.legacy.class.Name
"""
buff = yaml_to_asdf(content)
with assert_no_warnings():
Expand All @@ -260,52 +261,52 @@ def test_reading_extension_metadata():
# Warn when the URI is missing, even if there's
# a class name match:
content = f"""
history:
extensions:
- !core/extension_metadata-1.0.0
extension_uri: some-missing-URI
extension_class: {extension_with_uri.class_name}
history:
extensions:
- !core/extension_metadata-1.0.0
extension_uri: some-missing-URI
extension_class: {extension_with_uri.class_name}
"""
buff = yaml_to_asdf(content)
with pytest.warns(AsdfWarning, match=r"URI 'some-missing-URI'"):
open_asdf(buff)

# Warn when the class name is missing:
content = """
history:
extensions:
- !core/extension_metadata-1.0.0
extension_class: some.missing.class.Name
history:
extensions:
- !core/extension_metadata-1.0.0
extension_class: some.missing.class.Name
"""
buff = yaml_to_asdf(content)
with pytest.warns(AsdfWarning, match=r"class 'some.missing.class.Name'"):
open_asdf(buff)

# Warn when the package version is older:
content = """
history:
extensions:
- !core/extension_metadata-1.0.0
extension_uri: asdf://somewhere.org/extensions/foo-1.0
extension_class: some.class.Name
software: !core/software-1.0.0
name: foo
version: 9.2.4
history:
extensions:
- !core/extension_metadata-1.0.0
extension_uri: asdf://somewhere.org/extensions/foo-1.0
extension_class: some.class.Name
software: !core/software-1.0.0
name: foo
version: 9.2.4
"""
buff = yaml_to_asdf(content)
with pytest.warns(AsdfWarning, match=r"older package"):
open_asdf(buff)

# Shouldn't warn when the package version is later:
content = """
history:
extensions:
- !core/extension_metadata-1.0.0
extension_uri: asdf://somewhere.org/extensions/foo-1.0
extension_class: some.class.Name
software: !core/software-1.0.0
name: foo
version: 0.1.2
history:
extensions:
- !core/extension_metadata-1.0.0
extension_uri: asdf://somewhere.org/extensions/foo-1.0
extension_class: some.class.Name
software: !core/software-1.0.0
name: foo
version: 0.1.2
"""
buff = yaml_to_asdf(content)
with assert_no_warnings():
Expand All @@ -314,14 +315,14 @@ def test_reading_extension_metadata():
# Shouldn't receive a warning when the package
# name changes, even if the version is later:
content = """
history:
extensions:
- !core/extension_metadata-1.0.0
extension_uri: asdf://somewhere.org/extensions/foo-1.0
extension_class: some.class.Name
software: !core/software-1.0.0
name: bar
version: 9.4.5
history:
extensions:
- !core/extension_metadata-1.0.0
extension_uri: asdf://somewhere.org/extensions/foo-1.0
extension_class: some.class.Name
software: !core/software-1.0.0
name: bar
version: 9.4.5
"""
buff = yaml_to_asdf(content)
with assert_no_warnings():
Expand Down
19 changes: 10 additions & 9 deletions asdf/_tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from asdf._tests import _helpers as helpers
from asdf.exceptions import AsdfConversionWarning, AsdfDeprecationWarning, AsdfWarning, ValidationError
from asdf.extension import TagDefinition
from asdf.testing.helpers import yaml_to_asdf


@contextlib.contextmanager
Expand Down Expand Up @@ -93,7 +94,7 @@ class ScalarExtension:
"""
with asdf.config_context() as cfg:
cfg.add_extension(ScalarExtension())
buff = helpers.yaml_to_asdf(yaml)
buff = yaml_to_asdf(yaml)
with asdf.open(buff) as ff:
assert isinstance(ff.tree["tagged"], Scalar)
assert not isinstance(ff.tree["not_tagged"], Scalar)
Expand Down Expand Up @@ -450,7 +451,7 @@ class CustomExtension:
custom: !<{tag_uri}>
foo
"""
buff = helpers.yaml_to_asdf(yaml)
buff = yaml_to_asdf(yaml)
# This should cause a warning but not an error because without explicitly
# providing an extension, our custom type will not be recognized and will
# simply be converted to a raw type.
Expand All @@ -474,7 +475,7 @@ class CustomExtension:
custom: !<{tag_uri}>
foo
"""
buff = helpers.yaml_to_asdf(yaml)
buff = yaml_to_asdf(yaml)
with pytest.raises(ValidationError, match=r".* is not of type .*"), asdf.open(
buff,
):
Expand Down Expand Up @@ -626,7 +627,7 @@ class DefaultExtension:
j:
l: 362
"""
buff = helpers.yaml_to_asdf(yaml)
buff = yaml_to_asdf(yaml)
with asdf.open(buff) as ff:
assert "a" in ff.tree["custom"]
assert ff.tree["custom"]["a"] == 42
Expand Down Expand Up @@ -738,7 +739,7 @@ class OneOfExtension:
cfg.add_extension(OneOfExtension())
cfg.add_resource_mapping({schema_uri: tag_schema})

buff = helpers.yaml_to_asdf(yaml)
buff = yaml_to_asdf(yaml)
with asdf.open(buff) as ff:
assert ff["one_of"].value == "foo"

Expand All @@ -753,7 +754,7 @@ def test_tag_reference_validation():
"""

with tag_reference_extension():
buff = helpers.yaml_to_asdf(yaml)
buff = yaml_to_asdf(yaml)
with asdf.open(buff) as ff:
custom = ff.tree["custom"]
assert custom.name == "Something"
Expand Down Expand Up @@ -814,7 +815,7 @@ class ForeignTagReferenceExtension:
cfg.add_resource_mapping({schema_uri: tag_schema})
cfg.add_extension(ForeignTagReferenceExtension())

buff = helpers.yaml_to_asdf(yaml)
buff = yaml_to_asdf(yaml)
with asdf.open(buff) as ff:
a = ff.tree["custom"].a
assert a.name == "Something"
Expand Down Expand Up @@ -891,14 +892,14 @@ def test_max_min_literals_write(num, ttype, tmp_path):
def test_read_large_literal(value):
yaml = f"integer: {value}"

buff = helpers.yaml_to_asdf(yaml)
buff = yaml_to_asdf(yaml)

with pytest.warns(AsdfWarning, match=r"Invalid integer literal value"), asdf.open(buff) as af:
assert af["integer"] == value

yaml = f"{value}: foo"

buff = helpers.yaml_to_asdf(yaml)
buff = yaml_to_asdf(yaml)

with pytest.warns(AsdfWarning, match=r"Invalid integer literal value"), asdf.open(buff) as af:
assert af[value] == "foo"
Expand Down
8 changes: 3 additions & 5 deletions asdf/_tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import asdf
from asdf.exceptions import AsdfConversionWarning

from . import _helpers as helpers
from asdf.testing.helpers import yaml_to_asdf


def test_undefined_tag():
Expand All @@ -23,7 +22,7 @@ def test_undefined_tag():
- !core/ndarray-1.0.0 [[7],[8],[9],[10]]
- !core/complex-1.0.0 3.14j
"""
buff = helpers.yaml_to_asdf(yaml)
buff = yaml_to_asdf(yaml)
with pytest.warns(Warning) as warning:
afile = asdf.open(buff)
missing = afile.tree["undefined_data"]
Expand All @@ -46,5 +45,4 @@ def test_undefined_tag():

# Make sure no warning occurs if explicitly ignored
buff.seek(0)
with helpers.assert_no_warnings():
afile = asdf.open(buff, ignore_unrecognized_tag=True)
afile = asdf.open(buff, ignore_unrecognized_tag=True)
4 changes: 2 additions & 2 deletions asdf/_tests/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,15 @@ def check_asdf(asdf):


def test_explicit_tags():
yaml = """#ASDF 1.0.0
yaml = b"""#ASDF 1.0.0
#ASDF_STANDARD 1.5.0
%YAML 1.1
--- !<tag:stsci.edu:asdf/core/asdf-1.1.0>
foo: !<tag:stsci.edu:asdf/core/ndarray-1.0.0> [1, 2, 3]
..."""

# Check that fully qualified explicit tags work
buff = helpers.yaml_to_asdf(yaml, yaml_headers=False)
buff = io.BytesIO(yaml)

with asdf.open(buff) as ff:
assert all(ff.tree["foo"] == [1, 2, 3])
Expand Down

0 comments on commit 3410660

Please sign in to comment.