sqlalchemy.testing.db.execute

Here are the examples of the python api sqlalchemy.testing.db.execute taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

24 Examples 7

Example 1

Project: sqlalchemy
Source File: test_query.py
View license
    @classmethod
    def insert_data(cls):
        stuff = cls.tables.stuff
        testing.db.execute(
            stuff.insert(),
            [
                {'id': 1, 'value': 1},
                {'id': 2, 'value': 2},
                {'id': 3, 'value': 3},
                {'id': 4, 'value': 4},
                {'id': 5, 'value': 5},
            ]
        )

Example 2

Project: sqlalchemy
Source File: test_transaction.py
View license
    def test_no_expire_on_commit(self):
        User, users = self.classes.User, self.tables.users

        sess = sessionmaker(expire_on_commit=False)()
        u1 = User(name='ed')
        sess.add(u1)
        sess.commit()

        testing.db.execute(
            users.update(users.c.name == 'ed').values(name='edward'))

        assert u1.name == 'ed'
        sess.expire_all()
        assert u1.name == 'edward'

Example 3

Project: sqlalchemy
Source File: test_transaction.py
View license
    def test_rollback_no_accounting(self):
        User, users = self.classes.User, self.tables.users

        sess = sessionmaker(_enable_transaction_accounting=False)()
        u1 = User(name='ed')
        sess.add(u1)
        sess.commit()

        u1.name = 'edwardo'
        sess.rollback()

        testing.db.execute(
            users.update(users.c.name == 'ed').values(name='edward'))

        assert u1.name == 'edwardo'
        sess.expire_all()
        assert u1.name == 'edward'

Example 4

Project: sqlalchemy
Source File: test_functions.py
View license
    def test_exec_options(self):
        f = func.foo()
        eq_(f._execution_options, {})

        f = f.execution_options(foo='bar')
        eq_(f._execution_options, {'foo': 'bar'})
        s = f.select()
        eq_(s._execution_options, {'foo': 'bar'})

        ret = testing.db.execute(func.now().execution_options(foo='bar'))
        eq_(ret.context.execution_options, {'foo': 'bar'})
        ret.close()

Example 5

Project: sqlalchemy
Source File: test_insert_exec.py
View license
    def test_implicit_id_insert_select_columns(self):
        users = self.tables.users
        stmt = users.insert().from_select(
            (users.c.user_id, users.c.user_name),
            users.select().where(users.c.user_id == 20))

        testing.db.execute(stmt)

Example 6

Project: sqlalchemy
Source File: test_insert_exec.py
View license
    def test_implicit_id_insert_select_keys(self):
        users = self.tables.users
        stmt = users.insert().from_select(
            ["user_id", "user_name"],
            users.select().where(users.c.user_id == 20))

        testing.db.execute(stmt)

Example 7

Project: sqlalchemy
Source File: test_insert_exec.py
View license
    def _test(self, stmt, row, returning=None, inserted_primary_key=False):
        r = testing.db.execute(stmt)

        if returning:
            returned = r.first()
            eq_(returned, returning)
        elif inserted_primary_key is not False:
            eq_(r.inserted_primary_key, inserted_primary_key)

        eq_(testing.db.execute(self.tables.foo.select()).first(), row)

Example 8

Project: sqlalchemy
Source File: test_insert_exec.py
View license
    def _test_multi(self, stmt, rows, data):
        testing.db.execute(stmt, rows)
        eq_(
            testing.db.execute(
                self.tables.foo.select().
                order_by(self.tables.foo.c.id)).fetchall(),
            data)

Example 9

Project: sqlalchemy
Source File: test_query.py
View license
    def _assert_raises(self, stmt, params):
        assert_raises_message(
            exc.StatementError,
            "A value is required for bind parameter 'x'",
            testing.db.execute, stmt, **params)

        assert_raises_message(
            exc.StatementError,
            "A value is required for bind parameter 'x'",
            testing.db.execute, stmt, params)

Example 10

Project: sqlalchemy
Source File: test_returning.py
View license
    def test_no_ipk_on_returning(self):
        result = testing.db.execute(
            table.insert().returning(table.c.id),
            {'persons': 1, 'full': False}
        )
        assert_raises_message(
            sa_exc.InvalidRequestError,
            "Can't call inserted_primary_key when returning\(\) is used.",
            getattr, result, "inserted_primary_key"
        )

Example 11

Project: sqlalchemy
Source File: test_returning.py
View license
    def test_chained_insert_pk(self):
        t1 = self.tables.t1
        result = testing.db.execute(
            t1.insert().values(upddef=1).return_defaults(t1.c.insdef)
        )
        eq_(
            [result.returned_defaults[k] for k in (t1.c.id, t1.c.insdef)],
            [1, 0]
        )

Example 12

Project: sqlalchemy
Source File: test_returning.py
View license
    def test_arg_insert_pk(self):
        t1 = self.tables.t1
        result = testing.db.execute(
            t1.insert(return_defaults=[t1.c.insdef]).values(upddef=1)
        )
        eq_(
            [result.returned_defaults[k] for k in (t1.c.id, t1.c.insdef)],
            [1, 0]
        )

Example 13

Project: sqlalchemy
Source File: test_returning.py
View license
    def test_chained_update_pk(self):
        t1 = self.tables.t1
        testing.db.execute(
            t1.insert().values(upddef=1)
        )
        result = testing.db.execute(t1.update().values(data='d1').
                                    return_defaults(t1.c.upddef))
        eq_(
            [result.returned_defaults[k] for k in (t1.c.upddef,)],
            [1]
        )

Example 14

Project: sqlalchemy
Source File: test_returning.py
View license
    def test_arg_update_pk(self):
        t1 = self.tables.t1
        testing.db.execute(
            t1.insert().values(upddef=1)
        )
        result = testing.db.execute(t1.update(return_defaults=[t1.c.upddef]).
                                    values(data='d1'))
        eq_(
            [result.returned_defaults[k] for k in (t1.c.upddef,)],
            [1]
        )

Example 15

Project: sqlalchemy
Source File: test_returning.py
View license
    def test_insert_non_default(self):
        """test that a column not marked at all as a
        default works with this feature."""

        t1 = self.tables.t1
        result = testing.db.execute(
            t1.insert().values(upddef=1).return_defaults(t1.c.data)
        )
        eq_(
            [result.returned_defaults[k] for k in (t1.c.id, t1.c.data,)],
            [1, None]
        )

Example 16

Project: sqlalchemy
Source File: test_returning.py
View license
    def test_update_non_default(self):
        """test that a column not marked at all as a
        default works with this feature."""

        t1 = self.tables.t1
        testing.db.execute(
            t1.insert().values(upddef=1)
        )
        result = testing.db.execute(
            t1.update(). values(
                upddef=2).return_defaults(
                t1.c.data))
        eq_(
            [result.returned_defaults[k] for k in (t1.c.data,)],
            [None]
        )

Example 17

Project: sqlalchemy
Source File: test_type_expressions.py
View license
    def test_targeting_no_labels(self):
        testing.db.execute(
            self.tables.test_table.insert(),
            {"x": "X1", "y": "Y1"},
        )
        row = testing.db.execute(select([self.tables.test_table])).first()
        eq_(
            row[self.tables.test_table.c.y],
            "Y1"
        )

Example 18

Project: sqlalchemy
Source File: test_type_expressions.py
View license
    def test_targeting_by_string(self):
        testing.db.execute(
            self.tables.test_table.insert(),
            {"x": "X1", "y": "Y1"},
        )
        row = testing.db.execute(select([self.tables.test_table])).first()
        eq_(
            row["y"],
            "Y1"
        )

Example 19

Project: sqlalchemy
Source File: test_type_expressions.py
View license
    def test_targeting_apply_labels(self):
        testing.db.execute(
            self.tables.test_table.insert(),
            {"x": "X1", "y": "Y1"},
        )
        row = testing.db.execute(select([self.tables.test_table]).
                                 apply_labels()).first()
        eq_(
            row[self.tables.test_table.c.y],
            "Y1"
        )

Example 20

Project: sqlalchemy
Source File: test_type_expressions.py
View license
    def test_targeting_individual_labels(self):
        testing.db.execute(
            self.tables.test_table.insert(),
            {"x": "X1", "y": "Y1"},
        )
        row = testing.db.execute(select([
            self.tables.test_table.c.x.label('xbar'),
            self.tables.test_table.c.y.label('ybar')
        ])).first()
        eq_(
            row[self.tables.test_table.c.y],
            "Y1"
        )

Example 21

Project: sqlalchemy
Source File: test_selects.py
View license
    def test_load(self):
        foo, bar, baz = self.tables.foo, self.tables.bar, self.tables.baz
        # TODO: add persistence test also
        testing.db.execute(foo.insert(), a='not bar', b='baz')
        testing.db.execute(foo.insert(), a='also not bar', b='baz')
        testing.db.execute(foo.insert(), a='i am bar', b='bar')
        testing.db.execute(foo.insert(), a='also bar', b='bar')

        class Foo(fixtures.ComparableEntity):
            pass
        class Bar(Foo):
            pass
        class Baz(Foo):
            pass

        mapper(Foo, foo, polymorphic_on=foo.c.b)

        mapper(Baz, baz,
                    with_polymorphic=('*', foo.join(baz, foo.c.b == 'baz').alias('baz')),
                    inherits=Foo,
                    inherit_condition=(foo.c.a == baz.c.a),
                    inherit_foreign_keys=[baz.c.a],
                    polymorphic_identity='baz')

        mapper(Bar, bar,
                    with_polymorphic=('*', foo.join(bar, foo.c.b == 'bar').alias('bar')),
                    inherits=Foo,
                    inherit_condition=(foo.c.a == bar.c.a),
                    inherit_foreign_keys=[bar.c.a],
                    polymorphic_identity='bar')

        s = Session()

        assert [Baz(), Baz(), Bar(), Bar()] == s.query(Foo).order_by(Foo.b.desc()).all()
        assert [Bar(), Bar()] == s.query(Bar).all()

Example 22

Project: sqlalchemy
Source File: test_transaction.py
View license
    def test_commit_no_accounting(self):
        User, users = self.classes.User, self.tables.users

        sess = sessionmaker(_enable_transaction_accounting=False)()
        u1 = User(name='ed')
        sess.add(u1)
        sess.commit()

        u1.name = 'edwardo'
        sess.rollback()

        testing.db.execute(
            users.update(users.c.name == 'ed').values(name='edward'))

        assert u1.name == 'edwardo'
        sess.commit()

        assert testing.db.execute(select([users.c.name])).fetchall() == \
            [('edwardo',)]
        assert u1.name == 'edwardo'

        sess.delete(u1)
        sess.commit()

Example 23

Project: sqlalchemy
Source File: test_join_rewriting.py
View license
    def _test(self, selectable, assert_):
        result = testing.db.execute(selectable)
        result.close()
        for col in selectable.inner_columns:
            assert col in result._metadata._keymap

Example 24

Project: sqlalchemy
Source File: test_type_expressions.py
View license
    def test_round_trip(self):
        testing.db.execute(
            self.tables.test_table.insert(),
            {"x": "X1", "y": "Y1"},
            {"x": "X2", "y": "Y2"},
            {"x": "X3", "y": "Y3"},
        )

        # test insert coercion alone
        eq_(
            testing.db.execute(
                "select * from test_table order by y").fetchall(),
            [
                ("X1", "y1"),
                ("X2", "y2"),
                ("X3", "y3"),
            ]
        )

        # conversion back to upper
        eq_(
            testing.db.execute(
                select([self.tables.test_table]).
                order_by(self.tables.test_table.c.y)
            ).fetchall(),
            [
                ("X1", "Y1"),
                ("X2", "Y2"),
                ("X3", "Y3"),
            ]
        )