Beispiel #1
0
def test_schema_empty_dict():
    s = Schema({})
    s({})

    with raises(MultipleInvalid, "extra keys not allowed @ data['var']"):
        assert s({'var': 123})

    with raises(MultipleInvalid, "expected a dictionary"):
        assert s([123])
Beispiel #2
0
def test_schema_empty_list():
    s = Schema([])
    s([])

    with raises(MultipleInvalid, "not a valid value @ data[123]"):
        assert s([123])

    with raises(MultipleInvalid, "expected a list"):
        assert s({'var': 123}) 
Beispiel #3
0
def test_frozenset_of_integers():
    schema = Schema(frozenset([int]))
    with raises(Invalid, 'expected a frozenset'):
        schema(42)
    with raises(Invalid, 'expected a frozenset'):
        schema(set([42]))

    schema(frozenset())
    schema(frozenset([42]))
    schema(frozenset([42, 43, 44]))
    try:
        schema(frozenset(['abc']))
    except MultipleInvalid as e:
        assert_equal(str(e), "invalid value in frozenset")
    else:
        assert False, "Did not raise Invalid"
Beispiel #4
0
def test_set_of_integers():
    schema = Schema({int})
    with raises(Invalid, 'expected a set'):
        schema(42)
    with raises(Invalid, 'expected a set'):
        schema(frozenset([42]))

    schema(set())
    schema(set([42]))
    schema(set([42, 43, 44]))
    try:
        schema(set(['abc']))
    except MultipleInvalid as e:
        assert str(e) == "invalid value in set"
    else:
        assert False, "Did not raise Invalid"
Beispiel #5
0
def test_schema_empty_dict_key():
    """ https://github.com/alecthomas/voluptuous/pull/434 """
    s = Schema({'var': []})
    s({'var': []})

    with raises(MultipleInvalid, "not a valid value for dictionary value @ data['var']"):
        assert s({'var': [123]})
Beispiel #6
0
def test_frozenset_of_integers():
    schema = Schema(frozenset([int]))
    with raises(Invalid, 'expected a frozenset'):
        schema(42)
    with raises(Invalid, 'expected a frozenset'):
        schema(set([42]))

    schema(frozenset())
    schema(frozenset([42]))
    schema(frozenset([42, 43, 44]))
    try:
        schema(frozenset(['abc']))
    except MultipleInvalid as e:
        assert_equal(str(e), "invalid value in frozenset")
    else:
        assert False, "Did not raise Invalid"
Beispiel #7
0
def test_SomeOf_min_validation():
    validator = All(Length(min=8), SomeOf(
        min_valid=3,
        validators=[Match(r'.*[A-Z]', 'no uppercase letters'),
                    Match(r'.*[a-z]', 'no lowercase letters'),
                    Match(r'.*[0-9]', 'no numbers'),
                    Match(r'.*[$@$!%*#?&^:;/<,>|{}()\-\'._+=]', 'no symbols')]))

    validator('ffe532A1!')
    with raises(MultipleInvalid, 'length of value must be at least 8'):
        validator('a')

    with raises(MultipleInvalid, 'no uppercase letters, no lowercase letters'):
        validator('wqs2!#s111')

    with raises(MultipleInvalid, 'no lowercase letters, no symbols'):
        validator('3A34SDEF5')
Beispiel #8
0
def test_SomeOf_min_validation():
    validator = All(Length(min=8), SomeOf(
        min_valid=3,
        validators=[Match(r'.*[A-Z]', 'no uppercase letters'),
                    Match(r'.*[a-z]', 'no lowercase letters'),
                    Match(r'.*[0-9]', 'no numbers'),
                    Match(r'.*[$@$!%*#?&^:;/<,>|{}()\-\'._+=]', 'no symbols')]))

    validator('ffe532A1!')
    with raises(MultipleInvalid, 'length of value must be at least 8'):
        validator('a')

    with raises(MultipleInvalid, 'no uppercase letters, no lowercase letters'):
        validator('wqs2!#s111')

    with raises(MultipleInvalid, 'no lowercase letters, no symbols'):
        validator('3A34SDEF5')
Beispiel #9
0
def test_maybe_returns_custom_subvalidator_error():
    schema = Schema(Maybe(validator_value_not_3))

    # The following should be valid
    schema(None)
    schema(1)

    with raises(MultipleInvalid, "Should not be 3"):
        assert schema(3)
Beispiel #10
0
def test_maybe_returns_subvalidator_error():
    schema = Schema(Maybe(Range(1, 2)))

    # The following should be valid
    schema(None)
    schema(1)
    schema(2)

    with raises(MultipleInvalid, "value must be at most 2"):
        assert schema(3)
Beispiel #11
0
def test_SomeOf_max_validation():
    validator = SomeOf(
        max_valid=2,
        validators=[Match(r'.*[A-Z]', 'no uppercase letters'),
                    Match(r'.*[a-z]', 'no lowercase letters'),
                    Match(r'.*[0-9]', 'no numbers')],
        msg='max validation test failed')

    validator('Aa')
    with raises(TooManyValid, 'max validation test failed'):
        validator('Aa1')
Beispiel #12
0
def test_SomeOf_max_validation():
    validator = SomeOf(
        max_valid=2,
        validators=[Match(r'.*[A-Z]', 'no uppercase letters'),
                    Match(r'.*[a-z]', 'no lowercase letters'),
                    Match(r'.*[0-9]', 'no numbers')],
        msg='max validation test failed')

    validator('Aa')
    with raises(TooManyValid, 'max validation test failed'):
        validator('Aa1')
Beispiel #13
0
def test_set_of_integers_and_strings():
    schema = Schema({int, str})
    with raises(Invalid, 'expected a set'):
        schema(42)

    schema(set())
    schema(set([42]))
    schema(set(['abc']))
    schema(set([42, 'abc']))
    try:
        schema(set([None]))
    except MultipleInvalid as e:
        assert_equal(str(e), "invalid value in set")
    else:
        assert False, "Did not raise Invalid"
Beispiel #14
0
def test_set_of_integers_and_strings():
    schema = Schema({int, str})
    with raises(Invalid, 'expected a set'):
        schema(42)

    schema(set())
    schema(set([42]))
    schema(set(['abc']))
    schema(set([42, 'abc']))
    try:
        schema(set([None]))
    except MultipleInvalid as e:
        assert_equal(str(e), "invalid value in set")
    else:
        assert False, "Did not raise Invalid"
Beispiel #15
0
def test_frozenset_of_integers_and_strings():
    schema = Schema(frozenset([int, str]))
    with raises(Invalid, 'expected a frozenset'):
        schema(42)

    schema(frozenset())
    schema(frozenset([42]))
    schema(frozenset(['abc']))
    schema(frozenset([42, 'abc']))
    try:
        schema(frozenset([None]))
    except MultipleInvalid as e:
        assert str(e) == "invalid value in frozenset"
    else:
        assert False, "Did not raise Invalid"
Beispiel #16
0
def test_any_with_discriminant():
    schema = Schema({
        'implementation': Union({
            'type': 'A',
            'a-value': str,
        }, {
            'type': 'B',
            'b-value': int,
        }, {
            'type': 'C',
            'c-value': bool,
        }, discriminant=lambda value, alternatives: filter(lambda v: v['type'] == value['type'], alternatives))
    })

    with raises(MultipleInvalid, "expected bool for dictionary value @ data[\'implementation\'][\'c-value\']"):
        assert schema({
            'implementation': {
                'type': 'C',
                'c-value': None
            }
        })
Beispiel #17
0
def test_SomeOf_on_bounds_assertion():
    with raises(
            AssertionError,
            'when using "SomeOf" you should specify at least one of min_valid and max_valid'
    ):
        SomeOf(validators=[])
Beispiel #18
0
def test_maybe_accepts_msg():
    s = Schema(Maybe(int, msg='int or None expected'))
    with raises(MultipleInvalid, 'int or None expected'):
        assert s([])
Beispiel #19
0
def test_SomeOf_on_bounds_assertion():
    with raises(AssertionError, 'when using "SomeOf" you should specify at least one of min_valid and max_valid'):
        SomeOf(validators=[])
Beispiel #20
0
def test_maybe_accepts_msg():
    s = Schema(Maybe(int, msg='int or None expected'))
    with raises(MultipleInvalid, 'int or None expected'):
        assert s([])