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())
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())
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']))
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)
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())
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]})
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))
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())
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)
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))
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])
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
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)
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
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)
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())
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())
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)
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)
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())
def test_can_specify_allow_additional_params_at_construction(self): schema = SchemaValidator(allow_additional_parameters=False) assert_false(schema.allow_additional_parameters)
def test_can_add_validators(self): schema = SchemaValidator() id_validator = IntegerValidator() schema.add('id', id_validator) assert_equals({'id': id_validator}, schema.fieldvalidators())
def test_process_smoke(self): assert_equals({},SchemaValidator().process(None)) assert_equals({}, SchemaValidator().process({}))
def test_new_schema_has_no_validators_by_default(self): assert_equals({}, SchemaValidator().fieldvalidators())
def test_can_add_validators(self): schema = SchemaValidator() id_validator = IntegerValidator() schema.add('id', id_validator) self.assert_equals({'id': id_validator}, schema.fieldvalidators())
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({}))
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({}))