Exemple #1
0
async def test_optional_key_convert_failed_randomly_while_with_another_optional_object(
):
    """
    In this test, created_at string "2015-10-10 00:00:00" is expected to be converted
    to a datetime instance.
        - it works when the schema is

            s = Schema({
                    'created_at': _datetime_validator,
                    Optional(basestring): object,
                })

        - but when wrapping the key 'created_at' with Optional, it fails randomly
    :return:
    """
    import datetime
    fmt = '%Y-%m-%d %H:%M:%S'
    _datetime_validator = Or(None,
                             Use(lambda i: datetime.datetime.strptime(i, fmt)))
    # FIXME given tests enough
    for _ in range(1024):
        s = Schema({
            Optional('created_at'): _datetime_validator,
            Optional('updated_at'): _datetime_validator,
            Optional('birth'): _datetime_validator,
            Optional(basestring): object,
        })
        data = {'created_at': '2015-10-10 00:00:00'}
        validated_data = await s.validate(data)
        # is expected to be converted to a datetime instance, but fails randomly
        # (most of the time)
        assert isinstance(validated_data['created_at'], datetime.datetime)
Exemple #2
0
async def test_dict_forbidden_keys():
    with raises(SchemaForbiddenKeyError):
        await Schema({Forbidden('b'): object}).validate({'b': 'bye'})
    with raises(SchemaWrongKeyError):
        await Schema({Forbidden('b'): int}).validate({'b': 'bye'})
    assert (await Schema({
        Forbidden('b'): int,
        Optional('b'): object
    }).validate({'b': 'bye'}) == {
        'b': 'bye'
    })
    with raises(SchemaForbiddenKeyError):
        await Schema({
            Forbidden('b'): object,
            Optional('b'): object
        }).validate({'b': 'bye'})
Exemple #3
0
async def test_issue_9_prioritized_key_comparison_in_dicts():
    # http://stackoverflow.com/questions/14588098/docopt-schema-validation
    s = Schema({
        'ID': Use(int, error='ID should be an int'),
        'FILE': Or(None, Use(open, error='FILE should be readable')),
        Optional(str): object
    })
    data = {'ID': 10, 'FILE': None, 'other': 'other', 'other2': 'other2'}
    assert await s.validate(data) == data
    data = {'ID': 10, 'FILE': None}
    assert await s.validate(data) == data
Exemple #4
0
async def test_complex():
    s = Schema({
        '<file>': And([Use(open)], lambda l: len(l)),
        '<path>': os.path.exists,
        Optional('--count'): And(int, lambda n: 0 <= n <= 5)
    })
    data = await s.validate({'<file>': ['./LICENSE-MIT'], '<path>': './'})
    assert len(data) == 2
    assert len(data['<file>']) == 1
    assert data['<file>'][0].read().startswith('Copyright')
    assert data['<path>'] == './'
Exemple #5
0
async def test_dict_optional_keys():
    with SE:
        await Schema({'a': 1, 'b': 2}).validate({'a': 1})
    assert await Schema({
        'a': 1,
        Optional('b'): 2
    }).validate({'a': 1}) == {
        'a': 1
    }
    assert await Schema({
        'a': 1,
        Optional('b'): 2
    }).validate({
        'a': 1,
        'b': 2
    }) == {
        'a': 1,
        'b': 2
    }
    # Make sure Optionals are favored over types:
    assert await Schema({
        basestring: 1,
        Optional('b'): 2
    }).validate({
        'a': 1,
        'b': 2
    }) == {
        'a': 1,
        'b': 2
    }
    # Make sure Optionals hash based on their key:
    assert len({Optional('a'): 1, Optional('a'): 1, Optional('b'): 2}) == 2
Exemple #6
0
async def test_dict_optional_defaults():
    # Optionals fill out their defaults:
    assert await Schema({
        Optional('a', default=1): 11,
        Optional('b', default=2): 22
    }).validate({'a': 11}) == {
        'a': 11,
        'b': 2
    }

    # Optionals take precedence over types. Here, the "a" is served by the
    # Optional:
    assert await Schema({
        Optional('a', default=1): 11,
        basestring: 22
    }).validate({'b': 22}) == {
        'a': 1,
        'b': 22
    }

    with raises(TypeError):
        Optional(And(str, Use(int)), default=7)
Exemple #7
0
async def test_nice_errors():
    try:
        await Schema(int, error='should be integer').validate('x')
    except SchemaError as e:
        assert e.errors == ['should be integer']
    try:
        await Schema(Use(float), error='should be a number').validate('x')
    except SchemaError as e:
        assert e.code == 'should be a number'
    try:
        await Schema({
            Optional('i'): Use(int, error='should be a number')
        }).validate({'i': 'x'})
    except SchemaError as e:
        assert e.code == 'should be a number'
Exemple #8
0
async def test_use_json():
    import json
    gist_schema = Schema(
        And(
            Use(json.loads),  # first convert from JSON
            {
                Optional('description'): basestring,
                'public': bool,
                'files': {
                    basestring: {
                        'content': basestring
                    }
                }
            }))
    gist = '''{"description": "the description for this gist",
               "public": true,
               "files": {
                   "file1.txt": {"content": "String file contents"},
                   "other.txt": {"content": "Another file contents"}}}'''
    assert await gist_schema.validate(gist)
Exemple #9
0
from schema_async import Schema, And, Use, Optional
import asyncio

schema = Schema([{
    'name':
    And(str, len),
    'age':
    And(Use(int), lambda n: 18 <= n <= 99),
    Optional('gender'):
    And(str, Use(str.lower), lambda s: s in ('squid', 'kid'))
}])

data = [{
    'name': 'Sue',
    'age': '28',
    'gender': 'Squid'
}, {
    'name': 'Sam',
    'age': '42'
}, {
    'name': 'Sacha',
    'age': '20',
    'gender': 'KID'
}]

loop = asyncio.get_event_loop()

validated = loop.run_until_complete(schema.validate(data))

print(validated)
assert validated == [{