def test_pool_simple_query_executemany(self): """Creates a pool with a single connection and does SELECT 2+2 with that with executemany""" cp = ConnectionPool(self.dd) with cp.cursor() as cur: cur.executemany('SELECT 2+2', ()) cp.close()
def test_transaction(self): """Creates a pool with a single connection and does a simple transaction on it""" cp = ConnectionPool(self.dd) with cp.transaction() as cur: cur.execute('SELECT 2+2') cur.execute('SELECT 2+2') cur.execute('SELECT 2+2') cp.close()
def test_pool_simple_query(self): """Creates a pool with a single connection and does SELECT 2+2 with that""" cp = ConnectionPool(self.dd) with cp.cursor() as cur: cur.execute('SELECT 2+2') a, = cur.fetchone() self.assertEquals(a, 4) cp.close()
def test_pool_regenerate_query(self): """Creates a pool with a single connection and does SELECT 2+2 with that. Regenerates connections in the meantime""" cp = ConnectionPool(self.dd) cp.regenerate_all() with cp.cursor() as cur: cur.execute('SELECT 2+2') a, = cur.fetchone() self.assertEquals(a, 4) cp.close()
def test_pool_onetime_reconnect(self): """Creates a pool with a single connection, get the connection, close it and return it, anticipating that it will be regenerated""" cp = ConnectionPool(self.dd) # get the connection, close it and put it back c = cp.get_connection() c.close() cp.put_connection(c) # do the query, anticipating cursor regen with cp.cursor() as cur: cur.execute('SELECT 2+2') a, = cur.fetchone() self.assertEquals(a, 4) cp.close()
def test_threads(self): """Pool, two threads, basic queries""" class TT(BaseThread): def __init__(self, cp, tc): BaseThread.__init__(self) self.cp = cp #: connection pool self.tc = tc #: core test case def run(self): for x in xrange(0, 10): with self.cp.cursor() as cur: cur.execute('SELECT 2+2') a, = cur.fetchone() self.tc.assertEquals(a, 4) cp = ConnectionPool(self.dd, 2) a = TT(cp, self) b = TT(cp, self) a.start(), b.start() cp.close()