Exemplo n.º 1
0
    def test_get_field_kwargs_for_decimal_field(self):
        field_kwargs = get_field_kwargs(
            'salary',
            self.TestGetFieldKwargsModel.salary,
            self.TestGetFieldKwargsModel
        )

        self.assertEqual(
            set(field_kwargs.keys()),
            set([
                'model_field', 'max_digits', 'decimal_places',
                'required', 'allow_null'
            ])
        )
        self.assertTrue(field_kwargs['allow_null'])
        self.assertFalse(field_kwargs['required'])
        self.assertEqual(
            field_kwargs['max_digits'],
            self.TestGetFieldKwargsModel.salary.type.precision
        )
        self.assertEqual(
            field_kwargs['decimal_places'],
            self.TestGetFieldKwargsModel.salary.type.scale,
        )
        self.assertEqual(
            field_kwargs['model_field'],
            self.TestGetFieldKwargsModel.salary
        )
Exemplo n.º 2
0
    def test_get_field_kwargs_for_primary_key_field(self):
        field_kwargs = get_field_kwargs('id', self.TestGetFieldKwargsModel.id,
                                        self.TestGetFieldKwargsModel)

        self.assertEqual(set(field_kwargs.keys()),
                         set(['model_field', 'read_only']))
        self.assertTrue(field_kwargs['read_only'])
        self.assertEqual(field_kwargs['model_field'],
                         self.TestGetFieldKwargsModel.id)
Exemplo n.º 3
0
    def build_standard_field(self, *args, **kwargs):
        """
        Create regular model fields.
        """
        field_name, model_field, model_class = args
        field_mapping = ClassLookupDict(self.serializer_field_mapping)

        field_class = field_mapping[model_field.type]
        field_kwargs = get_field_kwargs(field_name, model_field, model_class)

        if 'choices' in field_kwargs:
            # Fields with choices get coerced into `ChoiceField`
            # instead of using their regular typed field
            field_class = self.serializer_choice_field
            # Some model fields may introduce kwargs that would not be valid
            # for the choice field. We need to strip these out.
            # Eg. Column('race', Enum('one', 'two', 'three'))
            valid_kwargs = {
                'read_only', 'write_only', 'required', 'default', 'initial',
                'source', 'label', 'help_text', 'style', 'error_messages',
                'validators', 'allow_null', 'allow_blank', 'choices'
            }
            for key in list(field_kwargs.keys()):
                if key not in valid_kwargs:
                    field_kwargs.pop(key)

        if not issubclass(field_class, ModelField):
            # `model_field` is only valid for the fallback case of
            # `ModelField`, which is used when no other typed field
            # matched to the model field
            field_kwargs.pop('model_field', None)

        if not issubclass(field_class, CharField) and \
                not issubclass(field_class, EnumField):
            # `allow_blank` is only valid for textual fields
            field_kwargs.pop('allow_blank', None)

        if isinstance(model_field.type, postgresql.ARRAY):
            # Populate the `child` argument on `ListField` instances generated
            # for the PostgreSQL specific `ArrayField`
            child_field_name = '%s.%s' % (field_name, 'child')
            child_model_field = model_field.type.item_type
            # Because our model_field, which passed to the build_standard_field
            # function is a Column, then wrap this sub-field and invoke this
            # method one more time
            child_field_class, child_field_kwargs = self.build_standard_field(
                child_field_name, Column(child_model_field), model_class)
            field_kwargs['child'] = child_field_class(**child_field_kwargs)

        return field_class, field_kwargs
Exemplo n.º 4
0
    def test_get_field_kwargs_for_primary_key_field(self):
        field_kwargs = get_field_kwargs(
            'id',
            self.TestGetFieldKwargsModel.id,
            self.TestGetFieldKwargsModel
        )

        self.assertEqual(
            set(field_kwargs.keys()),
            set(['model_field', 'read_only'])
        )
        self.assertTrue(field_kwargs['read_only'])
        self.assertEqual(
            field_kwargs['model_field'],
            self.TestGetFieldKwargsModel.id
        )
Exemplo n.º 5
0
    def test_get_field_kwargs_for_nullable_field(self):
        field_kwargs = get_field_kwargs('address',
                                        self.TestGetFieldKwargsModel.address,
                                        self.TestGetFieldKwargsModel)

        self.assertEqual(
            set(field_kwargs.keys()),
            set([
                'model_field', 'required', 'allow_blank', 'allow_null',
                'max_length'
            ]))
        self.assertTrue(field_kwargs['allow_blank'])
        self.assertTrue(field_kwargs['allow_null'])
        self.assertFalse(field_kwargs['required'])
        self.assertEqual(field_kwargs['max_length'], 50)
        self.assertEqual(field_kwargs['model_field'],
                         self.TestGetFieldKwargsModel.address)
Exemplo n.º 6
0
    def test_get_field_kwargs_for_enum_field(self):
        field_kwargs = get_field_kwargs('gender',
                                        self.TestGetFieldKwargsModel.gender,
                                        self.TestGetFieldKwargsModel)

        self.assertEqual(
            set(field_kwargs.keys()),
            set([
                'model_field', 'required', 'allow_blank', 'allow_null',
                'choices'
            ]))
        self.assertTrue(field_kwargs['allow_blank'])
        self.assertTrue(field_kwargs['allow_null'])
        self.assertFalse(field_kwargs['required'])
        self.assertEqual(field_kwargs['model_field'],
                         self.TestGetFieldKwargsModel.gender)
        self.assertEqual(set(field_kwargs['choices']), {'male', 'female'})
Exemplo n.º 7
0
    def test_get_field_kwargs_for_unique_and_validated_field(self):
        field_kwargs = get_field_kwargs('email',
                                        self.TestGetFieldKwargsModel.email,
                                        self.TestGetFieldKwargsModel)

        self.assertEqual(
            set(field_kwargs.keys()),
            set([
                'model_field', 'required', 'allow_blank', 'allow_null',
                'validators', 'max_length'
            ]))
        self.assertTrue(field_kwargs['allow_blank'])
        self.assertTrue(field_kwargs['allow_null'])
        self.assertFalse(field_kwargs['required'])
        self.assertEqual(field_kwargs['max_length'], 50)
        self.assertEqual(field_kwargs['model_field'],
                         self.TestGetFieldKwargsModel.email)
        self.assertIn('validators', field_kwargs.keys())
Exemplo n.º 8
0
    def test_get_field_kwargs_for_decimal_field(self):
        field_kwargs = get_field_kwargs('salary',
                                        self.TestGetFieldKwargsModel.salary,
                                        self.TestGetFieldKwargsModel)

        self.assertEqual(
            set(field_kwargs.keys()),
            set([
                'model_field', 'max_digits', 'decimal_places', 'required',
                'allow_null'
            ]))
        self.assertTrue(field_kwargs['allow_null'])
        self.assertFalse(field_kwargs['required'])
        self.assertEqual(field_kwargs['max_digits'],
                         self.TestGetFieldKwargsModel.salary.type.precision)
        self.assertEqual(
            field_kwargs['decimal_places'],
            self.TestGetFieldKwargsModel.salary.type.scale,
        )
        self.assertEqual(field_kwargs['model_field'],
                         self.TestGetFieldKwargsModel.salary)
Exemplo n.º 9
0
    def test_get_field_kwargs_for_enum_field(self):
        field_kwargs = get_field_kwargs(
            'gender',
            self.TestGetFieldKwargsModel.gender,
            self.TestGetFieldKwargsModel
        )

        self.assertEqual(
            set(field_kwargs.keys()),
            set([
                'model_field', 'required', 'allow_blank', 'allow_null',
                'choices'
            ])
        )
        self.assertTrue(field_kwargs['allow_blank'])
        self.assertTrue(field_kwargs['allow_null'])
        self.assertFalse(field_kwargs['required'])
        self.assertEqual(
            field_kwargs['model_field'],
            self.TestGetFieldKwargsModel.gender
        )
        self.assertEqual(set(field_kwargs['choices']), {'male', 'female'})
Exemplo n.º 10
0
    def test_get_field_kwargs_for_nullable_field(self):
        field_kwargs = get_field_kwargs(
            'address',
            self.TestGetFieldKwargsModel.address,
            self.TestGetFieldKwargsModel
        )

        self.assertEqual(
            set(field_kwargs.keys()),
            set([
                'model_field', 'required', 'allow_blank', 'allow_null',
                'max_length'
            ])
        )
        self.assertTrue(field_kwargs['allow_blank'])
        self.assertTrue(field_kwargs['allow_null'])
        self.assertFalse(field_kwargs['required'])
        self.assertEqual(field_kwargs['max_length'], 50)
        self.assertEqual(
            field_kwargs['model_field'],
            self.TestGetFieldKwargsModel.address
        )
Exemplo n.º 11
0
    def test_get_field_kwargs_for_unique_and_validated_field(self):
        field_kwargs = get_field_kwargs(
            'email',
            self.TestGetFieldKwargsModel.email,
            self.TestGetFieldKwargsModel
        )

        self.assertEqual(
            set(field_kwargs.keys()),
            set([
                'model_field', 'required', 'allow_blank', 'allow_null',
                'validators', 'max_length'
            ])
        )
        self.assertTrue(field_kwargs['allow_blank'])
        self.assertTrue(field_kwargs['allow_null'])
        self.assertFalse(field_kwargs['required'])
        self.assertEqual(field_kwargs['max_length'], 50)
        self.assertEqual(
            field_kwargs['model_field'],
            self.TestGetFieldKwargsModel.email
        )
        self.assertIn('validators', field_kwargs.keys())