Skip to content

Commit

Permalink
remove _tests._helpers.assert_roundtrip_tree
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Feb 27, 2024
1 parent df5d5c6 commit b53b82d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 218 deletions.
133 changes: 0 additions & 133 deletions asdf/_tests/_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import io
import os
import warnings

try:
Expand All @@ -20,11 +18,6 @@
import numpy as np

import asdf
from asdf._asdf import AsdfFile, _get_asdf_library_info
from asdf.exceptions import AsdfConversionWarning
from asdf.tags.core import AsdfObject

from .httpserver import RangeHTTPServer

try:
from pytest_remotedata.disable_internet import INTERNET_OFF
Expand All @@ -34,7 +27,6 @@

__all__ = [
"assert_tree_match",
"assert_roundtrip_tree",
]


Expand Down Expand Up @@ -129,128 +121,3 @@ def recurse(old, new):
assert old == new

recurse(old_tree, new_tree)


def assert_roundtrip_tree(*args, **kwargs):
"""
Assert that a given tree saves to ASDF and, when loaded back,
the tree matches the original tree.
tree : ASDF tree
tmp_path : `str` or `pathlib.Path`
Path to temporary directory to save file
tree_match_func : `str` or `callable`
Passed to `assert_tree_match` and used to compare two objects in the
tree.
raw_yaml_check_func : callable, optional
Will be called with the raw YAML content as a string to
perform any additional checks.
asdf_check_func : callable, optional
Will be called with the reloaded ASDF file to perform any
additional checks.
"""
with warnings.catch_warnings():
warnings.filterwarnings("error", category=AsdfConversionWarning)
_assert_roundtrip_tree(*args, **kwargs)


def _assert_roundtrip_tree(
tree,
tmp_path,
*,
asdf_check_func=None,
raw_yaml_check_func=None,
write_options=None,
init_options=None,
extensions=None,
tree_match_func="assert_equal",
):
write_options = {} if write_options is None else write_options
init_options = {} if init_options is None else init_options

fname = os.path.join(str(tmp_path), "test.asdf")

# First, test writing/reading a BytesIO buffer
buff = io.BytesIO()
AsdfFile(tree, extensions=extensions, **init_options).write_to(buff, **write_options)
assert not buff.closed
buff.seek(0)
with asdf.open(buff, mode="rw", extensions=extensions) as ff:
assert not buff.closed
assert isinstance(ff.tree, AsdfObject)
assert "asdf_library" in ff.tree
assert ff.tree["asdf_library"] == _get_asdf_library_info()
assert_tree_match(tree, ff.tree, ff, funcname=tree_match_func)
if asdf_check_func:
asdf_check_func(ff)

buff.seek(0)
ff = AsdfFile(extensions=extensions, **init_options)
content = AsdfFile._open_impl(ff, buff, mode="r", _get_yaml_content=True)
buff.close()
# We *never* want to get any raw python objects out
assert b"!!python" not in content
assert b"!core/asdf" in content
assert content.startswith(b"%YAML 1.1")
if raw_yaml_check_func:
raw_yaml_check_func(content)

# Then, test writing/reading to a real file
ff = AsdfFile(tree, extensions=extensions, **init_options)
ff.write_to(fname, **write_options)
with asdf.open(fname, mode="rw", extensions=extensions) as ff:
assert_tree_match(tree, ff.tree, ff, funcname=tree_match_func)
if asdf_check_func:
asdf_check_func(ff)

# Make sure everything works without a block index
write_options["include_block_index"] = False
buff = io.BytesIO()
AsdfFile(tree, extensions=extensions, **init_options).write_to(buff, **write_options)
assert not buff.closed
buff.seek(0)
with asdf.open(buff, mode="rw", extensions=extensions) as ff:
assert not buff.closed
assert isinstance(ff.tree, AsdfObject)
assert_tree_match(tree, ff.tree, ff, funcname=tree_match_func)
if asdf_check_func:
asdf_check_func(ff)

# Now try everything on an HTTP range server
if not INTERNET_OFF:
server = RangeHTTPServer()
try:
ff = AsdfFile(tree, extensions=extensions, **init_options)
ff.write_to(os.path.join(server.tmpdir, "test.asdf"), **write_options)
with asdf.open(server.url + "test.asdf", mode="r", extensions=extensions) as ff:
assert_tree_match(tree, ff.tree, ff, funcname=tree_match_func)
if asdf_check_func:
asdf_check_func(ff)
finally:
server.finalize()

# Now don't be lazy and check that nothing breaks
with io.BytesIO() as buff:
AsdfFile(tree, extensions=extensions, **init_options).write_to(buff, **write_options)
buff.seek(0)
ff = asdf.open(buff, extensions=extensions, memmap=False, lazy_load=False)
# Ensure that all the blocks are loaded
for block in ff._blocks.blocks:
assert block._data is not None and not callable(block._data)
# The underlying file is closed at this time and everything should still work
assert_tree_match(tree, ff.tree, ff, funcname=tree_match_func)
if asdf_check_func:
asdf_check_func(ff)

# Now repeat with memmap=True and a real file to test mmap()
AsdfFile(tree, extensions=extensions, **init_options).write_to(fname, **write_options)
with asdf.open(fname, mode="rw", extensions=extensions, memmap=True, lazy_load=False) as ff:
for block in ff._blocks.blocks:
assert block._data is not None and not callable(block._data)
assert_tree_match(tree, ff.tree, ff, funcname=tree_match_func)
if asdf_check_func:
asdf_check_func(ff)
7 changes: 3 additions & 4 deletions asdf/_tests/tags/core/tests/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import asdf
from asdf import IntegerType
from asdf._tests import _helpers as helpers
from asdf.testing.helpers import roundtrip_object

# Make sure tests are deterministic
random.seed(0)
Expand All @@ -22,13 +22,12 @@
random.getrandbits(200),
],
)
def test_integer_value(tmp_path, value, sign):
def test_integer_value(value, sign):
if sign == "-":
value = -value

integer = IntegerType(value)
tree = {"integer": integer}
helpers.assert_roundtrip_tree(tree, tmp_path)
assert integer == roundtrip_object(integer)


@pytest.mark.parametrize("inline", [False, True])
Expand Down
10 changes: 4 additions & 6 deletions asdf/_tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +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 asdf.testing.helpers import roundtrip_object, yaml_to_asdf

from ._helpers import assert_roundtrip_tree, assert_tree_match
from ._helpers import assert_tree_match

RNG = np.random.default_rng(97)

Expand All @@ -41,11 +41,9 @@ def test_no_warning_nan_array(tmp_path):
"""
Tests for a regression that was introduced by
https://github.com/asdf-format/asdf/pull/557
where saving an array with a nan resulted in a warning
"""

tree = {"array": np.array([1, 2, np.nan])}

assert_roundtrip_tree(tree, tmp_path)
roundtrip_object(np.array([1, 2, np.nan]))


@pytest.mark.skipif(
Expand Down
Loading

0 comments on commit b53b82d

Please sign in to comment.