def test_replace_compilation_in_list(self, backend, schema_subnode, valid_subnode_data): schema_compnode = schema.Compilation({'spam': schema_subnode}) data_node = backend.module.List(schema.List(schema_compnode), parent=None, new_params=backend.new_params) data_node.replace([{ 'spam': valid_subnode_data }, { 'spam': valid_subnode_data }]) assert compare_values(data_node[0].spam.value, valid_subnode_data) assert compare_values(data_node[1].spam.value, valid_subnode_data)
def test_init_from_storage(self, hdf5file): test_comp = hdf5file.create_group('test_comp') test_comp.create_dataset('spam', data=True) test_comp.create_dataset('eggs', data=False) schema_node = schema.Compilation({ 'spam': schema.Bool(), 'eggs': schema.Bool() }) data_node = hdf5.Compilation(schema_node, parent=None, data_storage=test_comp) assert data_node.spam.value is True assert data_node.eggs.value is False
def test_init_new(self, hdf5file): schema_node = schema.Compilation({ 'spam': schema.Bool(), 'eggs': schema.Bool() }) comp = hdf5.Compilation(schema_node, parent=None, new_params={ 'name': 'test_comp', 'parent': hdf5file }) assert 'test_comp' in hdf5file assert isinstance(hdf5file['test_comp'], h5py.Group) assert 'spam' in comp._subnodes assert 'eggs' in comp._subnodes
def test_load_from(self, data_node, foreign_backend, schema_subnode, valid_subnode_data): schema_node = schema.Compilation({ 'spam': schema_subnode, 'eggs': schema_subnode }) data_node_foreign = foreign_backend.module.Compilation( schema_node, parent=None, new_params=foreign_backend.new_params) data_node_foreign.spam.value = valid_subnode_data data_node_foreign.eggs.value = valid_subnode_data data_node.load_from(data_node_foreign) assert hasattr(data_node, 'spam') assert hasattr(data_node, 'eggs') assert compare_values(data_node.spam.value, valid_subnode_data) assert compare_values(data_node.eggs.value, valid_subnode_data)
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.npz')) test_data = {'spam': np.array([True]), 'eggs': np.array([False]), '_schema': schema_data} np.savez(storage_path, **test_data) npz_file = npz.Storage(storage_path=storage_path) assert hasattr(npz_file, 'data') assert hasattr(npz_file.data, 'spam') assert hasattr(npz_file.data, 'eggs') assert isinstance(npz_file.data.spam, npz.Bool) assert isinstance(npz_file.data.eggs, npz.Bool) assert npz_file.data.spam.value is True assert npz_file.data.eggs.value is False
def test_save_compilation(self, tmpdir): schema_node = schema.Compilation({'spam': schema.Bool(), 'eggs': schema.Bool()}) storage_path = str(tmpdir.join('test_save_compilation.npz')) npz_file = npz.Storage(storage_path=storage_path, schema_node=schema_node) npz_file.data.spam.value = True npz_file.data.eggs.value = 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 'spam' in file_ assert file_['spam'].dtype == 'bool' assert file_['spam'][0] assert 'eggs' in file_ assert file_['eggs'].dtype == 'bool' assert not file_['eggs'][0]
def test_to_dict(self): node = schema.Compilation( { 'spam': schema.Bool(), 'eggs': schema.Bool() }, optionals=['eggs']) node_dict = node.to_dict() assert 'node_type' in node_dict assert node_dict['node_type'] == 'Compilation' assert 'config' in node_dict assert 'subnodes' in node_dict['config'] assert 'spam' in node_dict['config']['subnodes'] assert 'eggs' in node_dict['config']['subnodes'] for subnode_dict in node_dict['config']['subnodes'].values(): assert 'node_type' in subnode_dict assert 'config' in subnode_dict assert subnode_dict['node_type'] == 'Bool' assert subnode_dict['config'] == {} assert 'optionals' in node_dict['config'] assert node_dict['config']['optionals'] == ['eggs']
def test_save_compilation(self, tmpdir): schema_node = schema.Compilation({ 'spam': schema.Bool(), 'eggs': schema.Bool() }) storage_path = str(tmpdir.join('test_save_compilation.mat')) mat_file = mat.Storage(storage_path=storage_path, schema_node=schema_node) mat_file.data.spam.value = True mat_file.data.eggs.value = 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 'spam' in file_['data'].dtype.fields assert file_['data']['spam'] assert 'eggs' in file_['data'].dtype.fields assert not file_['data']['eggs']
def test_save_compilation(self, tmpdir): schema_node = schema.Compilation({ 'spam': schema.Bool(), 'eggs': schema.Bool() }) file_name = str(tmpdir.join('test_save_compilation.hdf5')) hdf5_file = hdf5.Storage(storage_path=file_name, schema_node=schema_node) hdf5_file.data.spam.value = True hdf5_file.data.eggs.value = 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 'spam' in file_ assert file_['spam'].dtype == 'bool' assert file_['spam'][()] assert 'eggs' in file_ assert file_['eggs'].dtype == 'bool' assert not file_['eggs'][()]
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) file_name = str(tmpdir.join('test_load_compilation.hdf5')) raw_file = h5py.File(file_name, 'x') raw_file.attrs['dsch_schema'] = schema_data raw_file.create_dataset('spam', data=True) raw_file.create_dataset('eggs', data=False) raw_file.flush() del raw_file hdf5_file = hdf5.Storage(storage_path=file_name) assert hasattr(hdf5_file, 'data') assert hasattr(hdf5_file.data, 'spam') assert hasattr(hdf5_file.data, 'eggs') assert isinstance(hdf5_file.data.spam, hdf5.Bool) assert isinstance(hdf5_file.data.eggs, hdf5.Bool) assert hdf5_file.data.spam.value is True assert hdf5_file.data.eggs.value is False
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
def storage(self, foreign_backend): schema_node = schema.Compilation({ 'spam': schema.Bool(), 'eggs': schema.Bytes(), }) return frontend.create(foreign_backend.storage_path, schema_node)
def test_init_defaults(self): node = schema.Compilation({ 'spam': schema.Bool(), 'eggs': schema.Bool() }) assert node.optionals == []
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()