Exemplo n.º 1
0
    def test_save_item(self, tmpdir):
        schema_node = schema.Bool()
        storage_path = str(tmpdir.join('test_save_item.npz'))
        npz_file = npz.Storage(storage_path=storage_path,
                               schema_node=schema_node)
        npz_file.data.value = True
        npz_file.save()

        with np.load(storage_path) as file_:
            assert '_schema' in file_
            assert file_['_schema'][()] == json.dumps(schema_node.to_dict(),
                                                      sort_keys=True)
            assert 'data' in file_
            assert file_['data'].dtype == 'bool'
            assert file_['data'][0]
Exemplo n.º 2
0
    def test_load_compilation(self, tmpdir):
        schema_node = schema.Compilation({
            'spam': schema.Bool(),
            'eggs': schema.Bool()
        })
        schema_data = json.dumps(schema_node.to_dict(), sort_keys=True)
        storage_path = str(tmpdir.join('test_load_compilation.mat'))
        test_data = {
            'data': {
                'spam': np.array([True]),
                'eggs': np.array([False])
            },
            'schema': schema_data
        }
        sio.savemat(storage_path, test_data)

        mat_file = mat.Storage(storage_path=storage_path)
        assert hasattr(mat_file, 'data')
        assert hasattr(mat_file.data, 'spam')
        assert hasattr(mat_file.data, 'eggs')
        assert isinstance(mat_file.data.spam, mat.Bool)
        assert isinstance(mat_file.data.eggs, mat.Bool)
        assert mat_file.data.spam.value is True
        assert mat_file.data.eggs.value is False
Exemplo n.º 3
0
    def test_save_item(self, tmpdir):
        schema_node = schema.Bool()
        file_name = str(tmpdir.join('test_save_item.h5'))
        hdf5_file = hdf5.Storage(storage_path=file_name,
                                 schema_node=schema_node)
        hdf5_file.data.value = True
        hdf5_file.save()
        del hdf5_file

        file_ = h5py.File(file_name, 'r')
        assert 'dsch_schema' in file_.attrs
        assert file_.attrs['dsch_schema'] == json.dumps(schema_node.to_dict(),
                                                        sort_keys=True)
        assert 'dsch_data' in file_
        assert file_['dsch_data'].dtype == 'bool'
        assert file_['dsch_data'][()]
Exemplo n.º 4
0
    def test_save_list(self, tmpdir):
        schema_node = schema.List(schema.Bool())
        storage_path = str(tmpdir.join('test_save_list.mat'))
        mat_file = mat.Storage(storage_path=storage_path,
                               schema_node=schema_node)
        mat_file.data.replace([True, False])
        mat_file.save()

        file_ = sio.loadmat(storage_path, squeeze_me=True)
        assert 'schema' in file_
        assert file_['schema'] == json.dumps(schema_node.to_dict(),
                                             sort_keys=True)
        assert 'data' in file_
        assert len(file_['data']) == 2
        assert file_['data'][0]
        assert not file_['data'][1]
Exemplo n.º 5
0
    def test_load_list(self, tmpdir):
        schema_node = schema.List(schema.Bool())
        schema_data = json.dumps(schema_node.to_dict(), sort_keys=True)
        file_name = str(tmpdir.join('test_load_list.hdf5'))
        raw_file = h5py.File(file_name, 'x')
        raw_file.attrs['dsch_schema'] = schema_data
        data = raw_file.create_group('dsch_data')
        data.create_dataset('item_0', data=True)
        data.create_dataset('item_1', data=False)
        raw_file.flush()
        del raw_file

        hdf5_file = hdf5.Storage(storage_path=file_name)
        assert hasattr(hdf5_file, 'data')
        assert isinstance(hdf5_file.data, hdf5.List)
        assert hdf5_file.data[0].value is True
        assert hdf5_file.data[1].value is False
Exemplo n.º 6
0
    def test_save_list(self, tmpdir):
        schema_node = schema.List(schema.Bool())
        storage_path = str(tmpdir.join('test_save_list.npz'))
        npz_file = npz.Storage(storage_path=storage_path,
                               schema_node=schema_node)
        npz_file.data.replace([True, False])
        npz_file.save()

        with np.load(storage_path) as file_:
            assert '_schema' in file_
            assert file_['_schema'][()] == json.dumps(schema_node.to_dict(),
                                                      sort_keys=True)
            assert 'data.item_0' in file_
            assert file_['data.item_0'].dtype == 'bool'
            assert file_['data.item_0'][0]
            assert 'data.item_1' in file_
            assert file_['data.item_1'].dtype == 'bool'
            assert not file_['data.item_1'][0]
Exemplo n.º 7
0
    def test_save_list(self, tmpdir):
        schema_node = schema.List(schema.Bool())
        file_name = str(tmpdir.join('test_save_list.h5'))
        hdf5_file = hdf5.Storage(storage_path=file_name,
                                 schema_node=schema_node)
        hdf5_file.data.replace([True, False])
        hdf5_file.save()

        file_ = h5py.File(file_name, 'r')
        assert 'dsch_schema' in file_.attrs
        assert file_.attrs['dsch_schema'] == json.dumps(schema_node.to_dict(),
                                                        sort_keys=True)
        assert 'dsch_data' in file_
        assert 'item_0' in file_['dsch_data']
        assert file_['dsch_data']['item_0'].dtype == 'bool'
        assert file_['dsch_data']['item_0'][()]
        assert 'item_1' in file_['dsch_data']
        assert file_['dsch_data']['item_1'].dtype == 'bool'
        assert not file_['dsch_data']['item_1'][()]
Exemplo n.º 8
0
    def storage_path(self, request, tmpdir):
        """Prepare storage path for all valid variants.

        Variants are tuples ``(backend, existing)``, where ``existing``
        indicates whether a storage should be created before the test.
        """
        backend, existing = request.param
        if backend in ('hdf5', 'npz', 'mat'):
            # File backends
            storage_path = str(tmpdir.join('test_pseudo.' + backend))
        elif backend == '::inmem::':
            storage_path = '::inmem::'
        if existing:
            storage = frontend.create(storage_path, schema.Bool())
            storage.data.value = True
            if hasattr(storage, 'save') and callable(storage.save):
                storage.save()
            del storage
        return storage_path
Exemplo n.º 9
0
 def test_init(self, storage_path):
     pseudo = frontend.PseudoStorage(storage_path, schema.Bool(), False)
     assert pseudo.data is not None
     assert pseudo.storage is not None
     assert pseudo.storage.storage_path == storage_path
     assert pseudo.schema_node is not None
Exemplo n.º 10
0
 def test_schema_alternative(self, storage):
     pseudo = frontend.PseudoStorage(storage.data.spam, schema.Bytes(),
                                     True, (schema.Bool(), ))
     pseudo.open()
     assert pseudo.schema_node == storage.data.spam.schema_node
Exemplo n.º 11
0
 def storage(self, foreign_backend):
     schema_node = schema.Compilation({
         'spam': schema.Bool(),
         'eggs': schema.Bytes(),
     })
     return frontend.create(foreign_backend.storage_path, schema_node)
Exemplo n.º 12
0
 def test_init(self, storage):
     pseudo = frontend.PseudoStorage(storage.data.spam, schema.Bool(),
                                     False)
     assert pseudo.data is not None
     assert pseudo.storage is None
     assert pseudo.schema_node is not None
Exemplo n.º 13
0
 def test_init(self):
     node = schema.List(schema.Bool(), max_length=3, min_length=1)
     assert isinstance(node.subnode, schema.Bool)
     assert node.max_length == 3
     assert node.min_length == 1
Exemplo n.º 14
0
 def test_init_defaults(self):
     node = schema.Compilation({
         'spam': schema.Bool(),
         'eggs': schema.Bool()
     })
     assert node.optionals == []
Exemplo n.º 15
0
def test_create(backend):
    schema_node = schema.Bool()
    storage = frontend.create(backend.storage_path, schema_node)
    assert isinstance(storage, backend.module.Storage)
    assert storage.schema_node == schema_node
Exemplo n.º 16
0
 def test_init_deferred(self, storage_path):
     pseudo = frontend.PseudoStorage(storage_path, schema.Bool(), True)
     assert pseudo.data is None
     assert pseudo.storage is None
     assert pseudo.schema_node is None
Exemplo n.º 17
0
 def test_init_defaults(self):
     node = schema.List(schema.Bool())
     assert node.max_length is None
     assert node.min_length is None
Exemplo n.º 18
0
 def test_validate(self):
     subnode = schema.Bool()
     node = schema.List(subnode, max_length=3, min_length=1)
     node.validate([23, 42])
Exemplo n.º 19
0
class TestBool(ItemNodeTestBase):
    class_name = 'Bool'
    schema_node = schema.Bool()
    valid_data = True
Exemplo n.º 20
0
 def test_schema_alternative(self, storage_path_existing):
     pseudo = frontend.PseudoStorage(storage_path_existing, schema.Bytes(),
                                     True, (schema.Bool(), ))
     pseudo.open()
     assert pseudo.schema_node == pseudo.storage.data.schema_node
Exemplo n.º 21
0
import datetime
import json

import numpy as np
import pytest

from dsch import data, schema
from dsch.backends import npz


@pytest.mark.parametrize('schema_node,valid_data', (
    (schema.Array(dtype='int32'), np.array([23, 42], dtype='int32')),
    (schema.Bool(), True),
    (schema.Date(), datetime.date.today()),
    (schema.DateTime(), datetime.datetime.now()),
    (schema.Scalar(dtype='int32'), np.int32(42)),
    (schema.String(), 'spam'),
    (schema.Time(), datetime.datetime.now().time()),
))
def test_save_item_node(schema_node, valid_data):
    data_node = data.data_node_from_schema(schema_node,
                                           module_name='dsch.backends.npz',
                                           parent=None)
    data_node.value = valid_data
    assert np.all(data_node.save() == data_node._storage)


class TestCompilation:
    def test_init_from_storage(self):
        schema_node = schema.Compilation({'spam': schema.Bool(),
                                          'eggs': schema.Bool()})
Exemplo n.º 22
0
def test_create_inmem():
    schema_node = schema.Bool()
    storage = frontend.create('::inmem::', schema_node)
    assert isinstance(storage, inmem.Storage)
    assert storage.schema_node == schema_node
Exemplo n.º 23
0
 def test_validate(self, test_data):
     node = schema.Bool()
     node.validate(test_data)
Exemplo n.º 24
0
import datetime
import json

import numpy as np
import pytest

from dsch import schema
from dsch.exceptions import ValidationError


@pytest.mark.parametrize(
    'node', (schema.Array(dtype='int32'), schema.Bool(), schema.Bytes(),
             schema.Compilation({'spam': schema.Bool()}), schema.Date(),
             schema.DateTime(), schema.List(schema.Bool()),
             schema.Scalar(dtype='int32'), schema.String(), schema.Time()))
class TestGenericSchemaNode:
    def test_to_json(self, node):
        json_str = node.to_json()
        node_dict = json.loads(json_str)
        assert isinstance(node_dict, dict)

    def test_hash(self, node):
        hash = node.hash()
        assert len(hash) == 64


@pytest.mark.parametrize('node1, node2', (
    (schema.Array(dtype='float'), schema.Array(dtype='float')),
    (schema.Bool(), schema.Bool()),
    (schema.Bytes(), schema.Bytes()),
    (schema.Compilation({'spam': schema.Bool()
Exemplo n.º 25
0
 def test_validate_fail(self, test_data):
     node = schema.Bool()
     with pytest.raises(ValidationError):
         node.validate(test_data)