def test_ping_not_on_first_connect(self): pool = self._pool_fixture(pre_ping=True, pool_kw=dict(pool_size=1, max_overflow=0)) conn = pool.connect() dbapi_conn = conn.dbapi_connection eq_(dbapi_conn.mock_calls, []) conn.close() # no ping, so no cursor() call. eq_(dbapi_conn.mock_calls, [call.rollback()]) conn = pool.connect() is_(conn.dbapi_connection, dbapi_conn) # ping, so cursor() call. eq_(dbapi_conn.mock_calls, [call.rollback(), call.cursor()]) conn.close() conn = pool.connect() is_(conn.dbapi_connection, dbapi_conn) # ping, so cursor() call. eq_( dbapi_conn.mock_calls, [call.rollback(), call.cursor(), call.rollback(), call.cursor()], ) conn.close()
def test_executemany(self): self.executor.executemany("a query") self.assertEqual([call.debug('%s (%s, %s)', 'a query', (), {})], self.logger.mock_calls) self.assertEqual([call.cursor(), call.cursor().executemany('a query')], self.connection.mock_calls)
def test_copy_stream(self): self.executor.copy_stream(SQLTable("table", [], []), BytesIO(b"data"), "utf-8", unix_dialect) self.assertEqual([ call.debug('%s (%s, %s)', 'INSERT INTO table VALUES ()', mock.ANY, {}) ], self.logger.mock_calls) self.assertEqual([ call.cursor(), call.cursor().executemany('INSERT INTO table VALUES ()', mock.ANY) ], self.connection.mock_calls)
def test_copy_stream(self): print(csv.unix_dialect.__dict__) self.executor.copy_stream(SQLTable("table", [], []), BytesIO(b"data"), "utf-8", csv.unix_dialect) # self.assertEqual([call.debug("COPY table FROM STDIN WITH # (FORMAT CSV, " # "HEADER TRUE, ENCODING 'UTF_8')")], # self.logger.mock_calls) self.assertEqual([ call.cursor(), call.cursor().execute( "COPY table FROM STDIN WITH (FORMAT CSV, " "HEADER TRUE, ENCODING 'UTF_8')", stream=mock.ANY) ], self.connection.mock_calls)
def test_ping_not_on_reconnect(self): pool = self._pool_fixture(pre_ping=True, pool_kw=dict(pool_size=1, max_overflow=0)) conn = pool.connect() dbapi_conn = conn.dbapi_connection conn_rec = conn._connection_record eq_(dbapi_conn.mock_calls, []) conn.close() conn = pool.connect() is_(conn.dbapi_connection, dbapi_conn) # ping, so cursor() call. eq_(dbapi_conn.mock_calls, [call.rollback(), call.cursor()]) conn.invalidate() is_(conn.dbapi_connection, None) # connect again, make sure we're on the same connection record conn = pool.connect() is_(conn._connection_record, conn_rec) # no ping dbapi_conn = conn.dbapi_connection eq_(dbapi_conn.mock_calls, [])
def test_pre_ping_weakref_finalizer(self): pool = self._pool_fixture(pre_ping=True) conn = pool.connect() old_dbapi_conn = conn.dbapi_connection conn.close() # no cursor() because no pre ping eq_(old_dbapi_conn.mock_calls, [call.rollback()]) conn = pool.connect() conn.close() # connect again, we see pre-ping eq_( old_dbapi_conn.mock_calls, [call.rollback(), call.cursor(), call.rollback()], ) self.dbapi.shutdown("execute", stop=True) self.dbapi.restart() conn = pool.connect() dbapi_conn = conn.dbapi_connection del conn gc_collect() # new connection was reset on return appropriately eq_(dbapi_conn.mock_calls, [call.rollback()]) # old connection was just closed - did not get an # erroneous reset on return eq_( old_dbapi_conn.mock_calls, [ call.rollback(), call.cursor(), call.rollback(), call.cursor(), call.close(), ], )
def test_normal(self): logger: Logger = Mock() connection: Connection = Mock() qe = NormalQueryExecutor(logger, connection) qe.execute_all(["sql1", "sql2"], stream="s") qe.commit() self.assertEqual([ call.debug('Execute: %s (args=%s, stream=%s)', 'sql1', (), {'stream': 's'}), call.debug('Execute: %s (args=%s, stream=%s)', 'sql2', (), {'stream': 's'}), call.debug('Commit') ], logger.mock_calls) self.assertEqual([ call.cursor(), call.cursor().execute('sql1', stream='s'), call.cursor().execute('sql2', stream='s'), call.commit() ], connection.mock_calls)
def test_execute(self): self.executor.execute(["a query"]) self.assertEqual([call.debug("a query")], self.logger.mock_calls) self.assertEqual( [call.cursor(), call.cursor().execute('a query')], self.connection.mock_calls)
def test_execute_empty(self): self.executor.execute([]) self.assertEqual([], self.logger.mock_calls) self.assertEqual([call.cursor()], self.connection.mock_calls)
def test_commit(self): self.executor.commit() self.assertEqual([call.debug('commit')], self.logger.mock_calls) self.assertEqual([call.cursor(), call.commit()], self.connection.mock_calls)
from unittest import mock from unittest.mock import call # * can be used to prepare assertations if __name__ == "__main__": mock = mock.Mock() cursor = mock.connection.cursor.return_value cursor.execute.return_value = ["foo"] cur = mock.connection.cursor() cur.execute("select * from DUAL") expected = [ call.connection.cursor(), call.cursor().execute("select * from DUAL") ] mock.mock_calls == expected # * True