class TestScalar(ItemNodeTestBase): class_name = 'Scalar' schema_node = None valid_data = None @pytest.fixture(params=( (schema.Scalar('float32'), np.float32(0.123)), (schema.Scalar('float'), 0.123), (schema.Scalar('int32'), np.int32(42)), (schema.Scalar('int32'), 42), ), autouse=True) def setup_scenario(self, request): self.schema_node = request.param[0] self.valid_data = request.param[1]
def test_validate_fail_max_value(self): node = schema.Scalar(dtype='int32', max_value=42) with pytest.raises(ValidationError) as err: node.validate(np.int32(43)) assert err.value.message == 'Maximum value exceeded.' assert err.value.expected == 42 assert err.value.got == 43
def test_validate_fail_min_value(self): node = schema.Scalar(dtype='int32', min_value=23) with pytest.raises(ValidationError) as err: node.validate(np.int32(22)) assert err.value.message == 'Minimum value undercut.' assert err.value.expected == 23 assert err.value.got == 22
def test_init(self): node = schema.Scalar(dtype='int32', unit='V', max_value=42, min_value=23) assert node.dtype == 'int32' assert node.unit == 'V' assert node.max_value == 42 assert node.min_value == 23
def test_to_dict(self): node = schema.Scalar(dtype='float64', unit='V', max_value=42, min_value=23) node_dict = node.to_dict() assert 'node_type' in node_dict assert node_dict['node_type'] == 'Scalar' assert 'config' in node_dict assert node_dict['config'] == { 'dtype': 'float64', 'unit': 'V', 'max_value': 42, 'min_value': 23, }
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()})
def test_validate_fail_type(self, test_data): node = schema.Scalar(dtype='float64') with pytest.raises(ValidationError): node.validate(test_data)
def test_validate(self): node = schema.Scalar(dtype='float64', max_value=42, min_value=23) node.validate(np.float64(23.5))
def test_init_defaults(self): node = schema.Scalar(dtype='int32') assert node.unit == '' assert node.max_value is None assert node.min_value is None
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()