예제 #1
0
    def test_list(self):  # type: () -> None
        schema = List(UnicodeString(), min_length=4, max_length=8)

        assert schema.errors(['foo', 'bar', 'baz', 'qux']) == []
        assert schema.errors(['foo', 'bar',
                              'baz']) == [Error('List is shorter than 4')]
        assert schema.errors(
            ['foo', 'bar', 'baz', 'qux', 'foo', 'bar', 'baz', 'qux', 'foo'
             ], ) == [Error('List is longer than 8')]

        with pytest.raises(ValueError):
            List(UnicodeString(), min_length=21, max_length=20)
    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
        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]
예제 #4
0
    def test_list(self):  # type: () -> None
        with pytest.raises(TypeError):
            # noinspection PyTypeChecker
            List(UnicodeString(),
                 additional_validator='Not a validator')  # type: ignore

        field = List(UnicodeString())

        assert field.errors(
            ('hello', 'goodbye')) == [Error(message='Not a list')]
        assert field.errors({'hello':
                             'goodbye'}) == [Error(message='Not a list')]
        assert field.errors({'hello',
                             'goodbye'}) == [Error(message='Not a list')]
        assert field.errors(
            ['hello',
             2]) == [Error(message='Not a unicode string', pointer='1')]
        assert field.errors(['hello', 'goodbye']) == []

        class V(AdditionalCollectionValidator[list]):
            def errors(self, value):
                errors = []
                for i, v in enumerate(value):
                    if v > 500:
                        errors.append(
                            Error('Whoop custom error',
                                  pointer='{}'.format(i)))
                return errors

        field = List(Integer(), additional_validator=V())

        assert field.errors([501, 'Not a number dude']) == [
            Error(message='Not an integer', pointer='1')
        ]
        assert field.errors(
            [501, 499]) == [Error(message='Whoop custom error', pointer='0')]
        assert field.errors([500, 499]) == []
예제 #5
0
파일: schemas.py 프로젝트: guoyu07/pysoa
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,
)

JobRequestSchema = Dictionary(
    {
        'control': ControlHeaderSchema,
        'context': ContextHeaderSchema,
        'actions': List(ActionRequestSchema),
    }, )
예제 #6
0
    def test_dictionary_ordering(self):  # type: () -> None
        schema1 = Dictionary(
            OrderedDict((
                ('foo', UnicodeString()),
                ('bar', Boolean()),
                ('baz', List(Integer())),
            )),
            optional_keys=('foo', ),
            description='Hello, world',
        )

        assert schema1.introspect()['contents'] == {
            'baz': List(Integer()).introspect(),
            'foo': UnicodeString().introspect(),
            'bar': Boolean().introspect(),
        }

        assert schema1.introspect()['display_order'] == ['foo', 'bar', 'baz']

        schema2 = schema1.extend(
            OrderedDict((
                ('bar', Integer()),
                ('qux', Set(UnicodeString())),
                ('moon', Tuple(Decimal(), UnicodeString())),
            )))

        assert schema2.introspect()['contents'] == {
            'baz': List(Integer()).introspect(),
            'foo': UnicodeString().introspect(),
            'moon': Tuple(Decimal(), UnicodeString()).introspect(),
            'bar': Integer().introspect(),
            'qux': Set(UnicodeString()).introspect(),
        }

        assert schema2.introspect()['display_order'] == [
            'foo', 'bar', 'baz', 'qux', 'moon'
        ]

        assert not schema1.errors({'bar': True, 'foo': 'Hello', 'baz': [15]})

        errors = schema1.errors({
            'baz': 'Nope',
            'foo': False,
            'bar': ['Heck nope']
        })

        assert errors == [
            Error(code='INVALID',
                  pointer='foo',
                  message='Not a unicode string'),
            Error(code='INVALID', pointer='bar', message='Not a boolean'),
            Error(code='INVALID', pointer='baz', message='Not a list'),
        ]

        assert not schema2.errors(
            {
                'bar': 91,
                'foo': 'Hello',
                'qux': {'Yes'},
                'baz': [15],
                'moon': (decimal.Decimal('15.25'), 'USD')
            }, )

        errors = schema2.errors({
            'baz': 'Nope',
            'foo': False,
            'bar': ['Heck nope'],
            'qux': 'Denied',
            'moon': 72
        })

        assert errors == [
            Error(code='INVALID',
                  pointer='foo',
                  message='Not a unicode string'),
            Error(code='INVALID', pointer='bar', message='Not an integer'),
            Error(code='INVALID', pointer='baz', message='Not a list'),
            Error(code='INVALID',
                  pointer='qux',
                  message='Not a set or frozenset'),
            Error(code='INVALID', pointer='moon', message='Not a tuple'),
        ]
예제 #7
0
    def test_dictionary_extension(self):  # type: () -> None
        schema1 = Dictionary(
            {
                'foo': UnicodeString(),
                'bar': Boolean(),
            },
            optional_keys=('foo', ),
            description='Hello, world',
        )

        schema2 = schema1.extend(
            {
                'bar': Integer(),
                'baz': List(Integer()),
            },
            optional_keys=('baz', ),
        )

        schema3 = schema1.extend(
            {
                'bar': Integer(),
                'baz': List(Integer()),
            },
            optional_keys=('baz', ),
            allow_extra_keys=True,
            description='Goodbye, universe',
            replace_optional_keys=True,
        )

        self.assertEqual(
            Dictionary(
                {
                    'foo': UnicodeString(),
                    'bar': Integer(),
                    'baz': List(Integer()),
                },
                optional_keys=(
                    'foo',
                    'baz',
                ),
                allow_extra_keys=False,
                description='Hello, world',
            ).introspect(),
            schema2.introspect(),
        )

        self.assertEqual(
            Dictionary(
                {
                    'foo': UnicodeString(),
                    'bar': Integer(),
                    'baz': List(Integer()),
                },
                optional_keys=('baz', ),
                allow_extra_keys=True,
                description='Goodbye, universe',
            ).introspect(),
            schema3.introspect(),
        )

        assert 'display_order' not in schema1.introspect()
        assert 'display_order' not in schema2.introspect()
        assert 'display_order' not in schema3.introspect()
예제 #8
0
    def test_complex(self):  # type: () -> None

        schema = Dictionary({
            'child_ids':
            List(Integer(gt=0)),
            'address':
            Dictionary(
                {
                    'line1': UnicodeString(),
                    'line2': UnicodeString(),
                    'city': UnicodeString(),
                    'postcode': UnicodeString(),
                    'state': UnicodeString(),
                    'country': UnicodeString(),
                },
                optional_keys=('line2', 'state'),
            ),
            'unique_things':
            Set(UnicodeString()),
        })

        self.assertEqual(
            schema.errors(None),
            [Error('Not a dict')],
        )

        self.assertEqual(
            sorted(
                schema.errors(
                    {
                        'child_ids': [1, 2, 'ten'],
                        'unsolicited_item': 'Should not be here',
                        'another_bad': 'Also extra',
                        'unique_things': ['hello', 'world'],
                    }, )),
            sorted([
                Error('Not an integer', pointer='child_ids.2'),
                Error('Missing key: address',
                      code=ERROR_CODE_MISSING,
                      pointer='address'),
                Error('Extra keys present: another_bad, unsolicited_item',
                      code=ERROR_CODE_UNKNOWN),
                Error('Not a set or frozenset', pointer='unique_things'),
            ]),
        )

        self.assertEqual(
            schema.errors({
                'child_ids': [1, 2, 3, 4],
                'address': {
                    'line1': '115 5th Street',
                    'city': 'San Francisco',
                    'state': 'CA',
                    'country': 'USA',
                    'postcode': '94103',
                },
                'unique_things': {'hello', b'world'},
            }),
            [
                Error('Not a unicode string',
                      pointer='unique_things.[{}]'.format(str(b'world')))
            ],
        )

        self.assertEqual(
            schema.errors({
                'child_ids': [1, 2, 3, 4],
                'address': {
                    'line1': '115 5th Street',
                    'city': 'San Francisco',
                    'state': 'CA',
                    'country': 'USA',
                    'postcode': '94103',
                },
                'unique_things': {'hello', 'world'},
            }),
            [],
        )

        introspection = schema.introspect()
        self.assertEqual('dictionary', introspection['type'])
        self.assertFalse(introspection['allow_extra_keys'])
        self.assertEqual([], introspection['optional_keys'])
        self.assertEqual(3, len(introspection['contents']))
        self.assertIn('child_ids', introspection['contents'])
        self.assertEqual(
            {
                'type': 'list',
                'contents': {
                    'gt': 0,
                    'type': 'integer'
                },
            },
            introspection['contents']['child_ids'],
        )
        self.assertIn('address', introspection['contents'])
        self.assertEqual('dictionary',
                         introspection['contents']['address']['type'])
        self.assertFalse(
            introspection['contents']['address']['allow_extra_keys'])
        self.assertEqual(
            {'line2', 'state'},
            set(introspection['contents']['address']['optional_keys']))
        self.assertEqual(
            {
                'city': {
                    'type': 'unicode'
                },
                'country': {
                    'type': 'unicode'
                },
                'line1': {
                    'type': 'unicode'
                },
                'line2': {
                    'type': 'unicode'
                },
                'postcode': {
                    'type': 'unicode'
                },
                'state': {
                    'type': 'unicode'
                },
            },
            introspection['contents']['address']['contents'],
        )
        self.assertEqual(
            {
                'type': 'set',
                'contents': {
                    'type': 'unicode'
                },
            },
            introspection['contents']['unique_things'],
        )
예제 #9
0
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,
)

JobRequestSchema = Dictionary(
    {
        'control': ControlHeaderSchema,
        'context': ContextHeaderSchema,
        'actions': List(ActionRequestSchema, min_length=1),
    }, )
예제 #10
0
            'when those requests are made with `request.client.call_**(...)` or '
            '`request.client.send_request(...)`. For example, if Foo Service calls Bar Service, the '
            'request context that Bar Service receives will include `"calling_service": "foo"`. May be '
            'useful for logging or metrics.'),
        'correlation_id':
        UnicodeString(
            description=
            'Correlation IDs can be used at your own discretion, but are generally shared across multiple '
            'service requests, even across multiple services, to correlate requests that are logically '
            'linked together (example: such as all PySOA requests that occur within the scope of a single '
            'HTTP request in a client application). The PySOA client automatically adds a UUID correlation '
            'ID to all outgoing requests if the client is not already configured with an inherited '
            'correlation ID, and the client available in `request.client` automatically inherits the '
            'correlation ID from the request.', ),
        'switches':
        List(Integer(),
             description='See: :ref:`api-versioning-using-switches`.'),
    },
    allow_extra_keys=True,
    optional_keys=('caller', 'calling_service'),
)

JobRequestSchema = Dictionary(
    {
        'control':
        ControlHeaderSchema,
        'context':
        ContextHeaderSchema,
        'actions':
        List(
            ActionRequestSchema,
            min_length=1,
예제 #11
0
    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)
예제 #12
0
from conformity.fields import Dictionary, UnicodeString, List
import json

instance = Dictionary(
    {
        "title": UnicodeString(),
        "url": UnicodeString(),
        "about_url": UnicodeString(),
        "description": UnicodeString(),
        "tags": List(UnicodeString()),
    },
    optional_keys=["description", "tags", "about_url"])
instances = List(instance)


def test_registry():
    data = json.load(open('registry.json'))
    assert [] == instances.errors(data)