本文整理匯總了Python中oslo_db.sqlalchemy.enginefacade.transaction_context方法的典型用法代碼示例。如果您正苦於以下問題:Python enginefacade.transaction_context方法的具體用法?Python enginefacade.transaction_context怎麽用?Python enginefacade.transaction_context使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類oslo_db.sqlalchemy.enginefacade
的用法示例。
在下文中一共展示了enginefacade.transaction_context方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_enginefacade
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def get_enginefacade(self):
"""Return an enginefacade._TransactionContextManager.
This is typically a global variable like "context_manager" declared
in the db/api.py module and is the object returned by
enginefacade.transaction_context().
If left not implemented, the global enginefacade manager is used.
For the case where a project uses per-object or per-test enginefacades
like Gnocchi, the get_per_test_enginefacade()
method should also be implemented.
"""
return enginefacade._context_manager
示例2: test_dispose_pool
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def test_dispose_pool(self):
facade = enginefacade.transaction_context()
facade.configure(
connection=self.engine_uri,
)
facade.dispose_pool()
self.assertFalse(hasattr(facade._factory, '_writer_engine'))
facade._factory._start()
facade.dispose_pool()
self.assertEqual(
facade._factory._writer_engine.pool.mock_calls,
[mock.call.dispose()]
)
示例3: test_dispose_pool_w_reader
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def test_dispose_pool_w_reader(self):
facade = enginefacade.transaction_context()
facade.configure(
connection=self.engine_uri,
slave_connection=self.slave_uri
)
facade.dispose_pool()
self.assertFalse(hasattr(facade._factory, '_writer_engine'))
self.assertFalse(hasattr(facade._factory, '_reader_engine'))
facade._factory._start()
facade.dispose_pool()
self.assertEqual(
facade._factory._writer_engine.pool.mock_calls,
[mock.call.dispose()]
)
self.assertEqual(
facade._factory._reader_engine.pool.mock_calls,
[mock.call.dispose()]
)
示例4: test_setup_for_connection_called_with_profiler
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def test_setup_for_connection_called_with_profiler(self):
context_manager = enginefacade.transaction_context()
context_manager.configure(connection='sqlite://')
hook = mock.Mock()
context_manager.append_on_engine_create(hook)
self.assertEqual(
[hook], context_manager._factory._facade_cfg['on_engine_create'])
@context_manager.reader
def go(context):
hook.assert_called_once_with(context.session.bind)
go(oslo_context.RequestContext())
# TODO(zzzeek): test configuration options, e.g. like
# test_sqlalchemy->test_creation_from_config
示例5: __init__
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def __init__(self, conf):
self.trans = enginefacade.transaction_context()
self.trans.configure(
**dict(conf.database.items())
)
self._context = threading.local()
示例6: create_context_manager
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def create_context_manager(connection=None):
"""Create a database context manager object.
: param connection: The database connection string
"""
ctxt_mgr = enginefacade.transaction_context()
ctxt_mgr.configure(**_get_db_conf(CONF.database, connection=connection))
return ctxt_mgr
示例7: create_context_manager
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def create_context_manager(connection=None):
"""Create a database context manager object.
: param connection: The database connection string
"""
ctxt_mgr = enginefacade.transaction_context()
ctxt_mgr.configure(**_get_db_conf(CONF.database, connection=connection))
return ctxt_mgr
示例8: test_db_fixture
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def test_db_fixture(self):
normal_mgr = enginefacade.transaction_context()
normal_mgr.configure(
connection="sqlite://",
sqlite_fk=True,
mysql_sql_mode="FOOBAR",
max_overflow=38
)
class MyFixture(test_fixtures.OpportunisticDbFixture):
def get_enginefacade(self):
return normal_mgr
test = mock.Mock(SCHEMA_SCOPE=None)
fixture = MyFixture(test=test)
resources = fixture._get_resources()
testresources.setUpResources(test, resources, None)
self.addCleanup(
testresources.tearDownResources,
test, resources, None
)
fixture.setUp()
self.addCleanup(fixture.cleanUp)
self.assertTrue(normal_mgr._factory._started)
test.engine = normal_mgr.writer.get_engine()
self.assertEqual("sqlite://", str(test.engine.url))
self.assertIs(test.engine, normal_mgr._factory._writer_engine)
engine_args = normal_mgr._factory._engine_args_for_conf(None)
self.assertTrue(engine_args['sqlite_fk'])
self.assertEqual("FOOBAR", engine_args["mysql_sql_mode"])
self.assertEqual(38, engine_args["max_overflow"])
fixture.cleanUp()
fixture._clear_cleanups() # so the real cleanUp works
self.assertFalse(normal_mgr._factory._started)
示例9: test_started_flag
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def test_started_flag(self):
facade = enginefacade.transaction_context()
self.assertFalse(facade.is_started)
facade.configure(connection=self.engine_uri)
facade.writer.get_engine()
self.assertTrue(facade.is_started)
示例10: test_multiple_factories
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def test_multiple_factories(self):
"""Test that the instrumentation applied to a context class is
independent of a specific _TransactionContextManager
/ _TransactionFactory.
"""
mgr1 = enginefacade.transaction_context()
mgr1.configure(
connection=self.engine_uri,
slave_connection=self.slave_uri
)
mgr2 = enginefacade.transaction_context()
mgr2.configure(
connection=self.engine_uri,
slave_connection=self.slave_uri
)
context = oslo_context.RequestContext()
self.assertRaises(
exception.NoEngineContextEstablished,
getattr, context, 'session'
)
with mgr1.writer.using(context):
self.assertIs(context.transaction_ctx.factory, mgr1._factory)
self.assertIsNot(context.transaction_ctx.factory, mgr2._factory)
self.assertIsNotNone(context.session)
self.assertRaises(
exception.NoEngineContextEstablished,
getattr, context, 'session'
)
with mgr2.writer.using(context):
self.assertIsNot(context.transaction_ctx.factory, mgr1._factory)
self.assertIs(context.transaction_ctx.factory, mgr2._factory)
self.assertIsNotNone(context.session)
示例11: test_multiple_factories_nested
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def test_multiple_factories_nested(self):
"""Test that the instrumentation applied to a context class supports
nested calls among multiple _TransactionContextManager objects.
"""
mgr1 = enginefacade.transaction_context()
mgr1.configure(
connection=self.engine_uri,
slave_connection=self.slave_uri
)
mgr2 = enginefacade.transaction_context()
mgr2.configure(
connection=self.engine_uri,
slave_connection=self.slave_uri
)
context = oslo_context.RequestContext()
with mgr1.writer.using(context):
self.assertIs(context.transaction_ctx.factory, mgr1._factory)
self.assertIsNot(context.transaction_ctx.factory, mgr2._factory)
with mgr2.writer.using(context):
self.assertIsNot(
context.transaction_ctx.factory, mgr1._factory)
self.assertIs(context.transaction_ctx.factory, mgr2._factory)
self.assertIsNotNone(context.session)
# mgr1 is restored
self.assertIs(context.transaction_ctx.factory, mgr1._factory)
self.assertIsNot(context.transaction_ctx.factory, mgr2._factory)
self.assertIsNotNone(context.session)
self.assertRaises(
exception.NoEngineContextEstablished,
getattr, context, 'transaction_ctx'
)
示例12: test_patch_manager
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def test_patch_manager(self):
normal_mgr = enginefacade.transaction_context()
normal_mgr.configure(connection="sqlite:///foo.db")
alt_mgr = enginefacade.transaction_context()
alt_mgr.configure(connection="sqlite:///bar.db")
@normal_mgr.writer
def go1(context):
s1 = context.session
self.assertEqual(
s1.bind.url, "sqlite:///foo.db")
self.assertIs(
s1.bind,
normal_mgr._factory._writer_engine)
@normal_mgr.writer
def go2(context):
s1 = context.session
self.assertEqual(
s1.bind.url,
"sqlite:///bar.db")
self.assertIs(
normal_mgr._factory._writer_engine,
alt_mgr._factory._writer_engine
)
def create_engine(sql_connection, **kw):
return mock.Mock(url=sql_connection)
with mock.patch(
"oslo_db.sqlalchemy.engines.create_engine", create_engine):
context = oslo_context.RequestContext()
go1(context)
reset = normal_mgr.patch_factory(alt_mgr)
go2(context)
reset()
go1(context)
示例13: test_patch_factory
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def test_patch_factory(self):
normal_mgr = enginefacade.transaction_context()
normal_mgr.configure(connection="sqlite:///foo.db")
alt_mgr = enginefacade.transaction_context()
alt_mgr.configure(connection="sqlite:///bar.db")
@normal_mgr.writer
def go1(context):
s1 = context.session
self.assertEqual(
s1.bind.url, "sqlite:///foo.db")
self.assertIs(
s1.bind,
normal_mgr._factory._writer_engine)
@normal_mgr.writer
def go2(context):
s1 = context.session
self.assertEqual(
s1.bind.url,
"sqlite:///bar.db")
self.assertIs(
normal_mgr._factory._writer_engine,
alt_mgr._factory._writer_engine
)
def create_engine(sql_connection, **kw):
return mock.Mock(url=sql_connection)
with mock.patch(
"oslo_db.sqlalchemy.engines.create_engine", create_engine):
context = oslo_context.RequestContext()
go1(context)
reset = normal_mgr.patch_factory(alt_mgr._factory)
go2(context)
reset()
go1(context)
示例14: test_new_manager_from_config
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def test_new_manager_from_config(self):
normal_mgr = enginefacade.transaction_context()
normal_mgr.configure(
connection="sqlite://",
sqlite_fk=True,
mysql_sql_mode="FOOBAR",
max_overflow=38
)
normal_mgr._factory._start()
copied_mgr = normal_mgr.make_new_manager()
self.assertTrue(normal_mgr._factory._started)
self.assertIsNotNone(normal_mgr._factory._writer_engine)
self.assertIsNot(copied_mgr._factory, normal_mgr._factory)
self.assertFalse(copied_mgr._factory._started)
copied_mgr._factory._start()
self.assertIsNot(
normal_mgr._factory._writer_engine,
copied_mgr._factory._writer_engine)
engine_args = copied_mgr._factory._engine_args_for_conf(None)
self.assertTrue(engine_args['sqlite_fk'])
self.assertEqual("FOOBAR", engine_args["mysql_sql_mode"])
self.assertEqual(38, engine_args["max_overflow"])
示例15: test_legacy_facades_from_different_context_managers
# 需要導入模塊: from oslo_db.sqlalchemy import enginefacade [as 別名]
# 或者: from oslo_db.sqlalchemy.enginefacade import transaction_context [as 別名]
def test_legacy_facades_from_different_context_managers(self):
transaction_context1 = enginefacade.transaction_context()
transaction_context2 = enginefacade.transaction_context()
transaction_context1.configure(connection='sqlite:///?conn1')
transaction_context2.configure(connection='sqlite:///?conn2')
legacy1 = transaction_context1.get_legacy_facade()
legacy2 = transaction_context2.get_legacy_facade()
self.assertNotEqual(legacy1, legacy2)