Beispiel #1
0
 def test_decimal(self):  # type: () -> None
     """
     Tests decimal.Decimal object validation
     """
     self.assertEqual([], Decimal().errors(decimal.Decimal('1')))
     self.assertEqual([], Decimal().errors(decimal.Decimal('1.4')))
     self.assertEqual([], Decimal().errors(decimal.Decimal('-3.14159')))
     self.assertEqual([Error('Not a decimal')],
                      Decimal().errors('-3.14159'))
     self.assertEqual([Error('Not a decimal')], Decimal().errors(-3.14159))
     self.assertEqual([Error('Not a decimal')], Decimal().errors(15))
     self.assertEqual(
         [Error('Value not > 6')],
         Decimal(lt=12, gt=6).errors(decimal.Decimal('6')),
     )
     self.assertEqual(
         [Error('Value not < 12')],
         Decimal(lt=12, gt=6).errors(decimal.Decimal('12')),
     )
     self.assertEqual([],
                      Decimal(lt=12, gt=6).errors(decimal.Decimal('6.1')))
     self.assertEqual([],
                      Decimal(lt=12, gt=6).errors(decimal.Decimal('11.9')))
     self.assertEqual(
         [Error('Value not >= 6')],
         Decimal(lte=12, gte=6).errors(decimal.Decimal('5.9')),
     )
     self.assertEqual(
         [Error('Value not <= 12')],
         Decimal(lte=12, gte=6).errors(decimal.Decimal('12.1')),
     )
     self.assertEqual([],
                      Decimal(lte=12, gte=6).errors(decimal.Decimal('6')))
     self.assertEqual([],
                      Decimal(lte=12, gte=6).errors(decimal.Decimal('12')))
Beispiel #2
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'),
        ]