コード例 #1
0
ファイル: test_steady_db.py プロジェクト: cctvadmin/DBUtils
 def test_rollback_error(self):
     db = SteadyDBconnect(dbapi, database='ok')
     db.begin()
     self.assertFalse(db._con.session)
     self.assertTrue(db._con.valid)
     db.rollback()
     self.assertEqual(db._con.session, ['rollback'])
     self.assertTrue(db._con.valid)
     db.begin()
     db._con.valid = False
     con = db._con
     self.assertRaises(dbapi.InternalError, db.rollback)
     self.assertFalse(db._con.session)
     self.assertTrue(db._con.valid)
     self.assertIsNot(con, db._con)
     db.begin()
     self.assertFalse(db._con.session)
     self.assertTrue(db._con.valid)
     db.rollback()
     self.assertEqual(db._con.session, ['rollback'])
     self.assertTrue(db._con.valid)
コード例 #2
0
ファイル: test_steady_db.py プロジェクト: cctvadmin/DBUtils
 def test_connection(self):
     db = SteadyDBconnect(dbapi,
                          0,
                          None,
                          None,
                          None,
                          True,
                          'SteadyDBTestDB',
                          user='******')
     self.assertTrue(isinstance(db, SteadyDBConnection))
     self.assertTrue(hasattr(db, '_con'))
     self.assertTrue(hasattr(db, '_usage'))
     self.assertEqual(db._usage, 0)
     self.assertTrue(hasattr(db._con, 'valid'))
     self.assertTrue(db._con.valid)
     self.assertTrue(hasattr(db._con, 'cursor'))
     self.assertTrue(hasattr(db._con, 'close'))
     self.assertTrue(hasattr(db._con, 'open_cursors'))
     self.assertTrue(hasattr(db._con, 'num_uses'))
     self.assertTrue(hasattr(db._con, 'num_queries'))
     self.assertTrue(hasattr(db._con, 'session'))
     self.assertTrue(hasattr(db._con, 'database'))
     self.assertEqual(db._con.database, 'SteadyDBTestDB')
     self.assertTrue(hasattr(db._con, 'user'))
     self.assertEqual(db._con.user, 'SteadyDBTestUser')
     self.assertTrue(hasattr(db, 'cursor'))
     self.assertTrue(hasattr(db, 'close'))
     self.assertEqual(db._con.open_cursors, 0)
     for i in range(3):
         cursor = db.cursor()
         self.assertEqual(db._con.open_cursors, 1)
         cursor.close()
         self.assertEqual(db._con.open_cursors, 0)
     cursor = []
     for i in range(3):
         cursor.append(db.cursor())
         self.assertEqual(db._con.open_cursors, i + 1)
     del cursor
     self.assertEqual(db._con.open_cursors, 0)
     cursor = db.cursor()
     self.assertTrue(hasattr(cursor, 'execute'))
     self.assertTrue(hasattr(cursor, 'fetchone'))
     self.assertTrue(hasattr(cursor, 'callproc'))
     self.assertTrue(hasattr(cursor, 'close'))
     self.assertTrue(hasattr(cursor, 'valid'))
     self.assertTrue(cursor.valid)
     self.assertEqual(db._con.open_cursors, 1)
     for i in range(3):
         self.assertEqual(db._usage, i)
         self.assertEqual(db._con.num_uses, i)
         self.assertEqual(db._con.num_queries, i)
         cursor.execute('select test%d' % i)
         self.assertEqual(cursor.fetchone(), 'test%d' % i)
     self.assertTrue(cursor.valid)
     self.assertEqual(db._con.open_cursors, 1)
     for i in range(4):
         cursor.callproc('test')
     cursor.close()
     self.assertFalse(cursor.valid)
     self.assertEqual(db._con.open_cursors, 0)
     self.assertEqual(db._usage, 7)
     self.assertEqual(db._con.num_uses, 7)
     self.assertEqual(db._con.num_queries, 3)
     cursor.close()
     cursor.execute('select test8')
     self.assertTrue(cursor.valid)
     self.assertEqual(db._con.open_cursors, 1)
     self.assertEqual(cursor.fetchone(), 'test8')
     self.assertEqual(db._usage, 8)
     self.assertEqual(db._con.num_uses, 8)
     self.assertEqual(db._con.num_queries, 4)
     self.assertTrue(db._con.valid)
     db.close()
     self.assertFalse(db._con.valid)
     self.assertEqual(db._con.open_cursors, 0)
     self.assertEqual(db._usage, 8)
     self.assertEqual(db._con.num_uses, 0)
     self.assertEqual(db._con.num_queries, 0)
     self.assertRaises(dbapi.InternalError, db._con.close)
     db.close()
     self.assertRaises(dbapi.InternalError, db._con.cursor)
     cursor = db.cursor()
     self.assertTrue(db._con.valid)
     cursor.execute('select test11')
     self.assertEqual(cursor.fetchone(), 'test11')
     cursor.execute('select test12')
     self.assertEqual(cursor.fetchone(), 'test12')
     cursor.callproc('test')
     self.assertEqual(db._usage, 3)
     self.assertEqual(db._con.num_uses, 3)
     self.assertEqual(db._con.num_queries, 2)
     cursor2 = db.cursor()
     self.assertEqual(db._con.open_cursors, 2)
     cursor2.execute('select test13')
     self.assertEqual(cursor2.fetchone(), 'test13')
     self.assertEqual(db._con.num_queries, 3)
     db.close()
     self.assertEqual(db._con.open_cursors, 0)
     self.assertEqual(db._con.num_queries, 0)
     cursor = db.cursor()
     self.assertTrue(cursor.valid)
     cursor.callproc('test')
     cursor._cursor.valid = False
     self.assertFalse(cursor.valid)
     self.assertRaises(dbapi.InternalError, cursor._cursor.callproc, 'test')
     cursor.callproc('test')
     self.assertTrue(cursor.valid)
     cursor._cursor.callproc('test')
     self.assertEqual(db._usage, 2)
     self.assertEqual(db._con.num_uses, 3)
     db._con.valid = cursor._cursor.valid = False
     cursor.callproc('test')
     self.assertTrue(cursor.valid)
     self.assertEqual(db._usage, 1)
     self.assertEqual(db._con.num_uses, 1)
     cursor.execute('set this')
     db.commit()
     cursor.execute('set that')
     db.rollback()
     self.assertEqual(db._con.session,
                      ['this', 'commit', 'that', 'rollback'])