Exemplo n.º 1
0
    def test_passing_context_to_predicate(self):
        class NonLocal:
            context = None

        def validator(value, context=None):
            NonLocal.context = context
            return True

        my_context = object()
        Predicate(validator)('foo', my_context)
        assert NonLocal.context == my_context
Exemplo n.º 2
0
 def test_customizing_validation_error(self):
     message = 'Invalid data'
     with raises(ValidationError) as exc_info:
         Predicate(lambda x: x in ['foo', 'bar'], message)('baz')
     assert exc_info.value.messages == message
Exemplo n.º 3
0
    def test_matching_values(self):
        with not_raises(ValidationError):
            Predicate(lambda x: x in ['foo', 'bar', 'baz'])('foo')

        with not_raises(ValidationError):
            Predicate(lambda x: x in ['foo', 'bar', 'baz'])('bar')
Exemplo n.º 4
0
 def test_raising_ValidationError_if_predicate_returns_False(self):
     with raises(ValidationError) as exc_info:
         Predicate(lambda x: x in ['foo', 'bar'])('baz')
     assert exc_info.value.messages == Predicate.default_error_messages[
         'invalid']
Exemplo n.º 5
0
            'unique']

    def test_customizing_error_message(self):
        class Foo:
            def __init__(self, foo):
                self.foo = foo

        message = 'Invalid data {data} with key {key}'
        x = Foo('foo')
        y = Foo('foo')
        with raises(ValidationError) as exc_info:
            Unique(lambda x: x.foo, error=message)([x, y])
        assert exc_info.value.messages == message.format(data=y, key='foo')


is_odd = Predicate(lambda x: x % 2 == 1, 'Value should be odd')
is_small = Predicate(lambda x: x <= 5, 'Value should be small')


class TestEach:
    def test_raising_ValidationError_if_value_is_not_collection(self):
        with raises(ValidationError) as exc_info:
            Each(lambda x: x)('foo')
        assert exc_info.value.messages == Each.default_error_messages[
            'invalid']

    def test_matching_empty_collections(self):
        with not_raises(ValidationError):
            Each(is_odd)([])

    def test_matching_collections_each_elemenet_of_which_matches_given_validators(