Exemplo n.º 1
0
 def test_validate_fail_min_length(self):
     node = schema.Bytes(min_length=3)
     with pytest.raises(ValidationError) as err:
         node.validate(b'ab')
     assert err.value.message == 'Minimum bytes 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.Bytes(max_length=3)
     with pytest.raises(ValidationError) as err:
         node.validate(b'abcd')
     assert err.value.message == 'Maximum bytes length exceeded.'
     assert err.value.expected == 3
     assert err.value.got == 4
Exemplo n.º 3
0
 def test_to_dict(self):
     node = schema.Bytes(min_length=3, max_length=5)
     node_dict = node.to_dict()
     assert 'node_type' in node_dict
     assert node_dict['node_type'] == 'Bytes'
     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.º 4
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.º 5
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.º 6
0
 def test_open_fail(self, storage_path_existing):
     pseudo = frontend.PseudoStorage(storage_path_existing, schema.Bytes(),
                                     True)
     with pytest.raises(exceptions.InvalidSchemaError):
         pseudo.open()
Exemplo n.º 7
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.º 8
0
 def test_open_fail(self, storage):
     pseudo = frontend.PseudoStorage(storage.data.spam, schema.Bytes(),
                                     True)
     with pytest.raises(exceptions.InvalidSchemaError):
         pseudo.open()
Exemplo n.º 9
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.º 10
0
class TestBytes(ItemNodeTestBase):
    class_name = 'Bytes'
    schema_node = schema.Bytes()
    valid_data = b'spam'
Exemplo n.º 11
0
class TestBytes(ItemNodeTestBase):
    class_name = 'Bytes'
    schema_node = schema.Bytes()
    valid_data = b'spam'


class TestBool(ItemNodeTestBase):
    class_name = 'Bool'
    schema_node = schema.Bool()
    valid_data = True


@pytest.mark.parametrize('schema_subnode,valid_subnode_data', (
    (schema.Array(dtype='int32'), np.array([23, 42], dtype='int32')),
    (schema.Bytes(), b'spam'),
    (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.time(13, 37, 42, 23)),
))
class TestCompilation:
    @pytest.fixture()
    def data_node(self, backend, schema_subnode):
        schema_node = schema.Compilation({
            'spam': schema_subnode,
            'eggs': schema_subnode
        })
        data_node = backend.module.Compilation(schema_node,
Exemplo n.º 12
0
 def test_validate_fail_type(self, test_data):
     node = schema.Bytes()
     with pytest.raises(ValidationError) as err:
         node.validate(test_data)
     assert err.value.message == 'Invalid type/value.'
Exemplo n.º 13
0
 def test_validate(self):
     node = schema.Bytes(max_length=5, min_length=3)
     node.validate(b'spam')
Exemplo n.º 14
0
 def test_init_defaults(self):
     node = schema.Bytes()
     assert node.max_length is None
     assert node.min_length is None
Exemplo n.º 15
0
 def test_init(self):
     node = schema.Bytes(max_length=5, min_length=3)
     assert node.max_length == 5
     assert node.min_length == 3
Exemplo n.º 16
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()