示例#1
0
    def test_server_default_absent_value(self):
        metadata = MetaData()
        table = Table(
            "sometable",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("data", String),
            Column("foo", Integer, server_default=func.foobar()),
        )

        values = [
            {
                "id": 1,
                "data": "data1",
                "foo": "plainfoo"
            },
            {
                "id": 2,
                "data": "data2"
            },
            {
                "id": 3,
                "data": "data3",
                "foo": "otherfoo"
            },
        ]

        assert_raises_message(
            exc.CompileError,
            "INSERT value for column sometable.foo is explicitly rendered "
            "as a boundparameter in the VALUES clause; a Python-side value or "
            "SQL expression is required",
            table.insert().values(values).compile,
        )
示例#2
0
    def test_sql_expression_pk_noautoinc_returning(self):
        # test that return_defaults() works with a primary key where we are
        # sending a SQL expression, and we need to get the server-calculated
        # value back.  [ticket:3133]
        metadata = MetaData()
        table = Table(
            "sometable",
            metadata,
            Column("id", Integer, autoincrement=False, primary_key=True),
            Column("data", String),
        )

        stmt = table.insert().return_defaults().values(id=func.foobar())
        returning_dialect = postgresql.dialect()
        returning_dialect.implicit_returning = True
        compiled = stmt.compile(dialect=returning_dialect,
                                column_keys=["data"])
        eq_(compiled.postfetch, [])
        eq_(compiled.returning, [table.c.id])

        self.assert_compile(
            stmt,
            "INSERT INTO sometable (id, data) VALUES "
            "(foobar(), %(data)s) RETURNING sometable.id",
            checkparams={"data": "foo"},
            params={"data": "foo"},
            dialect=returning_dialect,
        )
示例#3
0
    def test_sql_expression_pk_autoinc_lastinserted(self):
        # test that postfetch isn't invoked for a SQL expression
        # in a primary key column.  the DB either needs to support a lastrowid
        # that can return it, or RETURNING.  [ticket:3133]
        metadata = MetaData()
        table = Table(
            "sometable",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("data", String),
        )

        stmt = table.insert().return_defaults().values(id=func.foobar())
        compiled = stmt.compile(dialect=sqlite.dialect(), column_keys=["data"])
        eq_(compiled.postfetch, [])
        eq_(compiled.returning, [])

        self.assert_compile(
            stmt,
            "INSERT INTO sometable (id, data) VALUES "
            "(foobar(), ?)",
            checkparams={"data": "foo"},
            params={"data": "foo"},
            dialect=sqlite.dialect(),
        )
    def test_as_comparison_annotate(self):

        fn = func.foobar("x", "y", "q", "p", "r").as_comparison(2, 5)

        from sqlalchemy_1_3.sql import annotation

        fn_annotated = annotation._deep_annotate(fn, {"token": "yes"})

        eq_(fn.left._annotations, {})
        eq_(fn_annotated.left._annotations, {"token": "yes"})
示例#5
0
 def test_insert_from_select_no_defaults(self):
     metadata = MetaData()
     table = Table(
         "sometable",
         metadata,
         Column("id", Integer, primary_key=True),
         Column("foo", Integer, default=func.foobar()),
     )
     table1 = self.tables.mytable
     sel = select([table1.c.myid]).where(table1.c.name == "foo")
     ins = table.insert().from_select(["id"], sel, include_defaults=False)
     self.assert_compile(
         ins,
         "INSERT INTO sometable (id) SELECT mytable.myid "
         "FROM mytable WHERE mytable.name = :name_1",
         checkparams={"name_1": "foo"},
     )
示例#6
0
    def test_inline_default(self):
        metadata = MetaData()
        table = Table(
            "sometable",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("data", String),
            Column("foo", Integer, default=func.foobar()),
        )

        values = [
            {
                "id": 1,
                "data": "data1"
            },
            {
                "id": 2,
                "data": "data2",
                "foo": "plainfoo"
            },
            {
                "id": 3,
                "data": "data3"
            },
        ]

        checkparams = {
            "id_m0": 1,
            "id_m1": 2,
            "id_m2": 3,
            "data_m0": "data1",
            "data_m1": "data2",
            "data_m2": "data3",
            "foo_m1": "plainfoo",
        }

        self.assert_compile(
            table.insert().values(values),
            "INSERT INTO sometable (id, data, foo) VALUES "
            "(%(id_m0)s, %(data_m0)s, foobar()), "
            "(%(id_m1)s, %(data_m1)s, %(foo_m1)s), "
            "(%(id_m2)s, %(data_m2)s, foobar())",
            checkparams=checkparams,
            dialect=postgresql.dialect(),
        )
示例#7
0
    def test_inline_default(self):
        metadata = MetaData()
        table = Table(
            "sometable",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("foo", Integer, default=func.foobar()),
        )

        self.assert_compile(
            table.insert(values={}, inline=True),
            "INSERT INTO sometable (foo) VALUES (foobar())",
        )

        self.assert_compile(
            table.insert(inline=True),
            "INSERT INTO sometable (foo) VALUES (foobar())",
            params={},
        )
示例#8
0
 def __eq__(self, other):
     return func.foobar(
         self.__clause_element__()) == func.foobar(other)