Exemple #1
0
def test_custom_validation_with_definitions_bad(tmpdir):
    custom_schema_path = helpers.get_test_data_path('custom_schema_definitions.yaml')
    asdf_file = os.path.join(str(tmpdir), 'out.asdf')

    # This tree does NOT conform to the custom schema
    tree = {
        'forb': { 'biz': 'hello', 'baz': 'world' }
    }

    # Creating file without custom schema should pass
    with asdf.AsdfFile(tree) as ff:
        ff.write_to(asdf_file)

    # Creating file with custom schema should fail
    with pytest.raises(ValidationError):
        with asdf.AsdfFile(tree, custom_schema=custom_schema_path) as ff:
            pass

    # Opening file without custom schema should pass
    with asdf.open(asdf_file) as ff:
        pass

    # Opening file with custom schema should fail
    with pytest.raises(ValidationError):
        with asdf.open(asdf_file, custom_schema=custom_schema_path) as ff:
            pass
Exemple #2
0
def test_custom_validation_with_external_ref_bad(tmpdir):
    custom_schema_path = helpers.get_test_data_path(
        'custom_schema_external_ref.yaml')
    asdf_file = str(tmpdir.join('out.asdf'))

    # This tree does not conform to the custom schema
    tree = {'foo': False}

    # Creating file without custom schema should pass
    with asdf.AsdfFile(tree) as ff:
        ff.write_to(asdf_file)

    # Creating file with custom schema should fail
    with pytest.raises(ValidationError):
        with asdf.AsdfFile(tree, custom_schema=custom_schema_path):
            pass

    # Opening file without custom schema should pass
    with asdf.open(asdf_file):
        pass

    # Opening file with custom schema should fail
    with pytest.raises(ValidationError):
        with asdf.open(asdf_file, custom_schema=custom_schema_path):
            pass
Exemple #3
0
def test_custom_validation_with_definitions_bad(tmpdir):
    custom_schema_path = helpers.get_test_data_path(
        'custom_schema_definitions.yaml')
    asdf_file = str(tmpdir.join('out.asdf'))

    # This tree does NOT conform to the custom schema
    tree = {'forb': {'biz': 'hello', 'baz': 'world'}}

    # Creating file without custom schema should pass
    with asdf.AsdfFile(tree) as ff:
        ff.write_to(asdf_file)

    # Creating file with custom schema should fail
    with pytest.raises(ValidationError):
        with asdf.AsdfFile(tree, custom_schema=custom_schema_path):
            pass

    # Opening file without custom schema should pass
    with asdf.open(asdf_file):
        pass

    # Opening file with custom schema should fail
    with pytest.raises(ValidationError):
        with asdf.open(asdf_file, custom_schema=custom_schema_path):
            pass
Exemple #4
0
def test_self_reference_resolution():
    r = resolver.Resolver(CustomExtension().url_mapping, 'url')
    s = schema.load_schema(
        helpers.get_test_data_path('self_referencing-1.0.0.yaml'),
        resolver=r,
        resolve_references=True)
    assert '$ref' not in repr(s)
    assert s['anyOf'][1] == s['anyOf'][0]
Exemple #5
0
def test_self_reference_resolution():
    r = resolver.Resolver(CustomExtension().url_mapping, 'url')
    s = schema.load_schema(
        helpers.get_test_data_path('self_referencing-1.0.0.yaml'),
        resolver=r,
        resolve_references=True)
    assert '$ref' not in repr(s)
    assert s['anyOf'][1] == s['anyOf'][0]
Exemple #6
0
def test_read_json_schema():
    """Pytest to make sure reading JSON schemas succeeds.

    This was known to fail on Python 3.5 See issue #314 at
    https://github.com/asdf-format/asdf/issues/314 for more details.
    """
    json_schema = helpers.get_test_data_path('example_schema.json')
    schema_tree = schema.load_schema(json_schema, resolve_references=True)
    schema.check_schema(schema_tree)
Exemple #7
0
def test_read_json_schema():
    """Pytest to make sure reading JSON schemas succeeds.

    This was known to fail on Python 3.5 See issue #314 at
    https://github.com/spacetelescope/asdf/issues/314 for more details.
    """
    json_schema = helpers.get_test_data_path('example_schema.json')
    schema_tree = schema.load_schema(json_schema, resolve_references=True)
    schema.check_schema(schema_tree)
Exemple #8
0
def test_custom_validation_good(tmpdir):
    custom_schema_path = helpers.get_test_data_path('custom_schema.yaml')
    asdf_file = str(tmpdir.join('out.asdf'))

    # This tree conforms to the custom schema
    tree = {'foo': {'x': 42, 'y': 10}, 'bar': {'a': 'hello', 'b': 'banjo'}}

    with asdf.AsdfFile(tree, custom_schema=custom_schema_path) as ff:
        ff.write_to(asdf_file)

    with asdf.open(asdf_file, custom_schema=custom_schema_path):
        pass
Exemple #9
0
def test_custom_validation_with_definitions_good(tmpdir):
    custom_schema_path = helpers.get_test_data_path(
        'custom_schema_definitions.yaml')
    asdf_file = str(tmpdir.join('out.asdf'))

    # This tree conforms to the custom schema
    tree = {'thing': {'biz': 'hello', 'baz': 'world'}}

    with asdf.AsdfFile(tree, custom_schema=custom_schema_path) as ff:
        ff.write_to(asdf_file)

    with asdf.open(asdf_file, custom_schema=custom_schema_path):
        pass
Exemple #10
0
def test_custom_validation_with_definitions_good(tmpdir):
    custom_schema_path = helpers.get_test_data_path('custom_schema_definitions.yaml')
    asdf_file = os.path.join(str(tmpdir), 'out.asdf')

    # This tree conforms to the custom schema
    tree = {
        'thing': { 'biz': 'hello', 'baz': 'world' }
    }

    with asdf.AsdfFile(tree, custom_schema=custom_schema_path) as ff:
        ff.write_to(asdf_file)

    with asdf.open(asdf_file, custom_schema=custom_schema_path) as ff:
        pass
Exemple #11
0
def test_custom_validation_good(tmpdir):
    custom_schema_path = helpers.get_test_data_path('custom_schema.yaml')
    asdf_file = os.path.join(str(tmpdir), 'out.asdf')

    # This tree conforms to the custom schema
    tree = {
        'foo': {'x': 42, 'y': 10},
        'bar': {'a': 'hello', 'b': 'banjo'}
    }

    with asdf.AsdfFile(tree, custom_schema=custom_schema_path) as ff:
        ff.write_to(asdf_file)

    with asdf.open(asdf_file, custom_schema=custom_schema_path) as ff:
        pass
Exemple #12
0
def test_custom_validation_with_external_ref_good(tmpdir):
    custom_schema_path = helpers.get_test_data_path(
        'custom_schema_external_ref.yaml')
    asdf_file = str(tmpdir.join('out.asdf'))

    # This tree conforms to the custom schema
    tree = {
        'foo': asdf.tags.core.Software(name="Microsoft Windows", version="95")
    }

    with asdf.AsdfFile(tree, custom_schema=custom_schema_path) as ff:
        ff.write_to(asdf_file)

    with asdf.open(asdf_file, custom_schema=custom_schema_path):
        pass
Exemple #13
0
def test_custom_validation_pathlib(tmpdir):
    """
    Make sure custom schema paths can be pathlib.Path objects

    See https://github.com/asdf-format/asdf/issues/653 for discussion.
    """
    from pathlib import Path

    custom_schema_path = Path(helpers.get_test_data_path('custom_schema.yaml'))
    asdf_file = str(tmpdir.join('out.asdf'))

    # This tree conforms to the custom schema
    tree = {'foo': {'x': 42, 'y': 10}, 'bar': {'a': 'hello', 'b': 'banjo'}}

    with asdf.AsdfFile(tree, custom_schema=custom_schema_path) as ff:
        ff.write_to(asdf_file)

    with asdf.open(asdf_file, custom_schema=custom_schema_path):
        pass
Exemple #14
0
def test_custom_validation_pathlib(tmpdir):
    """
    Make sure custom schema paths can be pathlib.Path objects

    See https://github.com/spacetelescope/asdf/issues/653 for discussion.
    """
    from pathlib import Path

    custom_schema_path = Path(helpers.get_test_data_path('custom_schema.yaml'))
    asdf_file = os.path.join(str(tmpdir), 'out.asdf')

    # This tree conforms to the custom schema
    tree = {
        'foo': {'x': 42, 'y': 10},
        'bar': {'a': 'hello', 'b': 'banjo'}
    }

    with asdf.AsdfFile(tree, custom_schema=custom_schema_path) as ff:
        ff.write_to(asdf_file)

    with asdf.open(asdf_file, custom_schema=custom_schema_path) as ff:
        pass
Exemple #15
0
def test_custom_validation_bad(tmpdir):
    custom_schema_path = helpers.get_test_data_path('custom_schema.yaml')
    asdf_file = str(tmpdir.join('out.asdf'))

    # This tree does not conform to the custom schema
    tree = {'stuff': 42, 'other_stuff': 'hello'}

    # Creating file without custom schema should pass
    with asdf.AsdfFile(tree) as ff:
        ff.write_to(asdf_file)

    # Creating file using custom schema should fail
    with pytest.raises(ValidationError):
        with asdf.AsdfFile(tree, custom_schema=custom_schema_path):
            pass

    # Opening file without custom schema should pass
    with asdf.open(asdf_file):
        pass

    # Opening file with custom schema should fail
    with pytest.raises(ValidationError):
        with asdf.open(asdf_file, custom_schema=custom_schema_path):
            pass
Exemple #16
0
def test_custom_validation_bad(tmpdir):
    custom_schema_path = helpers.get_test_data_path('custom_schema.yaml')
    asdf_file = os.path.join(str(tmpdir), 'out.asdf')

    # This tree does not conform to the custom schema
    tree = {'stuff': 42, 'other_stuff': 'hello'}

    # Creating file without custom schema should pass
    with asdf.AsdfFile(tree) as ff:
        ff.write_to(asdf_file)

    # Creating file using custom schema should fail
    with pytest.raises(ValidationError):
        with asdf.AsdfFile(tree, custom_schema=custom_schema_path) as ff:
            pass

    # Opening file without custom schema should pass
    with asdf.open(asdf_file) as ff:
        pass

    # Opening file with custom schema should fail
    with pytest.raises(ValidationError):
        with asdf.open(asdf_file, custom_schema=custom_schema_path) as ff:
            pass
Exemple #17
0
import numpy as np
from numpy import ma
from numpy.testing import assert_array_equal

import jsonschema

import yaml

import asdf
from asdf import util
from asdf.tests import helpers, CustomTestType
from asdf.tags.core import ndarray

from . import data as test_data
TEST_DATA_PATH = helpers.get_test_data_path('', module=test_data)


# These custom types and the custom extension are here purely for the purpose
# of testing NDArray objects and making sure that they can be validated as part
# of a nested hierarchy, and not just top-level objects.
class CustomNdim(CustomTestType):
    name = 'ndim'
    organization = 'nowhere.org'
    standard = 'custom'
    version = '1.0.0'


class CustomDatatype(CustomTestType):
    name = 'datatype'
    organization = 'nowhere.org'
Exemple #18
0
def test_load_custom_schema_deprecated():
    custom_schema_path = helpers.get_test_data_path('custom_schema.yaml')

    with pytest.deprecated_call():
        schema.load_custom_schema(custom_schema_path)
Exemple #19
0
def test_load_schema_resolve_local_refs_deprecated():
    custom_schema_path = helpers.get_test_data_path(
        'custom_schema_definitions.yaml')

    with pytest.deprecated_call():
        schema.load_schema(custom_schema_path, resolve_local_refs=True)
Exemple #20
0
 def url_mapping(self):
     return [('http://nowhere.org/schemas/custom/',
              util.filepath_to_url(helpers.get_test_data_path('')) +
              '/{url_suffix}.yaml')]
Exemple #21
0
import numpy as np
from numpy import ma
from numpy.testing import assert_array_equal

import jsonschema

import yaml

import asdf
from asdf import util
from asdf.tests import helpers, CustomTestType
from asdf.tags.core import ndarray

from . import data as test_data
TEST_DATA_PATH = helpers.get_test_data_path('', module=test_data)


# These custom types and the custom extension are here purely for the purpose
# of testing NDArray objects and making sure that they can be validated as part
# of a nested hierarchy, and not just top-level objects.
class CustomNdim(CustomTestType):
    name = 'ndim'
    organization = 'nowhere.org'
    standard = 'custom'
    version = '1.0.0'


class CustomDatatype(CustomTestType):
    name = 'datatype'
    organization = 'nowhere.org'
Exemple #22
0
 def url_mapping(self):
     return [('http://nowhere.org/schemas/custom/',
              util.filepath_to_url(helpers.get_test_data_path('')) +
              '/{url_suffix}.yaml')]