Exemplo n.º 1
0
 def test_validate_fail_min_length(self):
     node = schema.String(min_length=3)
     with pytest.raises(ValidationError) as err:
         node.validate('ab')
     assert err.value.message == 'Minimum string length undercut.'
     assert err.value.expected == 3
     assert err.value.got == 2
Exemplo n.º 2
0
 def test_validate_fail_max_length(self):
     node = schema.String(max_length=3)
     with pytest.raises(ValidationError) as err:
         node.validate('abcd')
     assert err.value.message == 'Maximum string length exceeded.'
     assert err.value.expected == 3
     assert err.value.got == 4
Exemplo n.º 3
0
def test_load_validation_fail(backend):
    schema_node = schema.String(max_length=3)
    storage = frontend.create(backend.storage_path, schema_node)
    storage.data.value = 'spam'
    storage.save(force=True)

    with pytest.raises(exceptions.ValidationError):
        frontend.load(backend.storage_path)
    # With force=True, no exception must be raised.
    frontend.load(backend.storage_path, force=True)
Exemplo n.º 4
0
 def test_to_dict(self):
     node = schema.String(min_length=3, max_length=5)
     node_dict = node.to_dict()
     assert 'node_type' in node_dict
     assert node_dict['node_type'] == 'String'
     assert 'config' in node_dict
     assert 'min_length' in node_dict['config']
     assert 'max_length' in node_dict['config']
     assert node_dict['config']['min_length'] == 3
     assert node_dict['config']['max_length'] == 5
Exemplo n.º 5
0
def test_validation_error_chain(backend):
    schema_node = schema.Compilation({
        'spam':
        schema.List(
            schema.Compilation(
                {'eggs': schema.List(schema.String(max_length=3))}))
    })
    data_node = backend.module.Compilation(schema_node,
                                           parent=None,
                                           new_params=backend.new_params)
    data_node.spam.append({'eggs': ['abc', 'def']})
    data_node.spam.append({'eggs': ['abc', 'def', 'ghij']})
    with pytest.raises(exceptions.SubnodeValidationError) as err:
        data_node.validate()
    assert err.value.node_path() == 'spam[1].eggs[2]'
    assert err.value.__cause__.node_path() == '[1].eggs[2]'
    assert err.value.__cause__.__cause__.node_path() == 'eggs[2]'
    assert err.value.__cause__.__cause__.__cause__.node_path() == '[2]'
    assert str(
        err.value).startswith('Node "spam[1].eggs[2]" failed validation:')
    assert str(err.value).endswith(str(err.value.original_cause()))
Exemplo n.º 6
0
 def test_schema_alternative_fail(self, storage_path_existing):
     pseudo = frontend.PseudoStorage(storage_path_existing, schema.Bytes(),
                                     True, (schema.String(), ))
     with pytest.raises(exceptions.InvalidSchemaError):
         pseudo.open()
Exemplo n.º 7
0
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()})
        data_storage = {'spam': np.array([True]),
Exemplo n.º 8
0
class TestString(ItemNodeTestBase):
    class_name = 'String'
    schema_node = schema.String()
    valid_data = 'spam'
Exemplo n.º 9
0
 def test_validate_fail_type(self, test_data):
     node = schema.String()
     with pytest.raises(ValidationError) as err:
         node.validate(test_data)
     assert err.value.message == 'Invalid type/value.'
Exemplo n.º 10
0
 def test_validate(self):
     node = schema.String(max_length=5, min_length=3)
     node.validate('spam')
Exemplo n.º 11
0
 def test_init_defaults(self):
     node = schema.String()
     assert node.max_length is None
     assert node.min_length is None
Exemplo n.º 12
0
 def test_init(self):
     node = schema.String(max_length=5, min_length=3)
     assert node.max_length == 5
     assert node.min_length == 3
Exemplo n.º 13
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()