Source

sqlalchemy / test / orm / test_lockmode.py

from sqlalchemy.engine import default
from sqlalchemy.databases import *
from sqlalchemy.orm import mapper
from sqlalchemy.orm import Session
from test.lib import AssertsCompiledSQL
from test.lib.testing import assert_raises_message
from test.orm import _fixtures


class LockModeTest(_fixtures.FixtureTest, AssertsCompiledSQL):
    run_inserts = None

    @classmethod
    def setup_mappers(cls):
        User, users = cls.classes.User, cls.tables.users
        mapper(User, users)

    def test_default_update(self):
        User = self.classes.User
        sess = Session()
        self.assert_compile(sess.query(User.id).with_lockmode('update'),
            "SELECT users.id AS users_id FROM users FOR UPDATE",
            dialect=default.DefaultDialect()
        )

    def test_not_supported_by_dialect_should_just_use_update(self):
        User = self.classes.User
        sess = Session()
        self.assert_compile(sess.query(User.id).with_lockmode('read'),
            "SELECT users.id AS users_id FROM users FOR UPDATE",
            dialect=default.DefaultDialect()
        )

    def test_none_lock_mode(self):
        User = self.classes.User
        sess = Session()
        self.assert_compile(sess.query(User.id).with_lockmode(None),
            "SELECT users.id AS users_id FROM users",
            dialect=default.DefaultDialect()
        )

    def test_unknown_lock_mode(self):
        User = self.classes.User
        sess = Session()
        assert_raises_message(
            Exception, "Unknown lockmode 'unknown_mode'",
            self.assert_compile,
            sess.query(User.id).with_lockmode('unknown_mode'), None,
            dialect=default.DefaultDialect()
        )

    def test_postgres_read(self):
        User = self.classes.User
        sess = Session()
        self.assert_compile(sess.query(User.id).with_lockmode('read'),
            "SELECT users.id AS users_id FROM users FOR SHARE",
            dialect=postgresql.dialect()
        )

    def test_postgres_read_nowait(self):
        User = self.classes.User
        sess = Session()
        self.assert_compile(sess.query(User.id).with_lockmode('read_nowait'),
            "SELECT users.id AS users_id FROM users FOR SHARE NOWAIT",
            dialect=postgresql.dialect()
        )

    def test_postgres_update(self):
        User = self.classes.User
        sess = Session()
        self.assert_compile(sess.query(User.id).with_lockmode('update'),
            "SELECT users.id AS users_id FROM users FOR UPDATE",
            dialect=postgresql.dialect()
        )

    def test_postgres_update_nowait(self):
        User = self.classes.User
        sess = Session()
        self.assert_compile(sess.query(User.id).with_lockmode('update_nowait'),
            "SELECT users.id AS users_id FROM users FOR UPDATE NOWAIT",
            dialect=postgresql.dialect()
        )

    def test_oracle_update(self):
        User = self.classes.User
        sess = Session()
        self.assert_compile(sess.query(User.id).with_lockmode('update'),
            "SELECT users.id AS users_id FROM users FOR UPDATE",
            dialect=oracle.dialect()
        )

    def test_oracle_update_nowait(self):
        User = self.classes.User
        sess = Session()
        self.assert_compile(sess.query(User.id).with_lockmode('update_nowait'),
            "SELECT users.id AS users_id FROM users FOR UPDATE NOWAIT",
            dialect=oracle.dialect()
        )

    def test_mysql_read(self):
        User = self.classes.User
        sess = Session()
        self.assert_compile(sess.query(User.id).with_lockmode('read'),
            "SELECT users.id AS users_id FROM users LOCK IN SHARE MODE",
            dialect=mysql.dialect()
        )

    def test_mysql_update(self):
        User = self.classes.User
        sess = Session()
        self.assert_compile(sess.query(User.id).with_lockmode('update'),
            "SELECT users.id AS users_id FROM users FOR UPDATE",
            dialect=mysql.dialect()
        )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.