Beispiel #1
0
def test_extension_version_warning():

    yaml = """
history:
  extensions:
    - !core/extension_metadata-1.0.0
      extension_class: asdf.extension.BuiltinExtension
      software: !core/software-1.0.0
        name: asdf
        version: 100.0.3
    """

    buff = yaml_to_asdf(yaml)
    with pytest.warns(None) as warnings:
        with asdf.open(buff) as af:
            pass

    assert len(warnings) == 1, display_warnings(warnings)
    assert str(warnings[0].message).startswith(
        "File was created with extension 'asdf.extension.BuiltinExtension' "
        "from package asdf-100.0.3")

    buff.seek(0)

    # Make sure suppressing the warning works too
    with pytest.warns(None) as warnings:
        with asdf.open(buff, ignore_missing_extensions=True) as af:
            pass

    assert len(warnings) == 0, display_warnings(warnings)
Beispiel #2
0
def test_extension_version_warning():

    yaml = """
history:
  extensions:
    - !core/extension_metadata-1.0.0
      extension_class: asdf.extension.BuiltinExtension
      software: !core/software-1.0.0
        name: asdf
        version: 100.0.3
    """

    buff = yaml_to_asdf(yaml)
    with pytest.warns(None) as warnings:
        with asdf.open(buff) as af:
            pass

    assert len(warnings) == 1, display_warnings(warnings)
    assert str(warnings[0].message).startswith(
        "File was created with extension 'asdf.extension.BuiltinExtension' "
        "from package asdf-100.0.3")

    buff.seek(0)

    # Make sure suppressing the warning works too
    with pytest.warns(None) as warnings:
        with asdf.open(buff, ignore_missing_extensions=True) as af:
            pass

    assert len(warnings) == 0, display_warnings(warnings)
Beispiel #3
0
def _assert_warnings(_warnings):
    if astropy.__version__ < '1.3.3':
        # Make sure at most only one warning occurred
        assert len(_warnings) <= 1, helpers.display_warnings(_warnings)
        # Make sure the warning was the one we expected
        if len(_warnings) == 1:
            message = str(_warnings[0].message)
            target_string = "gwcs and astropy-1.3.3 packages are required"
            assert message.startswith('Failed to convert'), \
                helpers.display_warnings(_warnings)
            assert target_string in str(_warnings[0].message), \
                helpers.display_warnings(_warnings)
    else:
        assert len(_warnings) == 0, helpers.display_warnings(_warnings)
Beispiel #4
0
def test_assert_roundtrip_with_extension(tmpdir):
    called_custom_assert_equal = [False]

    class CustomType(dict, asdftypes.CustomType):
        name = 'custom_flow'
        organization = 'nowhere.org'
        version = (1, 0, 0)
        standard = 'custom'

        @classmethod
        def assert_equal(cls, old, new):
            called_custom_assert_equal[0] = True

    class CustomTypeExtension(CustomExtension):
        @property
        def types(self):
            return [CustomType]

    tree = {
        'custom': CustomType({'a': 42, 'b': 43})
    }

    def check(ff):
        assert isinstance(ff.tree['custom'], CustomType)

    with pytest.warns(None) as warnings:
        helpers.assert_roundtrip_tree(
            tree, tmpdir, extensions=[CustomTypeExtension()])

    assert len(warnings) == 0, helpers.display_warnings(warnings)

    assert called_custom_assert_equal[0] is True
Beispiel #5
0
def test_assert_roundtrip_with_extension(tmpdir):
    called_custom_assert_equal = [False]

    class CustomType(dict, asdftypes.AsdfType):
        name = 'custom_flow'
        organization = 'nowhere.org'
        version = (1, 0, 0)
        standard = 'custom'

        @classmethod
        def assert_equal(cls, old, new):
            called_custom_assert_equal[0] = True

    class CustomTypeExtension(CustomExtension):
        @property
        def types(self):
            return [CustomType]

    tree = {'custom': CustomType({'a': 42, 'b': 43})}

    def check(ff):
        assert isinstance(ff.tree['custom'], CustomType)

    with pytest.warns(None) as warnings:
        helpers.assert_roundtrip_tree(tree,
                                      tmpdir,
                                      extensions=[CustomTypeExtension()])

    assert len(warnings) == 0, helpers.display_warnings(warnings)

    assert called_custom_assert_equal[0] is True
Beispiel #6
0
def test_nonexistent_tag(tmpdir):
    """
    This tests the case where a node is tagged with a type that apparently
    comes from an extension that is known, but the type itself can't be found.

    This could occur when a more recent version of an installed package
    provides the new type, but an older version of the package is installed.
    ASDF should still be able to open the file in this case, but it won't be
    able to restore the type.

    The bug that prompted this test results from attempting to load a schema
    file that doesn't exist, which is why this test belongs in this file.
    """

    # This shouldn't ever happen, but it's a useful test case
    yaml = """
a: !core/doesnt_exist-1.0.0
  hello
    """

    buff = helpers.yaml_to_asdf(yaml)
    with pytest.warns(None) as w:
        with asdf.open(buff) as af:
            assert str(af['a']) == 'hello'
            # Currently there are 3 warnings since one occurs on each of the
            # validation passes. It would be good to consolidate these
            # eventually
            assert len(w) == 3, helpers.display_warnings(w)
            assert str(w[0].message).startswith("Unable to locate schema file")
            assert str(w[1].message).startswith("Unable to locate schema file")
            assert str(w[2].message).startswith(af['a']._tag)

    # This is a more realistic case since we're using an external extension
    yaml = """
a: !<tag:nowhere.org:custom/doesnt_exist-1.0.0>
  hello
  """

    buff = helpers.yaml_to_asdf(yaml)
    with pytest.warns(None) as w:
        with asdf.open(buff, extensions=CustomExtension()) as af:
            assert str(af['a']) == 'hello'
            assert len(w) == 3, helpers.display_warnings(w)
            assert str(w[0].message).startswith("Unable to locate schema file")
            assert str(w[1].message).startswith("Unable to locate schema file")
            assert str(w[2].message).startswith(af['a']._tag)
Beispiel #7
0
def test_nonexistent_tag(tmpdir):
    """
    This tests the case where a node is tagged with a type that apparently
    comes from an extension that is known, but the type itself can't be found.

    This could occur when a more recent version of an installed package
    provides the new type, but an older version of the package is installed.
    ASDF should still be able to open the file in this case, but it won't be
    able to restore the type.

    The bug that prompted this test results from attempting to load a schema
    file that doesn't exist, which is why this test belongs in this file.
    """

    # This shouldn't ever happen, but it's a useful test case
    yaml = """
a: !core/doesnt_exist-1.0.0
  hello
    """

    buff = helpers.yaml_to_asdf(yaml)
    with pytest.warns(None) as w:
        with asdf.open(buff) as af:
            assert str(af['a']) == 'hello'
            # Currently there are 3 warnings since one occurs on each of the
            # validation passes. It would be good to consolidate these
            # eventually
            assert len(w) == 3, helpers.display_warnings(w)
            assert str(w[0].message).startswith("Unable to locate schema file")
            assert str(w[1].message).startswith("Unable to locate schema file")
            assert str(w[2].message).startswith(af['a']._tag)

    # This is a more realistic case since we're using an external extension
    yaml = """
a: !<tag:nowhere.org:custom/doesnt_exist-1.0.0>
  hello
  """

    buff = helpers.yaml_to_asdf(yaml)
    with pytest.warns(None) as w:
        with asdf.open(buff, extensions=CustomExtension()) as af:
            assert str(af['a']) == 'hello'
            assert len(w) == 3, helpers.display_warnings(w)
            assert str(w[0].message).startswith("Unable to locate schema file")
            assert str(w[1].message).startswith("Unable to locate schema file")
            assert str(w[2].message).startswith(af['a']._tag)
Beispiel #8
0
    def runtest(self):
        from asdf import AsdfFile, block, util
        from asdf.tests import helpers

        name, version = parse_schema_filename(self.filename)
        if should_skip(name, version):
            return

        standard_version = self._find_standard_version(name, version)

        # Make sure that the examples in the schema files (and thus the
        # ASDF standard document) are valid.
        buff = helpers.yaml_to_asdf(
            'example: ' + self.example.strip(), standard_version=standard_version)

        ff = AsdfFile(
            uri=util.filepath_to_url(os.path.abspath(self.filename)),
            ignore_unrecognized_tag=self.ignore_unrecognized_tag,
            ignore_version_mismatch=self.ignore_version_mismatch,
        )

        # Fake an external file
        ff2 = AsdfFile({'data': np.empty((1024*1024*8), dtype=np.uint8)})

        ff._external_asdf_by_uri[
            util.filepath_to_url(
                os.path.abspath(
                    os.path.join(
                        os.path.dirname(self.filename), 'external.asdf')))] = ff2

        # Add some dummy blocks so that the ndarray examples work
        for i in range(3):
            b = block.Block(np.zeros((1024*1024*8), dtype=np.uint8))
            b._used = True
            ff.blocks.add(b)
        b._array_storage = "streamed"

        try:
            with pytest.warns(None) as w:
                ff._open_impl(ff, buff, mode='rw')
            # Do not tolerate any warnings that occur during schema validation
            assert len(w) == 0, helpers.display_warnings(w)
        except Exception:
            print("From file:", self.filename)
            raise

        # Just test we can write it out.  A roundtrip test
        # wouldn't always yield the correct result, so those have
        # to be covered by "real" unit tests.
        if b'external.asdf' not in buff.getvalue():
            buff = io.BytesIO()
            ff.write_to(buff)
Beispiel #9
0
    def runtest(self):
        from asdf import AsdfFile, block, util
        from asdf.tests import helpers
        from .extension import TestExtension

        name, version = parse_schema_filename(self.filename)
        if should_skip(name, version):
            return

        standard_version = self._find_standard_version(name, version)

        # Make sure that the examples in the schema files (and thus the
        # ASDF standard document) are valid.
        buff = helpers.yaml_to_asdf(
            'example: ' + self.example.strip(), standard_version=standard_version)
        ff = AsdfFile(
            uri=util.filepath_to_url(os.path.abspath(self.filename)),
            extensions=TestExtension())

        # Fake an external file
        ff2 = AsdfFile({'data': np.empty((1024*1024*8), dtype=np.uint8)})

        ff._external_asdf_by_uri[
            util.filepath_to_url(
                os.path.abspath(
                    os.path.join(
                        os.path.dirname(self.filename), 'external.asdf')))] = ff2

        # Add some dummy blocks so that the ndarray examples work
        for i in range(3):
            b = block.Block(np.zeros((1024*1024*8), dtype=np.uint8))
            b._used = True
            ff.blocks.add(b)
        b._array_storage = "streamed"

        try:
            with pytest.warns(None) as w:
                import warnings
                ff._open_impl(ff, buff, mode='rw')
            # Do not tolerate any warnings that occur during schema validation
            assert len(w) == 0, helpers.display_warnings(w)
        except Exception:
            print("From file:", self.filename)
            raise

        # Just test we can write it out.  A roundtrip test
        # wouldn't always yield the correct result, so those have
        # to be covered by "real" unit tests.
        if b'external.asdf' not in buff.getvalue():
            buff = io.BytesIO()
            ff.write_to(buff)
Beispiel #10
0
def test_missing_extension_warning():

    yaml = """
history:
  extensions:
    - !core/extension_metadata-1.0.0
      extension_class: foo.bar.FooBar
      software: !core/software-1.0.0
        name: foo
        version: 1.2.3
    """

    buff = yaml_to_asdf(yaml)
    with pytest.warns(None) as warnings:
        with asdf.open(buff) as af:
            pass

    assert len(warnings) == 1, display_warnings(warnings)
    assert str(warnings[0].message).startswith(
        "File was created with extension 'foo.bar.FooBar'")
Beispiel #11
0
def test_missing_extension_warning():

    yaml = """
history:
  extensions:
    - !core/extension_metadata-1.0.0
      extension_class: foo.bar.FooBar
      software: !core/software-1.0.0
        name: foo
        version: 1.2.3
    """

    buff = yaml_to_asdf(yaml)
    with pytest.warns(None) as warnings:
        with asdf.open(buff) as af:
            pass

    assert len(warnings) == 1, display_warnings(warnings)
    assert str(warnings[0].message).startswith(
        "File was created with extension 'foo.bar.FooBar'")