def test_is_many_to_many(self):
     interf = SQLAInterface(Parent)
     eq_(True, interf.is_relation('neighbours'))
     eq_(False, interf.is_relation_one_to_many('neighbours'))
     eq_(False, interf.is_relation_one_to_one('neighbours'))
     eq_(False, interf.is_relation_many_to_one('neighbours'))
     eq_(True, interf.is_relation_many_to_many('neighbours'))
     interf = SQLAInterface(Neighbour)
     eq_(True, interf.is_relation('parents'))
     eq_(False, interf.is_relation_one_to_many('parents'))
     eq_(False, interf.is_relation_one_to_one('parents'))
     eq_(False, interf.is_relation_many_to_one('parents'))
     eq_(True, interf.is_relation_many_to_many('parents'))
 def test_is_one_to_one(self):
     interf = SQLAInterface(Parent)
     eq_(True, interf.is_relation('favorite_child'))
     eq_(True, interf.is_relation_one_to_one('favorite_child'))
     eq_(False, interf.is_relation_one_to_many('favorite_child'))
     eq_(False, interf.is_relation_many_to_many('favorite_child'))
     eq_(False, interf.is_relation_many_to_one('favorite_child'))
     interf = SQLAInterface(FavoriteChild)
     eq_(True, interf.is_relation('parent'))
     eq_(True, interf.is_relation_one_to_one('parent'))
     eq_(False, interf.is_relation_one_to_many('parent'))
     eq_(False, interf.is_relation_many_to_many('parent'))
     eq_(False, interf.is_relation_many_to_one('parent'))
 def test_is_one_to_many(self):
     interf = SQLAInterface(Parent)
     eq_(True, interf.is_relation('children'))
     eq_(True, interf.is_relation_one_to_many('children'))
     eq_(False, interf.is_relation_one_to_one('children'))
     eq_(False, interf.is_relation_many_to_one('children'))
     eq_(False, interf.is_relation_many_to_many('children'))
 def test_is_many_to_one_no_backref(self):
     interf = SQLAInterface(Headache)
     eq_(True, interf.is_relation('parent'))
     eq_(False, interf.is_relation_one_to_many('parent'))
     eq_(False, interf.is_relation_one_to_one('parent'))
     eq_(True, interf.is_relation_many_to_one('parent'))
     eq_(False, interf.is_relation_many_to_many('parent'))
 def test_is_many_to_one(self):
     interf = SQLAInterface(Child)
     eq_(True, interf.is_relation('parent'))
     eq_(False, interf.is_relation_one_to_many('parent'))
     eq_(False, interf.is_relation_one_to_one('parent'))
     eq_(True, interf.is_relation_many_to_one('parent'))
     eq_(False, interf.is_relation_many_to_many('parent'))
Example #6
0
    def _column2field(
        self,
        datamodel: SQLAInterface,
        column: TreeNode,
        nested: bool = True,
        enum_dump_by_name: bool = False,
        parent_schema_name: Optional[str] = None,
    ) -> Field:
        """

        :param datamodel: SQLAInterface
        :param column: TreeNode column (childs are dotted columns)
        :param nested: Boolean if will create nested fields
        :param enum_dump_by_name:
        :return: Schema.field
        """
        # Handle relations
        if datamodel.is_relation(column.data):
            return self._column2relation(
                datamodel, column, nested=nested, parent_schema_name=parent_schema_name
            )
        # Handle Enums
        elif datamodel.is_enum(column.data):
            return self._column2enum(
                datamodel, column, enum_dump_by_name=enum_dump_by_name
            )
        # is custom property method field?
        if hasattr(getattr(datamodel.obj, column.data), "fget"):
            return fields.Raw(dump_only=True)
        # its a model function
        if hasattr(getattr(datamodel.obj, column.data), "__call__"):
            return fields.Function(getattr(datamodel.obj, column.data), dump_only=True)
        # is a normal model field not a function?
        if not hasattr(getattr(datamodel.obj, column.data), "__call__"):
            field = field_for(datamodel.obj, column.data)
            field.unique = datamodel.is_unique(column.data)
            if column.data in self.validators_columns:
                if field.validate is None:
                    field.validate = []
                field.validate.append(self.validators_columns[column.data])
                field.validators.append(self.validators_columns[column.data])
            return field