Python sqlalchemy.func.abs() Examples

The following are 4 code examples of sqlalchemy.func.abs(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.func , or try the search function .
Example #1
Source File: test_sqlite.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_expression_with_function_default(self):
        t = Table(
            "t",
            self.metadata,
            Column("id", Integer, primary_key=True),
            Column("x", Integer(), server_default=func.abs(-5) + 17),
        )
        t.create(testing.db)
        with testing.db.connect() as conn:
            conn.execute(t.insert())
            conn.execute(t.insert().values(x=35))
            eq_(
                conn.execute(select([t.c.x]).order_by(t.c.id)).fetchall(),
                [(22,), (35,)],
            ) 
Example #2
Source File: test_sqlite.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_column_defaults_ddl(self):

        t = Table(
            "t",
            MetaData(),
            Column(
                "x",
                Boolean(create_constraint=True),
                server_default=sql.false(),
            ),
        )

        self.assert_compile(
            CreateTable(t),
            "CREATE TABLE t (x BOOLEAN DEFAULT (0), CHECK (x IN (0, 1)))",
        )

        t = Table(
            "t",
            MetaData(),
            Column("x", String(), server_default=func.sqlite_version()),
        )
        self.assert_compile(
            CreateTable(t),
            "CREATE TABLE t (x VARCHAR DEFAULT (sqlite_version()))",
        )

        t = Table(
            "t",
            MetaData(),
            Column("x", Integer(), server_default=func.abs(-5) + 17),
        )
        self.assert_compile(
            CreateTable(t), "CREATE TABLE t (x INTEGER DEFAULT (abs(-5) + 17))"
        ) 
Example #3
Source File: test_metadata.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_indexes(self):
        meta = MetaData()

        table = Table(
            "mytable",
            meta,
            Column("id", Integer, primary_key=True),
            Column("data1", Integer, index=True),
            Column("data2", Integer),
            Index("text", text("data1 + 1")),
        )
        Index("multi", table.c.data1, table.c.data2)
        Index("func", func.abs(table.c.data1))
        Index("multi-func", table.c.data1, func.abs(table.c.data2))

        meta2 = MetaData()
        table_c = table.to_metadata(meta2)

        def _get_key(i):
            return (
                [i.name, i.unique]
                + sorted(i.kwargs.items())
                + [str(col) for col in i.expressions]
            )

        eq_(
            sorted([_get_key(i) for i in table.indexes]),
            sorted([_get_key(i) for i in table_c.indexes]),
        ) 
Example #4
Source File: test_metadata.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_indexes_with_col_redefine(self):
        meta = MetaData()

        table = Table(
            "mytable",
            meta,
            Column("id", Integer, primary_key=True),
            Column("data1", Integer),
            Column("data2", Integer),
            Index("text", text("data1 + 1")),
        )
        Index("multi", table.c.data1, table.c.data2)
        Index("func", func.abs(table.c.data1))
        Index("multi-func", table.c.data1, func.abs(table.c.data2))

        table = Table(
            "mytable",
            meta,
            Column("data1", Integer),
            Column("data2", Integer),
            extend_existing=True,
        )

        meta2 = MetaData()
        table_c = table.to_metadata(meta2)

        def _get_key(i):
            return (
                [i.name, i.unique]
                + sorted(i.kwargs.items())
                + [str(col) for col in i.expressions]
            )

        eq_(
            sorted([_get_key(i) for i in table.indexes]),
            sorted([_get_key(i) for i in table_c.indexes]),
        )