Пример #1
0
    def test_can_return_error_list_from_subvalidator(self):
        schema = SchemaValidator(exception_if_invalid=False)
        schema.add('number', IntegerValidator(exception_if_invalid=False))
        validator = ForEach(schema, exception_if_invalid=True)

        with assert_raises(InvalidDataError) as c:
            validator.process([
                {
                    'number': '42'
                },
                {
                    'number': 'invalid'
                },
            ])
        e = c.caught_exception
        list_errors = e.errors()
        assert_length(2, list_errors)
        assert_not_equals((None, None), list_errors)

        assert_none(list_errors[0])
        nr2_error = list_errors[1]
        assert_not_none(nr2_error)
        assert_isinstance(
            nr2_error,
            InvalidDataError,
            message=
            'Schema raises on error so pycerberus only supports one error per field'
        )
        assert_equals('invalid_number', nr2_error.details().key())
Пример #2
0
    def test_can_return_result_for_empty_input_value(self):
        validator = ForEach(IntegerValidator(exception_if_invalid=False),
                            exception_if_invalid=False)
        result = validator.process(None)

        assert_true(result.contains_errors())
        assert_length(1, result.errors)
        assert_length(1, result.global_errors)  # seems to be duplicated?
        assert_equals(('empty', ), error_keys(result.errors))
Пример #3
0
    def test_can_return_results_from_subvalidators(self):
        validator = ForEach(IntegerValidator(exception_if_invalid=False),
                            exception_if_invalid=False)
        result = validator.process(('invalid', ))

        assert_true(result.contains_errors())
        assert_length(1,
                      result.errors)  # maybe 2 when we add global errors here?
        int_errors = result.errors[0]

        assert_length(1, int_errors)
        int_error = int_errors[0]
        assert_equals('invalid_number', int_error.key)
Пример #4
0
    def test_can_return_errors_from_subschemas(self):
        schema = SchemaValidator(exception_if_invalid=False)
        schema.add('foo', IntegerValidator(exception_if_invalid=False))
        foreach = ForEach(schema, exception_if_invalid=False)

        result = foreach.process([
            {
                'foo': 'invalid'
            },
        ])
        assert_true(result.contains_errors())

        assert_length(1, result.errors)
        item_errors = result.errors[0]
        assert_equals(set(('foo', )), set(item_errors))
Пример #5
0
    def test_can_return_result_for_valid_schema_with_foreach_and_formvalidator(self):
        schema = SchemaValidator()
        schema.add('foo', ForEach(IntegerValidator()))
        schema.add_formvalidator(Validator())

        with assert_not_raises():
            schema.process({'foo': [10]})
Пример #6
0
    def test_can_return_exceptions_from_subschemas_as_errors(self):
        schema = SchemaValidator(exception_if_invalid=True)
        schema.add('foo', IntegerValidator(exception_if_invalid=True))
        foreach = ForEach(schema, exception_if_invalid=False)

        with assert_not_raises(
                message='foreach should be able to return results'):
            result = foreach.process([
                {
                    'foo': 'invalid'
                },
            ])
        assert_true(result.contains_errors())
        assert_length(1, result.errors)

        schema_error = result.errors[0]
        assert_equals(set(('foo', )), set(schema_error))
Пример #7
0
    def test_can_return_errors_from_list_children(self):
        schema = SchemaValidator(exception_if_invalid=False)
        foo_list = ForEach(IntegerValidator())
        schema.add('foo', foo_list)

        result = schema.process({'foo': ['abc']})
        assert_true(result.contains_errors())
        assert_equals(('foo',), tuple(result.errors))
        foo_errors = result.errors['foo']

        assert_length(1, foo_errors)
        int_errors = foo_errors[0]
        assert_length(1, int_errors)
        int_error = int_errors[0]
        assert_isinstance(int_error, Error)
        assert_equals('invalid_number', int_error.key)
Пример #8
0
    def test_can_return_errors_from_raising_list_children(self):
        schema = SchemaValidator(exception_if_invalid=False)
        # exception_if_invalid=True is important here to trigger the bug in
        # error conversion...
        foo_list = ForEach(IntegerValidator(), exception_if_invalid=True)
        schema.add('foo', foo_list)

        # Test case: multiple items for the list child but only one contains an
        # error
        result = schema.process({'foo': ['bar', '1']})
        assert_true(result.contains_errors())
        assert_equals(('foo',), tuple(result.errors))
        foo_errors = result.errors['foo']
        assert_length(2, foo_errors)

        assert_length(1, foo_errors[0])
        assert_none(foo_errors[1])
Пример #9
0
 def test_returns_empty_tuple_if_not_required_and_no_value_given(self):
     validator = ForEach(IntegerValidator, required=False)
     assert_equals((), validator.empty_value({}))
Пример #10
0
 def test_min_length_must_be_smaller_or_equal_max_length(self):
     with assert_raises(InvalidArgumentsError):
         ForEach(IntegerValidator, min_length=2, max_length=1)
Пример #11
0
 def test_can_specify_min_length_and_max_lenght(self):
     self.init_validator(
         ForEach(IntegerValidator, min_length=1, max_length=1))
     self.process((21, ))
     self.assert_raises_error_with_key((), 'too_short')
     self.assert_raises_error_with_key((21, 42), 'too_long')
Пример #12
0
    def test_can_specify_max_amount_of_items(self):
        self.init_validator(ForEach(IntegerValidator, max_length=2))
        self.process((21, 42))

        self.assert_raises_error_with_key((21, 42, 63), 'too_long')
Пример #13
0
    def test_can_specify_min_amount_of_items(self):
        self.init_validator(ForEach(IntegerValidator, min_length=2))
        self.process((21, 42))

        self.assert_raises_error_with_key((), 'too_short')
        self.assert_raises_error_with_key((21, ), 'too_short')
Пример #14
0
 def test_works_with_validator_instances(self):
     # This is important if validators need some configuration in the
     # constructor
     self.init_validator(ForEach(IntegerValidator()))
     self.process((42, ))
Пример #15
0
 def test_can_set_validator_arguments_for_constructor(self):
     self.init_validator(
         ForEach(IntegerValidator, default=[], required=False))
     assert_equals([], self.process(None))