def test_all(self):  # type: () -> None
        schema = All(Constant('one'), UnicodeString())
        self.assertEqual(
            schema.errors('one'),
            [],
        )
        self.assertEqual(
            len(schema.errors('two')),
            1,
        )

        assert schema.introspect() == {
            'type':
            'all',
            'requirements': [
                {
                    'type': 'constant',
                    'values': ['one']
                },
                {
                    'type': 'unicode'
                },
            ]
        }

        with pytest.raises(TypeError):
            # noinspection PyTypeChecker
            All('not a field')  # type: ignore

        with pytest.raises(TypeError):
            All(Constant('one'), UnicodeString(), description=b'Not unicode')

        with pytest.raises(TypeError):
            All(Constant('one'), UnicodeString(), unsupported='argument')
Example #2
0
    def test_nullable(self):
        constant = Constant('one', 'two')
        schema = Nullable(constant)
        self.assertEqual([], schema.errors(None))
        self.assertEqual([], schema.errors('one'))
        self.assertEqual([], schema.errors('two'))
        self.assertEqual(1, len(schema.errors('three')))
        self.assertEqual(
            {
                'type': 'nullable',
                'nullable': constant.introspect()
            }, schema.introspect())

        boolean = Boolean(description='This is a test description')
        schema = Nullable(boolean)
        self.assertEqual([], schema.errors(None))
        self.assertIsNone(schema.errors(True))
        self.assertIsNone(schema.errors(False))
        self.assertEqual(1, len(schema.errors('true')))
        self.assertEqual(1, len(schema.errors(1)))
        self.assertEqual({
            'type': 'nullable',
            'nullable': boolean.introspect()
        }, schema.introspect())

        string = UnicodeString()
        schema = Nullable(string)
        self.assertEqual([], schema.errors(None))
        self.assertIsNone(schema.errors('hello, world'))
        self.assertEqual(1, len(schema.errors(b'hello, world')))
        self.assertEqual({
            'type': 'nullable',
            'nullable': string.introspect()
        }, schema.introspect())
Example #3
0
 def test_any(self):
     schema = Any(Constant('one'), Constant('two'))
     self.assertEqual(
         schema.errors('one'),
         [],
     )
     self.assertEqual(
         schema.errors('two'),
         [],
     )
     self.assertEqual(
         len(schema.errors('three')),
         2,
     )
Example #4
0
    def test_multi_constant(self):  # type: () -> None
        """
        Tests constants with multiple options
        """
        schema = Constant(42, 36, 81, 9231)
        self.assertEqual(
            schema.errors(9231),
            [],
        )
        self.assertEqual(
            schema.errors(81),
            [],
        )
        self.assertEqual(
            schema.errors(360000),
            [
                Error('Value is not one of: 36, 42, 81, 9231',
                      code=ERROR_CODE_UNKNOWN)
            ],
        )

        with pytest.raises(TypeError):
            Constant(42, 36, 81, 9231, description='foo', unsupported='bar')

        with pytest.raises(ValueError):
            Constant()

        with pytest.raises(TypeError):
            Constant(42, 36, 81, 9231, description=b'not unicode')
Example #5
0
 def test_all(self):
     schema = All(Constant('one'), UnicodeString())
     self.assertEqual(
         schema.errors('one'),
         [],
     )
     self.assertEqual(
         len(schema.errors('two')),
         1,
     )
Example #6
0
 def test_multi_constant(self):
     """
     Tests constants with multiple options
     """
     schema = Constant(42, 36, 81, 9231)
     self.assertEqual(
         schema.errors(9231),
         [],
     )
     self.assertEqual(
         schema.errors(81),
         [],
     )
     self.assertEqual(
         schema.errors(360000),
         [
             Error('Value is not one of: 36, 42, 81, 9231',
                   code=ERROR_CODE_UNKNOWN)
         ],
     )
Example #7
0
    def test_tuple(self):
        schema = Tuple(Integer(gt=0), UnicodeString(),
                       Constant('I love tuples'))

        self.assertEqual(schema.errors((1, 'test', 'I love tuples')), [])

        # too short
        self.assertEqual(
            schema.errors((1, 'test')),
            [Error('Number of elements 2 does not match expected 3')])

        # too long
        self.assertEqual(
            schema.errors((1, 'test', 'I love tuples', '... and coffee')),
            [Error('Number of elements 4 does not match expected 3')])

        self.assertEqual(schema.errors((
            -1,
            None,
            'I hate tuples',
        )), [
            Error('Value not > 0', pointer='0'),
            Error('Not a unicode string', pointer='1'),
            Error(
                'Value is not "I love tuples"',
                code=ERROR_CODE_UNKNOWN,
                pointer='2',
            ),
        ])

        self.assertEqual(
            schema.introspect(), {
                'type':
                'tuple',
                'contents': [
                    {
                        'type': 'integer',
                        'gt': 0
                    },
                    {
                        'type': 'unicode'
                    },
                    {
                        'type': 'constant',
                        'values': ['I love tuples']
                    },
                ]
            })
    def test_any(self):  # type: () -> None
        schema = Any(Constant('one'), Constant('two'))
        self.assertEqual(
            schema.errors('one'),
            [],
        )
        self.assertEqual(
            schema.errors('two'),
            [],
        )
        self.assertEqual(
            len(schema.errors('three')),
            2,
        )

        assert schema.introspect() == {
            'type':
            'any',
            'options': [
                {
                    'type': 'constant',
                    'values': ['one']
                },
                {
                    'type': 'constant',
                    'values': ['two']
                },
            ]
        }

        with pytest.raises(TypeError):
            # noinspection PyTypeChecker
            Any('not a field')  # type: ignore

        with pytest.raises(TypeError):
            Any(Constant('one'), Constant('two'), description=b'Not unicode')

        with pytest.raises(TypeError):
            Any(Constant('one'), Constant('two'), unsupported='argument')
Example #9
0
    def test_polymorph(self):

        card = Dictionary({
            'payment_type':
            Constant('card'),
            'number':
            UnicodeString(),
            'cvc':
            UnicodeString(description='Card Verification Code'),
        })

        bankacc = Dictionary({
            'payment_type':
            Constant('bankacc'),
            'routing':
            UnicodeString(description='US RTN or foreign equivalent'),
            'account':
            UnicodeString(),
        })

        schema = Polymorph(
            'payment_type',
            {
                'card': card,
                'bankacc': bankacc,
            },
        )

        self.assertEqual(
            schema.errors({
                'payment_type': 'card',
                'number': '1234567890123456',
                'cvc': '000',
            }),
            [],
        )

        self.assertEqual(
            schema.errors({
                'payment_type': 'bankacc',
                'routing': '13456790',
                'account': '13910399',
            }),
            [],
        )

        self.assertEqual(
            schema.introspect(),
            {
                'type': 'polymorph',
                'contents_map': {
                    'bankacc': {
                        'type': 'dictionary',
                        'allow_extra_keys': False,
                        'contents': {
                            'account': {
                                'type': 'unicode'
                            },
                            'payment_type': {
                                'type': 'constant',
                                'values': ['bankacc'],
                            },
                            'routing': {
                                'type': 'unicode',
                                'description': 'US RTN or foreign equivalent',
                            },
                        },
                        'optional_keys': [],
                    },
                    'card': {
                        'type': 'dictionary',
                        'allow_extra_keys': False,
                        'contents': {
                            'cvc': {
                                'type': 'unicode',
                                'description': 'Card Verification Code',
                            },
                            'number': {
                                'type': 'unicode'
                            },
                            'payment_type': {
                                'type': 'constant',
                                'values': ['card'],
                            },
                        },
                        'optional_keys': [],
                    },
                },
                'switch_field': 'payment_type',
            },
        )
Example #10
0
    def test_tuple(self):  # type: () -> None
        schema = Tuple(Integer(gt=0), UnicodeString(),
                       Constant('I love tuples'))

        self.assertEqual(schema.errors((1, 'test', 'I love tuples')), [])

        # too short
        self.assertEqual(
            schema.errors((1, 'test')),
            [Error('Number of elements 2 does not match expected 3')])

        # too long
        self.assertEqual(
            schema.errors((1, 'test', 'I love tuples', '... and coffee')),
            [Error('Number of elements 4 does not match expected 3')])

        self.assertEqual(schema.errors((
            -1,
            None,
            'I hate tuples',
        )), [
            Error('Value not > 0', pointer='0'),
            Error('Not a unicode string', pointer='1'),
            Error(
                'Value is not "I love tuples"',
                code=ERROR_CODE_UNKNOWN,
                pointer='2',
            ),
        ])

        self.assertEqual(
            schema.introspect(), {
                'type':
                'tuple',
                'contents': [
                    {
                        'type': 'integer',
                        'gt': 0
                    },
                    {
                        'type': 'unicode'
                    },
                    {
                        'type': 'constant',
                        'values': ['I love tuples']
                    },
                ]
            })

        with pytest.raises(TypeError):
            # noinspection PyTypeChecker
            Tuple('not a field')  # type: ignore

        with pytest.raises(TypeError):
            Tuple(Integer(gt=0),
                  UnicodeString(),
                  Constant('I love tuples'),
                  description=b'Not a unicode string')

        with pytest.raises(TypeError):
            Tuple(Integer(gt=0),
                  UnicodeString(),
                  Constant('I love tuples'),
                  unsupported='argument')
Example #11
0
        'bar': Boolean()
    }))
class BasicProvider(object):
    def __init__(self, foo, bar):
        self.foo = foo
        self.bar = bar


class BaseSomething(object):
    pass


@ClassConfigurationSchema.provider(
    Dictionary({
        'foo': Boolean(),
        'bar': Constant('walk', 'run')
    }))
class SpecificSomething(BaseSomething):
    def __init__(self, foo, bar):
        self.foo = foo
        self.bar = bar


@ClassConfigurationSchema.provider(
    Dictionary({
        'baz': Nullable(UnicodeString()),
        'qux': Boolean()
    },
               optional_keys=('qux', )), )
class AnotherSomething(BaseSomething):
    def __init__(self, baz, qux='unset'):