def _fixture( self, dialect_name, exception, num_disconnects, is_disconnect=True): engine = self.engine compat.engine_connect(engine, private_session._connect_ping_listener) real_do_execute = engine.dialect.do_execute counter = itertools.count(1) def fake_do_execute(self, *arg, **kw): if next(counter) > num_disconnects: return real_do_execute(self, *arg, **kw) else: raise exception with self._dbapi_fixture(dialect_name): with test_utils.nested( mock.patch.object(engine.dialect, "do_execute", fake_do_execute), mock.patch.object(engine.dialect, "is_disconnect", mock.Mock(return_value=is_disconnect)) ): yield
def test_branch(self): engine = self.engine listener = mock.Mock() compat.engine_connect(engine, listener) conn = engine.connect() branched = conn.connect() conn.close() self.assertEqual(listener.mock_calls, [mock.call(conn, False), mock.call(branched, True)])
def create_engine(sql_connection, sqlite_fk=False, mysql_sql_mode=None, idle_timeout=3600, connection_debug=0, max_pool_size=None, max_overflow=None, pool_timeout=None, sqlite_synchronous=True, connection_trace=False, max_retries=10, retry_interval=10, thread_checkin=True, logging_name=None): """Return a new SQLAlchemy engine.""" url = sqlalchemy.engine.url.make_url(sql_connection) engine_args = { "pool_recycle": idle_timeout, 'convert_unicode': True, 'connect_args': {}, 'logging_name': logging_name } _setup_logging(connection_debug) _init_connection_args(url, engine_args, sqlite_fk=sqlite_fk, max_pool_size=max_pool_size, max_overflow=max_overflow, pool_timeout=pool_timeout) engine = sqlalchemy.create_engine(url, **engine_args) _init_events(engine, mysql_sql_mode=mysql_sql_mode, sqlite_synchronous=sqlite_synchronous, sqlite_fk=sqlite_fk, thread_checkin=thread_checkin, connection_trace=connection_trace) # register alternate exception handler exc_filters.register_engine(engine) # register engine connect handler compat.engine_connect(engine, _connect_ping_listener) # initial connect + test _test_connection(engine, max_retries, retry_interval) return engine
def test_connect_event(self): engine = self.engine listener = mock.Mock() compat.engine_connect(engine, listener) conn = engine.connect() self.assertEqual(listener.mock_calls, [mock.call(conn, False)]) conn.close() conn2 = engine.connect() conn2.close() self.assertEqual(listener.mock_calls, [mock.call(conn, False), mock.call(conn2, False)])
def create_engine(sql_connection, sqlite_fk=False, mysql_sql_mode=None, idle_timeout=3600, connection_debug=0, max_pool_size=None, max_overflow=None, pool_timeout=None, sqlite_synchronous=True, connection_trace=False, max_retries=10, retry_interval=10, thread_checkin=True, logging_name=None): """Return a new SQLAlchemy engine.""" url = sqlalchemy.engine.url.make_url(sql_connection) engine_args = { "pool_recycle": idle_timeout, 'convert_unicode': True, 'connect_args': {}, 'logging_name': logging_name } _setup_logging(connection_debug) _init_connection_args( url, engine_args, sqlite_fk=sqlite_fk, max_pool_size=max_pool_size, max_overflow=max_overflow, pool_timeout=pool_timeout ) engine = sqlalchemy.create_engine(url, **engine_args) _init_events( engine, mysql_sql_mode=mysql_sql_mode, sqlite_synchronous=sqlite_synchronous, sqlite_fk=sqlite_fk, thread_checkin=thread_checkin, connection_trace=connection_trace ) # register alternate exception handler exc_filters.register_engine(engine) # register engine connect handler compat.engine_connect(engine, _connect_ping_listener) # initial connect + test _test_connection(engine, max_retries, retry_interval) return engine