本文整理汇总了Python中sqlalchemy.testing.engines.testing_engine方法的典型用法代码示例。如果您正苦于以下问题:Python engines.testing_engine方法的具体用法?Python engines.testing_engine怎么用?Python engines.testing_engine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.testing.engines
的用法示例。
在下文中一共展示了engines.testing_engine方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_set_isolation_level
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_set_isolation_level(self):
eng = engines.testing_engine(options=dict())
conn = eng.connect()
eq_(
eng.dialect.get_isolation_level(conn.connection),
self._default_isolation_level()
)
eng.dialect.set_isolation_level(
conn.connection, self._non_default_isolation_level()
)
eq_(
eng.dialect.get_isolation_level(conn.connection),
self._non_default_isolation_level()
)
conn.close()
示例2: test_reset_level
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_reset_level(self):
eng = engines.testing_engine(options=dict())
conn = eng.connect()
eq_(
eng.dialect.get_isolation_level(conn.connection),
self._default_isolation_level()
)
eng.dialect.set_isolation_level(
conn.connection, self._non_default_isolation_level()
)
eq_(
eng.dialect.get_isolation_level(conn.connection),
self._non_default_isolation_level()
)
eng.dialect.reset_isolation_level(conn.connection)
eq_(
eng.dialect.get_isolation_level(conn.connection),
self._default_isolation_level()
)
conn.close()
示例3: test_argument_format_execute
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_argument_format_execute(self):
def before_execute(conn, clauseelement, multiparams, params):
assert isinstance(multiparams, (list, tuple))
assert isinstance(params, dict)
def after_execute(conn, clauseelement, multiparams, params, result):
assert isinstance(multiparams, (list, tuple))
assert isinstance(params, dict)
e1 = testing_engine(config.db_url)
event.listen(e1, "before_execute", before_execute)
event.listen(e1, "after_execute", after_execute)
with testing.expect_deprecated(
r"The argument signature for the "
r"\"ConnectionEvents.before_execute\" event listener",
r"The argument signature for the "
r"\"ConnectionEvents.after_execute\" event listener",
):
e1.execute(select([1]))
示例4: test_listen_targets_scope
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_listen_targets_scope(self):
canary = []
def listen_one(*args):
canary.append("listen_one")
def listen_two(*args):
canary.append("listen_two")
def listen_three(*args):
canary.append("listen_three")
def listen_four(*args):
canary.append("listen_four")
engine = testing_engine(testing.db.url)
event.listen(pool.Pool, "connect", listen_one)
event.listen(engine.pool, "connect", listen_two)
event.listen(engine, "connect", listen_three)
event.listen(engine.__class__, "connect", listen_four)
engine.execute(select([1])).close()
eq_(
canary, ["listen_one", "listen_four", "listen_two", "listen_three"]
)
示例5: test_engine_level_options
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_engine_level_options(self):
eng = engines.testing_engine(
options={"execution_options": {"foo": "bar"}}
)
with eng.connect() as conn:
eq_(conn._execution_options["foo"], "bar")
eq_(
conn.execution_options(bat="hoho")._execution_options["foo"],
"bar",
)
eq_(
conn.execution_options(bat="hoho")._execution_options["bat"],
"hoho",
)
eq_(
conn.execution_options(foo="hoho")._execution_options["foo"],
"hoho",
)
eng.update_execution_options(foo="hoho")
conn = eng.connect()
eq_(conn._execution_options["foo"], "hoho")
示例6: test_generative_engine_execution_options
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_generative_engine_execution_options(self):
eng = engines.testing_engine(
options={"execution_options": {"base": "x1"}}
)
is_(eng.engine, eng)
eng1 = eng.execution_options(foo="b1")
is_(eng1.engine, eng1)
eng2 = eng.execution_options(foo="b2")
eng1a = eng1.execution_options(bar="a1")
eng2a = eng2.execution_options(foo="b3", bar="a2")
is_(eng2a.engine, eng2a)
eq_(eng._execution_options, {"base": "x1"})
eq_(eng1._execution_options, {"base": "x1", "foo": "b1"})
eq_(eng2._execution_options, {"base": "x1", "foo": "b2"})
eq_(eng1a._execution_options, {"base": "x1", "foo": "b1", "bar": "a1"})
eq_(eng2a._execution_options, {"base": "x1", "foo": "b3", "bar": "a2"})
is_(eng1a.pool, eng.pool)
# test pool is shared
eng2.dispose()
is_(eng1a.pool, eng2.pool)
is_(eng.pool, eng2.pool)
示例7: test_per_engine_independence
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_per_engine_independence(self):
e1 = testing_engine(config.db_url)
e2 = testing_engine(config.db_url)
canary = Mock()
event.listen(e1, "before_execute", canary)
s1 = select([1])
s2 = select([2])
with e1.connect() as conn:
conn.execute(s1)
with e2.connect() as conn:
conn.execute(s2)
eq_([arg[1][1] for arg in canary.mock_calls], [s1])
event.listen(e2, "before_execute", canary)
with e1.connect() as conn:
conn.execute(s1)
with e2.connect() as conn:
conn.execute(s2)
eq_([arg[1][1] for arg in canary.mock_calls], [s1, s1, s2])
示例8: test_per_engine_plus_global
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_per_engine_plus_global(self):
canary = Mock()
event.listen(Engine, "before_execute", canary.be1)
e1 = testing_engine(config.db_url)
e2 = testing_engine(config.db_url)
event.listen(e1, "before_execute", canary.be2)
event.listen(Engine, "before_execute", canary.be3)
e1.connect()
e2.connect()
with e1.connect() as conn:
conn.execute(select([1]))
eq_(canary.be1.call_count, 1)
eq_(canary.be2.call_count, 1)
with e2.connect() as conn:
conn.execute(select([1]))
eq_(canary.be1.call_count, 2)
eq_(canary.be2.call_count, 1)
eq_(canary.be3.call_count, 2)
示例9: test_per_connection_plus_engine
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_per_connection_plus_engine(self):
canary = Mock()
e1 = testing_engine(config.db_url)
event.listen(e1, "before_execute", canary.be1)
conn = e1.connect()
event.listen(conn, "before_execute", canary.be2)
conn.execute(select([1]))
eq_(canary.be1.call_count, 1)
eq_(canary.be2.call_count, 1)
if testing.requires.legacy_engine.enabled:
conn._branch().execute(select([1]))
eq_(canary.be1.call_count, 2)
eq_(canary.be2.call_count, 2)
示例10: test_cursor_events_execute
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_cursor_events_execute(self):
canary = Mock()
e1 = testing_engine(config.db_url)
event.listen(e1, "before_cursor_execute", canary.bce)
event.listen(e1, "after_cursor_execute", canary.ace)
stmt = str(select([1]).compile(dialect=e1.dialect))
with e1.connect() as conn:
result = conn.exec_driver_sql(stmt)
ctx = result.context
eq_(
canary.bce.mock_calls,
[call(conn, ctx.cursor, stmt, ctx.parameters[0], ctx, False)],
)
eq_(
canary.ace.mock_calls,
[call(conn, ctx.cursor, stmt, ctx.parameters[0], ctx, False)],
)
示例11: test_options
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_options(self):
canary = []
def execute(conn, *args, **kw):
canary.append("execute")
def cursor_execute(conn, *args, **kw):
canary.append("cursor_execute")
engine = engines.testing_engine()
event.listen(engine, "before_execute", execute)
event.listen(engine, "before_cursor_execute", cursor_execute)
conn = engine.connect()
c2 = conn.execution_options(foo="bar")
eq_(c2._execution_options, {"foo": "bar"})
c2.execute(select([1]))
c3 = c2.execution_options(bar="bat")
eq_(c3._execution_options, {"foo": "bar", "bar": "bat"})
eq_(canary, ["execute", "cursor_execute"])
示例12: test_handle_error
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_handle_error(self):
engine = engines.testing_engine()
canary = Mock(return_value=None)
event.listen(engine, "handle_error", canary)
with engine.connect() as conn:
try:
conn.exec_driver_sql("SELECT FOO FROM I_DONT_EXIST")
assert False
except tsa.exc.DBAPIError as e:
ctx = canary.mock_calls[0][1][0]
eq_(ctx.original_exception, e.orig)
is_(ctx.sqlalchemy_exception, e)
eq_(ctx.statement, "SELECT FOO FROM I_DONT_EXIST")
示例13: test_exception_autorollback_fails
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_exception_autorollback_fails(self):
engine = engines.testing_engine()
conn = engine.connect()
def boom(connection):
raise engine.dialect.dbapi.OperationalError("rollback failed")
with expect_warnings(
r"An exception has occurred during handling of a previous "
r"exception. The previous exception "
r"is.*(?:i_dont_exist|does not exist)",
py2konly=True,
):
with patch.object(conn.dialect, "do_rollback", boom):
assert_raises_message(
tsa.exc.OperationalError,
"rollback failed",
conn.exec_driver_sql,
"insert into i_dont_exist (x) values ('y')",
)
示例14: _test_alter_disconnect
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def _test_alter_disconnect(self, orig_error, evt_value):
engine = engines.testing_engine()
@event.listens_for(engine, "handle_error")
def evt(ctx):
ctx.is_disconnect = evt_value
with patch.object(
engine.dialect, "is_disconnect", Mock(return_value=orig_error)
):
with engine.connect() as c:
try:
c.exec_driver_sql("SELECT x FROM nonexistent")
assert False
except tsa.exc.StatementError as st:
eq_(st.connection_invalidated, evt_value)
示例15: test_handle_error_event_connect_isolation_level
# 需要导入模块: from sqlalchemy.testing import engines [as 别名]
# 或者: from sqlalchemy.testing.engines import testing_engine [as 别名]
def test_handle_error_event_connect_isolation_level(self):
engine = engines.testing_engine()
class MySpecialException(Exception):
pass
@event.listens_for(engine, "handle_error")
def handle_error(ctx):
raise MySpecialException("failed operation")
ProgrammingError = engine.dialect.dbapi.ProgrammingError
with engine.connect() as conn:
with patch.object(
conn.dialect,
"get_isolation_level",
Mock(side_effect=ProgrammingError("random error")),
):
assert_raises(MySpecialException, conn.get_isolation_level)