Exemplo n.º 1
0
        class A:
            field = Field()

            @staticmethod
            @field.set_format
            def return_1(value):
                return 1

            @staticmethod
            @field.set_parse
            def field_add_1(value):
                return value + 1

            @staticmethod
            @field.set_validators
            def large_than(value):
                assert value > 0
                return value + 1  # useless return

            @staticmethod
            @field.add_validator
            def less_than(value):
                assert value < 100
Exemplo n.º 2
0
        class A(Catalyst):
            all_errors = False

            a = Field()
            b = Field()
Exemplo n.º 3
0
 class Base(Catalyst):
     base = Field()
 class C(Catalyst):
     _format_field_name = staticmethod(snake_to_camel)
     _format_field_key = staticmethod(snake_to_camel)
     snake_to_camel = Field()
     still_snake = Field(name='still_snake', key='still_snake')
 class B(Catalyst):
     _format_field_name = staticmethod(snake_to_camel)
     snake_to_camel = Field()
 class A(Catalyst):
     _format_field_key = staticmethod(snake_to_camel)
     snake_to_camel = Field()
Exemplo n.º 7
0
    def test_field(self):
        field = BaseField()
        with self.assertRaises(NotImplementedError):
            field.load()
        with self.assertRaises(NotImplementedError):
            field.dump()

        # test set field opts in class
        class A:
            field = Field()

            @staticmethod
            @field.set_format
            def return_1(value):
                return 1

            @staticmethod
            @field.set_parse
            def field_add_1(value):
                return value + 1

            @staticmethod
            @field.set_validators
            def large_than(value):
                assert value > 0
                return value + 1  # useless return

            @staticmethod
            @field.add_validator
            def less_than(value):
                assert value < 100

        a = A()

        # test dump
        self.assertEqual(a.field.dump(1000), 1)
        self.assertEqual(a.field.dump('asd'), 1)

        # test load
        self.assertEqual(a.field.load(0), 1)
        self.assertEqual(a.field.load(None), None)
        with self.assertRaises(TypeError):
            a.field.load('asd')
        with self.assertRaises(AssertionError):
            a.field.load(-1)
        with self.assertRaises(AssertionError):
            a.field.load(100)

        # test validators
        self.assertEqual(len(a.field.validators), 2)

        # test wrong args
        with self.assertRaises(TypeError):
            a.field.set_validators(1)
        with self.assertRaises(TypeError):
            a.field.add_validator(1)
        with self.assertRaises(TypeError):
            a.field.set_format(1)
        with self.assertRaises(TypeError):
            a.field.set_parse(1)

        # test set functions when init field
        field = Field(formatter=A.return_1, parser=a.field.parse)
        self.assertEqual(field.format, A.return_1)
        self.assertEqual(field.parse, A.field_add_1)

        # test error msg
        field = Field(key='a',
                      allow_none=False,
                      error_messages={'none': '666'})
        with self.assertRaises(ValidationError) as cm:
            field.load(None)
        self.assertEqual(cm.exception.msg, '666')

        # test in and not in
        field = Field(in_='123', not_in='456')
        self.assertEqual(field.load('1'), '1')
        self.assertEqual(len(field.validators), 2)
        with self.assertRaises(ValidationError) as cm:
            field.load('0')
        with self.assertRaises(ValidationError) as cm:
            field.load('4')

        # test none value
        field = Field(as_none=('', None), dump_none='', load_none=None)
        self.assertEqual(field.load(None), None)
        self.assertEqual(field.load(''), None)
        self.assertEqual(field.load(0), 0)
        self.assertEqual(field.dump(None), '')
        self.assertEqual(field.dump(''), '')
        self.assertEqual(field.dump(0), 0)
Exemplo n.º 8
0
    def test_field_override_method(self):
        field = Field()

        field.override_method(no_processing, 'dump')
        self.assertEqual(field.dump, no_processing)
        self.assertEqual(field.dump(1), 1)

        field.override_method(attr='load')(no_processing)
        self.assertEqual(field.load, no_processing)
        self.assertEqual(field.load(1), 1)

        field = Field()

        @field.set_format
        def to_str_1(value):
            return str(value)

        self.assertEqual(field.format, to_str_1)
        self.assertEqual(field.format(1), '1')

        @field.set_format
        def to_str_2(value, field, original_method):
            assert isinstance(field, Field)
            return original_method(value)

        self.assertEqual(field.format, to_str_2)
        self.assertEqual(field.format(1), '1')

        field = Field()

        @field.set_format
        def to_str_3(self, value, field):
            assert self is field
            assert isinstance(self, Field)
            return str(value)

        self.assertEqual(field.format, to_str_3)
        self.assertEqual(field.format(1), '1')

        @field.set_format(obj_name='obj', original_name='old')
        def to_str_4(self, value, obj, old):
            assert self is obj
            assert isinstance(obj, Field)
            return old(value)

        self.assertEqual(field.format, to_str_4)
        self.assertEqual(field.format(1), '1')

        field = Field()

        @field.set_format
        def to_str_5(value, **kwargs):
            assert set(kwargs) == {'field', 'original_method'}
            return str(value)

        self.assertEqual(field.format, to_str_5)
        self.assertEqual(field.format(1), '1')

        @field.set_format(obj_name='obj', original_name='old')
        def to_str_6(value, **kwargs):
            assert set(kwargs) == {'obj', 'old'}
            return str(value)

        self.assertEqual(field.format, to_str_6)
        self.assertEqual(field.format(1), '1')

        with self.assertRaises(TypeError):
            field.set_format(lambda field, value: str(value))