def test_schemaless_dict(self): """ Tests the schemaless dict with some schema """ schema = SchemalessDictionary(Integer(), UnicodeString()) self.assertEqual(schema.errors({1: u'value'}), []) self.assertEqual( schema.errors({'x': 123}), [ Error('Not an integer', pointer='x'), Error('Not a unicode string', pointer='x'), ], ) self.assertEqual( schema.introspect(), { 'type': 'schemaless_dictionary', 'key_type': { 'type': 'integer' }, 'value_type': { 'type': 'unicode' }, })
def test_schemaless_dict_empty(self): # type: () -> None """ Tests the schemaless dict without any schema at all (so the default Hashable: Anything) """ schema = SchemalessDictionary() self.assertEqual(schema.errors({'key': 'value'}), []) self.assertEqual(schema.errors('a thing'), [Error('Not a dict')]) self.assertEqual(schema.introspect(), { 'type': 'schemaless_dictionary', })
def test_python_path(self): # type: () -> None schema = PythonPath() assert schema.errors('tests.test_fields_meta.MY_FOO') == [] assert schema.errors('tests.test_fields_meta.MY_BAR') == [] assert schema.errors('tests.test_fields_meta:MY_QUX') == [] assert schema.errors('tests.test_fields_meta.MY_DICT') == [] assert schema.errors('tests.test_fields_meta:Qux.INNER_CONSTANT') == [] schema = PythonPath(ObjectInstance(Foo)) assert schema.errors('tests.test_fields_meta.MY_FOO') == [] assert schema.errors('tests.test_fields_meta.MY_BAR') == [] assert schema.errors('tests.test_fields_meta:MY_QUX') == [ Error('Not an instance of Foo') ] assert schema.errors('tests.test_fields_meta.MY_DICT') == [ Error('Not an instance of Foo') ] assert schema.errors('tests.test_fields_meta:Qux.INNER_CONSTANT') == [ Error('Not an instance of Foo') ] schema = PythonPath(SchemalessDictionary()) assert schema.errors('tests.test_fields_meta.MY_DICT') == [] assert schema.errors('tests.test_fields_meta:MY_QUX') == [ Error('Not a dict') ] schema = PythonPath(UnicodeString()) assert schema.errors('tests.test_fields_meta:Qux.INNER_CONSTANT') == [] assert schema.errors('tests.test_fields_meta.MY_DICT') == [ Error('Not a unicode string') ]
def test_validator_arguments_validation(self): # type: () -> None with pytest.raises(ValueError): @validate_call(List(UnicodeString()), UnicodeString()) # type: ignore def something(_foo): pass with pytest.raises(ValueError): @validate_call(args=SchemalessDictionary(), kwargs=None, returns=UnicodeString()) # type: ignore def something_else(_foo): pass
def test_schemaless_dict(self): # type: () -> None """ Tests the schemaless dict with some schema """ schema = SchemalessDictionary(Integer(), UnicodeString(), min_length=1, max_length=5) self.assertEqual(schema.errors({1: 'value'}), []) assert schema.errors( {}) == [Error('Dict contains fewer than 1 value(s)')] self.assertEqual( schema.errors({ 'x': 123, 2: 'foo', 3: 'bar', 4: 'baz', 5: 'qux', 6: 'too many' }), [ Error('Dict contains more than 5 value(s)'), Error('Not an integer', pointer='x'), Error('Not a unicode string', pointer='x'), ], ) self.assertEqual( schema.introspect(), { 'type': 'schemaless_dictionary', 'key_type': { 'type': 'integer' }, 'value_type': { 'type': 'unicode' }, 'max_length': 5, 'min_length': 1, }) with pytest.raises(ValueError): SchemalessDictionary(Integer(), UnicodeString(), min_length=12, max_length=11)
class Helper(object): @classmethod @validate_method(schema, UnicodeString()) def greeter(cls, name, greeting='Hello'): # Special case to check return value stuff if name == 'error': return 5 return '{}, {}!'.format(greeting, name) @staticmethod @validate_call(args=Tuple(Integer(), Integer()), kwargs=None, returns=Integer()) def args_method(one, two): return one + two # noinspection PyMethodMayBeStatic @validate_method( args=List(UnicodeString()), kwargs=SchemalessDictionary(value_type=UnicodeString()), returns=List(UnicodeString()), ) def args_and_kwargs_method(self, *args, **kwargs): return [s.format(**kwargs) for s in args]
from conformity.fields import ( Boolean, Dictionary, Integer, List, SchemalessDictionary, UnicodeString, ) ActionRequestSchema = Dictionary( { 'action': UnicodeString(), 'body': SchemalessDictionary(key_type=UnicodeString()), }, optional_keys=['body'], ) ControlHeaderSchema = Dictionary( { 'continue_on_error': Boolean(), }, allow_extra_keys=True, ) ContextHeaderSchema = Dictionary( { 'switches': List(Integer()), 'correlation_id': UnicodeString(), }, allow_extra_keys=True, )
'qux': Boolean() }, optional_keys=('qux', )), ) class AnotherSomething(BaseSomething): def __init__(self, baz, qux='unset'): self.baz = baz self.qux = qux class ExtendedAnotherSomething(AnotherSomething): pass @ClassConfigurationSchema.provider( Dictionary({ 'no_baz': Nullable(UnicodeString()), 'no_qux': Boolean() }, optional_keys=('no_qux', )), ) class OverridingAnotherSomething(AnotherSomething): def __init__(self, no_baz, no_qux='no_unset'): super(OverridingAnotherSomething, self).__init__(baz=no_baz, qux=no_qux) @ClassConfigurationSchema.provider( SchemalessDictionary(key_type=UnicodeString(), value_type=Any())) class SomethingWithJustKwargs(BaseSomething): def __init__(self, **kwargs): self.kwargs = kwargs
__all__ = ( 'ActionRequestSchema', 'ContextHeaderSchema', 'ControlHeaderSchema', 'JobRequestSchema', ) ActionRequestSchema = Dictionary( { 'action': UnicodeString( description='The name of the service action to execute.'), 'body': SchemalessDictionary( key_type=UnicodeString(), description='The request parameters for this action.'), }, optional_keys=('body', ), ) """The Conformity schema with which action requests are validated.""" ControlHeaderSchema = Dictionary( { 'continue_on_error': Boolean( description= 'Whether to continue executing more actions in a multi-action job request if an action ' 'results in an error.', ), 'suppress_response': Boolean(
def test_schemaless_dictionary(self): with pytest.raises(TypeError): # noinspection PyTypeChecker SchemalessDictionary( key_type=UnicodeString(), value_type=Integer(), additional_validator='Not a validator', # type: ignore ) field = SchemalessDictionary(key_type=UnicodeString(), value_type=Integer()) assert field.errors(['foo', 'bar', 'baz']) == [Error(message='Not a dict')] assert field.errors( ('foo', 'bar', 'baz')) == [Error(message='Not a dict')] assert field.errors({'foo', 'bar', 'baz'}) == [Error(message='Not a dict')] assert field.errors({ 'foo': 42, 'bar': 11, 'baz': 'Goodbye' }) == [ Error(message='Not an integer', pointer='baz'), ] assert field.errors({'foo': 42, 'bar': 11, 'baz': 91}) == [] class V(AdditionalCollectionValidator[Mapping[HashableType, AnyType]]): def errors(self, value): if value['foo'] != value['baz']: return [ Error('Value foo does not match value baz', pointer='foo') ] return [] field = SchemalessDictionary(key_type=UnicodeString(), value_type=Integer(), additional_validator=V()) assert field.errors({ 'foo': 42, 'bar': 11, 'baz': 91 }) == [ Error(message='Value foo does not match value baz', pointer='foo'), ] assert field.errors({'foo': 42, 'bar': 11, 'baz': 42}) == []
def test_validate_method(self): # type: () -> None schema = Dictionary({ 'name': UnicodeString(max_length=20), 'greeting': UnicodeString(), }, optional_keys=('greeting', )) class Helper(object): @classmethod @validate_method(schema, UnicodeString()) def greeter(cls, name, greeting='Hello'): # Special case to check return value stuff if name == 'error': return 5 return '{}, {}!'.format(greeting, name) @staticmethod @validate_call(args=Tuple(Integer(), Integer()), kwargs=None, returns=Integer()) def args_method(one, two): return one + two # noinspection PyMethodMayBeStatic @validate_method( args=List(UnicodeString()), kwargs=SchemalessDictionary(value_type=UnicodeString()), returns=List(UnicodeString()), ) def args_and_kwargs_method(self, *args, **kwargs): return [s.format(**kwargs) for s in args] assert getattr(Helper.greeter, '__validated__') is True assert getattr(Helper.greeter, '__validated_schema_args__') is None assert getattr(Helper.greeter, '__validated_schema_kwargs__') is schema assert getattr(Helper.greeter, '__validated_schema_returns__') == UnicodeString() assert getattr(Helper.args_method, '__validated__') is True assert getattr(Helper.args_method, '__validated_schema_args__') == Tuple(Integer(), Integer()) assert getattr(Helper.args_method, '__validated_schema_kwargs__') is None assert getattr(Helper.args_method, '__validated_schema_returns__') == Integer() assert getattr(Helper.args_and_kwargs_method, '__validated__') is True assert getattr(Helper.args_and_kwargs_method, '__validated_schema_args__') == List(UnicodeString()) assert ( getattr(Helper.args_and_kwargs_method, '__validated_schema_kwargs__') == SchemalessDictionary(value_type=UnicodeString()) ) assert getattr(Helper.args_and_kwargs_method, '__validated_schema_returns__') == List(UnicodeString()) self.assertEqual(Helper.greeter(name='Andrew'), 'Hello, Andrew!') self.assertEqual(Helper.greeter(name='Andrew', greeting='Ahoy'), 'Ahoy, Andrew!') with self.assertRaises(ValidationError): Helper.greeter(name='Andrewverylongnameperson') with self.assertRaises(ValidationError): Helper.greeter(name='Andrew', greeeeeeting='Boo') with self.assertRaises(ValidationError): Helper.greeter(name='error') with self.assertRaises(PositionalError): Helper.greeter('Andrew') assert Helper.args_method(1, 2) == 3 assert Helper.args_method(75, 23) == 98 with pytest.raises(ValidationError): Helper.args_method(1.0, 2) with pytest.raises(ValidationError): Helper.args_method(1, 2.0) with pytest.raises(KeywordError): Helper.args_method(1, 2, extra='Forbidden') assert Helper().args_and_kwargs_method('hello', 'cool {planet}', 'hot {star}', planet='Earth', star='Sun') == [ 'hello', 'cool Earth', 'hot Sun', ] with pytest.raises(ValidationError): Helper().args_and_kwargs_method(1, 'sweet', planet='Earth', star='Sun') with pytest.raises(ValidationError): Helper().args_and_kwargs_method('hello', 'cool {planet}', 'hot {star}', planet=1, star=2)