summaryrefslogtreecommitdiff
path: root/oslo_db/tests/sqlalchemy/test_enginefacade.py
diff options
context:
space:
mode:
Diffstat (limited to 'oslo_db/tests/sqlalchemy/test_enginefacade.py')
-rw-r--r--oslo_db/tests/sqlalchemy/test_enginefacade.py148
1 files changed, 148 insertions, 0 deletions
diff --git a/oslo_db/tests/sqlalchemy/test_enginefacade.py b/oslo_db/tests/sqlalchemy/test_enginefacade.py
index 72663d2..fb63856 100644
--- a/oslo_db/tests/sqlalchemy/test_enginefacade.py
+++ b/oslo_db/tests/sqlalchemy/test_enginefacade.py
@@ -1076,6 +1076,154 @@ class MockFacadeTest(oslo_test_base.BaseTestCase):
session.execute("test")
+class PatchFactoryTest(oslo_test_base.BaseTestCase):
+
+ 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)
+
+ 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)
+
+ def test_patch_engine(self):
+ normal_mgr = enginefacade.transaction_context()
+ normal_mgr.configure(connection="sqlite:///foo.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")
+
+ def create_engine(sql_connection, **kw):
+ return mock.Mock(url=sql_connection)
+
+ with mock.patch(
+ "oslo_db.sqlalchemy.engines.create_engine", create_engine):
+ mock_engine = create_engine("sqlite:///bar.db")
+
+ context = oslo_context.RequestContext()
+ go1(context)
+ reset = normal_mgr.patch_engine(mock_engine)
+ go2(context)
+ self.assertIs(
+ normal_mgr._factory._writer_engine, mock_engine)
+ reset()
+ go1(context)
+
+ 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"])
+
+
class SynchronousReaderWSlaveMockFacadeTest(MockFacadeTest):
synchronous_reader = True