示例#1
0
    def test_exclude_current_instance_not_applied(self):
        validator = UniqueORMValidator(self.TableWithUniqueName, 'name')

        queryset = self.session.query(validator.model)
        self.assertEqual(queryset.count(), 3)
        self.assertEqual(
            validator.exclude_current_instance(queryset).count(), 3)
示例#2
0
 def test_filter_queryset(self):
     validator = UniqueORMValidator(
         self.TableWithUniqueName, 'name'
     )
     result_queryset = validator.filter_queryset(
         'Bob', self.session.query(validator.model)
     )
     self.assertEqual(result_queryset.count(), 1)
     self.assertEqual(result_queryset.value('name'), 'Bob')
示例#3
0
    def test_exclude_current_instance(self):
        validator = UniqueORMValidator(self.TableWithUniqueName, 'name')

        validator.instance = self.session.query(validator.model)\
            .filter(self.TableWithUniqueName.name == 'Adam')\
            .first()
        queryset = self.session.query(validator.model)
        self.assertEqual(queryset.count(), 3)
        self.assertEqual(
            validator.exclude_current_instance(queryset).count(), 2)
示例#4
0
    def test_exclude_current_instance_not_applied(self):
        validator = UniqueORMValidator(
            self.TableWithUniqueName, 'name'
        )

        queryset = self.session.query(validator.model)
        self.assertEqual(queryset.count(), 3)
        self.assertEqual(
            validator.exclude_current_instance(queryset).count(), 3
        )
示例#5
0
    def test_exclude_current_instance(self):
        validator = UniqueORMValidator(
            self.TableWithUniqueName, 'name'
        )

        validator.instance = self.session.query(validator.model)\
            .filter(self.TableWithUniqueName.name == 'Adam')\
            .first()
        queryset = self.session.query(validator.model)
        self.assertEqual(queryset.count(), 3)
        self.assertEqual(
            validator.exclude_current_instance(queryset).count(), 2
        )
示例#6
0
def get_field_kwargs(field_name, model_field, model_class):
    """
    Creates a default instance of a basic non-relational field.
    """
    mapper = model_class.__mapper__

    kwargs = {}
    field_validator = mapper.validators.get(field_name, None)
    validator_kwarg = []

    # The following will only be used by ModelField classes.
    # Gets removed for everything else
    kwargs['model_field'] = model_field

    if model_field.primary_key:
        # If this field is read-only, then return early.
        # Further keyword arguments are not valid
        kwargs['read_only'] = True
        return kwargs

    if model_field.default or model_field.nullable:
        kwargs['required'] = False

    if model_field.nullable:
        kwargs['allow_null'] = True

    if model_field.nullable and isinstance(model_field.type, STRING_TYPES):
        kwargs['allow_blank'] = True

    if isinstance(model_field.type, types.Enum):
        # If this model field contains choices, then return early.
        # Further keyword arguments are not valid
        kwargs['choices'] = model_field.type.enums
        return kwargs

    # Ensure that max_length is passed explicitly as a keyword arg, rather
    # than as a validator
    max_length = getattr(model_field.type, 'length', None)
    if max_length is not None and isinstance(model_field.type, STRING_TYPES):
        kwargs['max_length'] = max_length

    if getattr(model_field, 'unique', False):
        validator = UniqueORMValidator(model_class, field_name)
        validator_kwarg.append(validator)

    # If described table has a validator, then wrap him and call him later
    # when it will be necessary
    if field_validator:
        validator_func = field_validator[0]
        wrapper = ORMFieldValidator(validator_func, model_field, field_name)
        validator_kwarg.append(wrapper)

    if validator_kwarg:
        kwargs['validators'] = validator_kwarg

    return kwargs
示例#7
0
    def test_validation_raises_validation_error(self):
        validator = UniqueORMValidator(self.TableWithUniqueName, 'name')

        with self.assertRaises(ValidationError):
            self.assertIsNone(validator('Eve'))
示例#8
0
 def test_validation_passed_sucessfully(self):
     validator = UniqueORMValidator(self.TableWithUniqueName, 'name')
     self.assertIsNone(validator('John'))
示例#9
0
 def test_filter_queryset(self):
     validator = UniqueORMValidator(self.TableWithUniqueName, 'name')
     result_queryset = validator.filter_queryset(
         'Bob', self.session.query(validator.model))
     self.assertEqual(result_queryset.count(), 1)
     self.assertEqual(result_queryset.value('name'), 'Bob')