コード例 #1
0
    def test_can_return_errors_from_results(self):
        schema = SchemaValidator(exception_if_invalid=False)
        schema.add('first', IntegerValidator(min=10, exception_if_invalid=False))
        schema.add('second', IntegerValidator(max=10, exception_if_invalid=False))

        input_ = {'first': '5', 'second': '20'}
        result = schema.process(input_)

        assert_true(result.contains_errors())
        first_errors = result.errors['first']
        assert_length(1, first_errors)
        assert_equals('too_low', first_errors[0].key)
        second_errors = result.errors['second']
        assert_length(1, second_errors)
        assert_equals('too_big', second_errors[0].key)
コード例 #2
0
    def test_can_gather_empty_errors_correctly(self):
        schema = SchemaValidator(exception_if_invalid=False)
        schema.add('first', IntegerValidator(required=True, exception_if_invalid=False))
        schema.add('second', IntegerValidator(required=False, exception_if_invalid=False))

        input_ = OrderedDict((('first', ''), ('second', '')))
        result = schema.process(input_)

        assert_true(result.contains_errors())
        first = result.children['first']
        assert_true(first.contains_errors(),
            message='"first" is required and got an empty string as input')
        assert_equals(('empty',), error_keys(first.errors))
        second = result.children['second']
        assert_false(second.contains_errors())
コード例 #3
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())
コード例 #4
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]})
コード例 #5
0
 def setUp(self):
     schema = PositionalArgumentsParsingSchema()
     schema.set_internal_state_freeze(False)
     schema.add('id', IntegerValidator())
     schema.set_parameter_order(['id'])
     schema.set_internal_state_freeze(True)
     # the helper methods will use this private attribute
     self._validator = schema
コード例 #6
0
    def test_can_gather_return_values_and_exceptions(self):
        schema = SchemaValidator(exception_if_invalid=False)
        schema.add('first', IntegerValidator(min=10, exception_if_invalid=True))
        schema.add('second', IntegerValidator(max=10, exception_if_invalid=False))

        input_ = {'first': '5', 'second': '20'}
        result = schema.process(input_)

        assert_true(result.contains_errors())
        assert_is_not_empty(result.errors)
        first_errors = result.errors['first']
        assert_length(1, first_errors)
        assert_equals('too_low', first_errors[0].key)
        assert_true(first_errors[0].is_critical)
        second_errors = result.errors['second']
        assert_length(1, second_errors)
        assert_equals('too_big', second_errors[0].key)
        assert_false(second_errors[0].is_critical)
コード例 #7
0
    def test_can_handle_non_input(self):
        schema = SchemaValidator(exception_if_invalid=False)
        schema.add('first', IntegerValidator(min=10, exception_if_invalid=True))

        result = schema.process(None)
        assert_true(result.contains_errors())
        assert_equals({'first': None}, result.value)
        assert_equals({'first': None}, result.initial_value)
        assert_equals(('empty',), error_keys(result.errors['first']))
コード例 #8
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))
コード例 #9
0
 def _schema(self, fields=('id',), formvalidators=(), **kwargs):
     schema = SchemaValidator(**kwargs)
     assert set(fields).issubset(set(('id', 'key')))
     if 'id' in fields:
         schema.add('id', IntegerValidator())
     if 'key' in fields:
         schema.add('key', StringValidator())
     for formvalidator in formvalidators:
         schema.add_formvalidator(formvalidator)
     return schema
コード例 #10
0
    def test_can_return_error_for_invalid_input_types(self):
        schema = SchemaValidator(exception_if_invalid=False)
        schema.add('first', IntegerValidator(min=10, exception_if_invalid=True))

        result = schema.process([])
        assert_true(result.contains_errors())
        assert_length(0, result.errors.get('first', ()))
        assert_length(1, result.global_errors)
        error = result.global_errors[0]
        assert_equals('invalid_type', error.key)
        assert_true(error.is_critical)
コード例 #11
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)
コード例 #12
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))
コード例 #13
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)
コード例 #14
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))
コード例 #15
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])
コード例 #16
0
 class PassValuesSchema(SchemaValidator):
     filter_unvalidated_parameters=False
     id = IntegerValidator()
コード例 #17
0
class OhlohWidgetGroupParameters(VarArgsParsingSchema):

    parameter_order = ('project_id', 'widget_names')
    project_id = IntegerValidator()
    widget_names = Validator()
コード例 #18
0
class OhlohWidgetParameters(CommaSeparatedArgumentsParsingSchema):

    parameter_order = ('project_id', 'widget_name')
    project_id = IntegerValidator()
    widget_name = StringValidator()
コード例 #19
0
 class MultipleParametersSchema(PositionalArgumentsParsingSchema):
     foo = StringValidator()
     bar = IntegerValidator()
     parameter_order = ('foo', 'bar')
コード例 #20
0
 def _invalid_message(self):
     return IntegerValidator().message('invalid_number', {})
コード例 #21
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, ))
コード例 #22
0
 class DeclarativeSchema(SchemaValidator):
     allow_additional_parameters = False
     
     id = IntegerValidator()
     amount = IntegerValidator
     formvalidators = (Validator(), )
コード例 #23
0
 def test_can_add_validators(self):
     schema = SchemaValidator()
     id_validator = IntegerValidator()
     schema.add('id', id_validator)
     assert_equals({'id': id_validator}, schema.fieldvalidators())
コード例 #24
0
 def test_can_process_multiple_values(self):
     schema = self._schema()
     schema.add('amount', IntegerValidator())
     assert_equals({'id': 42, 'amount': 21},
                   schema.process({'id': '42', 'amount': '21'}))
コード例 #25
0
 def test_use_empty_value_from_validator_for_missing_fields(self):
     schema = SchemaValidator()
     schema.add('id', IntegerValidator(required=False))
     assert_equals({'id': None}, schema.process({}))
コード例 #26
0
 class VarArgsSchema(VarArgsParsingSchema):
     parameter_order = ('id', 'items')
     id = IntegerValidator()
     items = Validator()