示例#1
0
文件: test_val.py 项目: Trapit/val
def test_callable_gives_sensible_error():

    def less_than_two(value):
        return value < 2

    schema = Schema(less_than_two)
    with pytest.raises(NotValid) as ctx:
        schema.validate(12)
    assert ctx.value.args == ("12 not validated by 'less_than_two'",)
示例#2
0
文件: test_val.py 项目: Trapit/val
def test_issue_9_prioritized_key_comparison_in_dicts():
    # http://stackoverflow.com/questions/14588098/docopt-schema-validation
    schema = Schema(
        {'ID': Convert(int),  # , error='ID should be an int'),
            'FILE': Or(None, Convert(open)),  # , error='FILE not opened')),
            str: object})  # all type keys are optional
    data = {'ID': 10, 'FILE': None, 'other': 'other', 'other2': 'other2'}
    assert schema.validate(data) == data
    data = {'ID': 10, 'FILE': None}
    assert schema.validate(data) == data
示例#3
0
文件: test_val.py 项目: Trapit/val
def test_regression_validating_twice_works():
    schema = Schema({
        'key': str,
        Optional('key2', default='val2', null_values=(None,)): Or(
            str, None)})
    assert (
        schema.validate({'key': 'val', 'key2': 'other_val'}) ==
        {'key': 'val', 'key2': 'other_val'})
    assert schema.validate(
        {'key': 'new_val'}) == {'key': 'new_val', 'key2': 'val2'}
示例#4
0
文件: test_val.py 项目: Trapit/val
def test_callable_gives_readable_error():

    def less_than_two(value):
        """Must be less than two."""
        return value < 2

    schema = Schema(less_than_two)
    with pytest.raises(NotValid) as ctx:
        schema.validate(12)
    assert ctx.value.args == (
        "12 not validated by 'Must be less than two.'",)
示例#5
0
文件: test_val.py 项目: Trapit/val
def test_schema_with_additional_validators():

    def total_greater_than_12(value):
        """foo + bar > 12."""
        return value['foo'] + value['bar'] > 12

    schema = Schema({
        'foo': int,
        'bar': int},
        additional_validators=(total_greater_than_12,))

    assert schema.validates({'foo': 7, 'bar': 7})
    with pytest.raises(NotValid) as ctx:
        schema.validate({'foo': 5, 'bar': 7})
    assert ctx.value.args[0].endswith(
        "not validated by additional validator 'foo + bar > 12.'")
示例#6
0
文件: test_val.py 项目: Trapit/val
def test_dictionary_optional_null_value():
    schema = Schema({
        'key': str,
        Optional('key2', default='val2', null_values=(None,)): Or(
            str, None)})
    assert (
        schema.validate({'key': 'val', 'key2': None}) ==
        {'key': 'val', 'key2': 'val2'})
示例#7
0
文件: test_val.py 项目: Trapit/val
def test_dont_care_values_in_dict():
    schema = Schema(
        {'foo': int,
            'bar': str,
            str: object})
    assert (
        schema.validate({
            'foo': 12,
            'bar': 'bar',
            'qux': 'baz',
            'fnord': [1, 2, 'donkey kong']}) ==
        {
            'foo': 12,
            'bar': 'bar',
            'qux': 'baz',
            'fnord': [1, 2, 'donkey kong']})
示例#8
0
文件: test_val.py 项目: Trapit/val
def test_issue_9_prioritized_key_comparison():
    schema = Schema({'key': 42, object: 42})
    assert schema.validate({'key': 42, 777: 42}) == {'key': 42, 777: 42}
示例#9
0
文件: test_val.py 项目: Trapit/val
def test_identity():
    schema = Schema('test')
    assert schema.validate('test') == 'test'
示例#10
0
文件: test_val.py 项目: Trapit/val
def test_dictionary_wrong_key():
    schema = Schema({'key': str})
    with pytest.raises(NotValid):
        schema.validate({'not_key': 'val'})
示例#11
0
文件: test_val.py 项目: Trapit/val
def test_dictionary_optional_missing():
    schema = Schema({'key': str, Optional('key2', default='val2'): str})
    assert schema.validate(
        {'key': 'val'}) == {'key': 'val', 'key2': 'val2'}
示例#12
0
文件: test_val.py 项目: Trapit/val
def test_callable_exception():
    schema = Schema(lambda x: x + 2)
    with pytest.raises(NotValid):
        schema.validate("foo")
示例#13
0
文件: test_val.py 项目: Trapit/val
def test_callable():
    schema = Schema(lambda x: x < 2)
    assert schema.validate(1) == 1
示例#14
0
文件: test_val.py 项目: Trapit/val
def test_non_identity():
    schema = Schema('test')
    with pytest.raises(NotValid):
        schema.validate('bar')
示例#15
0
文件: test_val.py 项目: Trapit/val
def test_dictionary_not_a_dict():
    schema = Schema({'key': str})
    with pytest.raises(NotValid):
        schema.validate('foo')
示例#16
0
文件: test_val.py 项目: Trapit/val
def test_dictionary_optional():
    schema = Schema({'key': str, Optional('key2'): str})
    assert schema.validate({'key': 'val'}) == {'key': 'val'}
示例#17
0
文件: test_val.py 项目: Trapit/val
def test_type_check():
    schema = Schema(str)
    assert schema.validate('test'), 'test'
示例#18
0
文件: test_val.py 项目: Trapit/val
def test_failing_type_check():
    schema = Schema(int)
    with pytest.raises(NotValid):
        schema.validate('test')
示例#19
0
文件: test_val.py 项目: Trapit/val
def test_dictionary():
    schema = Schema({'key': str})
    assert schema.validate({'key': 'val'}) == {'key': 'val'}
示例#20
0
文件: test_val.py 项目: Trapit/val
def test_dictionary_optional_not_missing():
    schema = Schema({'key': str, Optional(
        'key2', default='val2'): Or(str, None)})
    assert schema.validate({
        'key': 'val',
        'key2': None}) == {'key': 'val', 'key2': None}
示例#21
0
文件: test_val.py 项目: Trapit/val
def test_validate_object():
    schema = Schema({object: str})
    assert schema.validate({42: 'str'}) == {42: 'str'}
    with pytest.raises(NotValid):
        schema.validate({42: 777})
示例#22
0
文件: test_val.py 项目: Trapit/val
def test_dictionary_missing_key():
    schema = Schema({'key': str, 'key2': str})
    with pytest.raises(NotValid):
        schema.validate({'key': 'val'})
示例#23
0
文件: test_val.py 项目: Trapit/val
def test_dictionary_leftover_key():
    schema = Schema({'key': str})
    with pytest.raises(NotValid):
        schema.validate({'key': 'val', 'key2': 'val2'})