コード例 #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_add_additional_validators_to_existing_schema(self):
     schema = self.schema()
     extended_schema = SchemaValidator()
     extended_schema.add('name', StringValidator())
     extended_schema.add_missing_validators(schema)
     
     self.assert_equals(set(['id', 'name']), self.known_fields(extended_schema))
     self.assert_length(1, schema.formvalidators())
コード例 #3
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']))
コード例 #4
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)
コード例 #5
0
    def test_adding_validators_appends_formvalidators(self):
        schema = self.schema()
        extended_schema = SchemaValidator()
        extended_schema.add('id', StringValidator())
        extended_schema.add_formvalidator(StringValidator())
        extended_schema.add_missing_validators(schema)

        assert_length(2, extended_schema.formvalidators())
コード例 #6
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]})
コード例 #7
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))
コード例 #8
0
 def test_adding_validators_appends_formvalidators(self):
     schema = self.schema()
     extended_schema = SchemaValidator()
     extended_schema.add('id', StringValidator())
     extended_schema.add_formvalidator(StringValidator())
     extended_schema.add_missing_validators(schema)
     
     self.assert_length(2, extended_schema.formvalidators())
コード例 #9
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)
コード例 #10
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))
コード例 #11
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])
コード例 #12
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
コード例 #13
0
    def test_existing_keys_are_kept(self):
        schema = self.schema()
        extended_schema = SchemaValidator()
        extended_schema.add('id', StringValidator())
        extended_schema.add_missing_validators(schema)

        assert_equals(set(['id']), self.known_fields(schema))
        assert_isinstance(extended_schema.validator_for('id'), StringValidator)
コード例 #14
0
ファイル: schema_test.py プロジェクト: hhru/pycerberus-deb
 def _schema(self, fields=('id',), formvalidators=()):
     schema = SchemaValidator()
     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
コード例 #15
0
 def test_existing_keys_are_kept(self):
     schema = self.schema()
     extended_schema = SchemaValidator()
     extended_schema.add('id', StringValidator())
     extended_schema.add_missing_validators(schema)
     
     self.assert_equals(set(['id']), self.known_fields(schema))
     self.assert_isinstance(extended_schema.validator_for('id'), StringValidator)
コード例 #16
0
    def test_can_add_additional_validators_to_existing_schema(self):
        schema = self.schema()
        extended_schema = SchemaValidator()
        extended_schema.add('name', StringValidator())
        extended_schema.add_missing_validators(schema)

        assert_equals(set(['id', 'name']), self.known_fields(extended_schema))
        assert_length(1, schema.formvalidators())
コード例 #17
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())
コード例 #18
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)
コード例 #19
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)
コード例 #20
0
 def test_non_dict_inputs_raise_invaliddataerror(self):
     with assert_raises(InvalidDataError):
         SchemaValidator().process('foo')
     exception = assert_raises(InvalidDataError, lambda: SchemaValidator().process([]))
     assert_equals('invalid_type', exception.details().key())
コード例 #21
0
 def test_can_specify_allow_additional_params_at_construction(self):
     schema = SchemaValidator(allow_additional_parameters=False)
     assert_false(schema.allow_additional_parameters)
コード例 #22
0
 def test_can_add_validators(self):
     schema = SchemaValidator()
     id_validator = IntegerValidator()
     schema.add('id', id_validator)
     assert_equals({'id': id_validator}, schema.fieldvalidators())
コード例 #23
0
 def test_process_smoke(self):
     assert_equals({},SchemaValidator().process(None))
     assert_equals({}, SchemaValidator().process({}))
コード例 #24
0
 def test_new_schema_has_no_validators_by_default(self):
     assert_equals({}, SchemaValidator().fieldvalidators())
コード例 #25
0
ファイル: schema_test.py プロジェクト: hhru/pycerberus-deb
 def test_can_add_validators(self):
     schema = SchemaValidator()
     id_validator = IntegerValidator()
     schema.add('id', id_validator)
     self.assert_equals({'id': id_validator}, schema.fieldvalidators())
コード例 #26
0
ファイル: schema_test.py プロジェクト: hhru/pycerberus-deb
 def test_use_empty_value_from_validator_for_missing_fields(self):
     schema = SchemaValidator()
     schema.add('id', IntegerValidator(required=False))
     self.assert_equals({'id': None}, schema.process({}))
コード例 #27
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({}))