Skip to content

Commit 024ed8e

Browse files
committed
Improved unit tests, found and fixed comparison bug.
1 parent 41d2d51 commit 024ed8e

6 files changed

Lines changed: 132 additions & 46 deletions

File tree

DBUtils/PooledDB.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def __lt__(self, other):
465465

466466
def __le__(self, other):
467467
if self.con._transaction == other.con._transaction:
468-
return self.shared == other.shared
468+
return self.shared <= other.shared
469469
else:
470470
return not self.con._transaction
471471

DBUtils/Tests/TestPersistentDB.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test1_NoThreadsafety(self):
4343
for dbapi.threadsafety in (None, 0):
4444
self.assertRaises(NotSupportedError, PersistentDB, dbapi)
4545

46-
def test2_PersistentDBClose(self):
46+
def test2_Close(self):
4747
for closeable in (False, True):
4848
persist = PersistentDB(dbapi, closeable=closeable)
4949
db = persist.connection()
@@ -57,7 +57,7 @@ def test2_PersistentDBClose(self):
5757
db._close()
5858
self.assert_(not db._con.valid)
5959

60-
def test3_PersistentDBConnection(self):
60+
def test3_Connection(self):
6161
persist = PersistentDB(dbapi)
6262
db = persist.connection()
6363
db_con = db._con
@@ -71,7 +71,7 @@ def test3_PersistentDBConnection(self):
7171
db2.close()
7272
db.close()
7373

74-
def test4_PersistentDBThreads(self):
74+
def test4_Threads(self):
7575
numThreads = 3
7676
persist = PersistentDB(dbapi, closeable=True)
7777
from Queue import Queue, Empty
@@ -181,7 +181,7 @@ def runQueries(i):
181181
except TypeError:
182182
queryQueue[i].put(None, 1)
183183

184-
def test5_PersistentDBMaxUsage(self):
184+
def test5_MaxUsage(self):
185185
persist = PersistentDB(dbapi, 20)
186186
db = persist.connection()
187187
self.assertEqual(db._maxusage, 20)
@@ -197,7 +197,7 @@ def test5_PersistentDBMaxUsage(self):
197197
self.assertEqual(db._con.num_uses, j)
198198
self.assertEqual(db._con.num_queries, j)
199199

200-
def test6_PersistentDBSetSession(self):
200+
def test6_SetSession(self):
201201
persist = PersistentDB(dbapi, 3, ('set datestyle',))
202202
db = persist.connection()
203203
self.assertEqual(db._maxusage, 3)
@@ -215,7 +215,7 @@ def test6_PersistentDBSetSession(self):
215215
cursor.close()
216216
self.assertEqual(db._con.session, ['datestyle'])
217217

218-
def test7_PersistentDBThreadLocal(self):
218+
def test7_ThreadLocal(self):
219219
persist = PersistentDB(dbapi)
220220
self.assert_(isinstance(persist.thread, ThreadingLocal.local))
221221
class threadlocal:

DBUtils/Tests/TestPersistentPg.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test0_CheckVersion(self):
3434
self.assertEqual(PersistentPgVersion, __version__)
3535
self.assertEqual(PersistentPg.version, __version__)
3636

37-
def test1_PersistentPgClose(self):
37+
def test1_Close(self):
3838
for closeable in (False, True):
3939
persist = PersistentPg(closeable=closeable)
4040
db = persist.connection()
@@ -50,7 +50,7 @@ def test1_PersistentPgClose(self):
5050
db._close()
5151
self.assert_(not db._con.db or not db._con.valid)
5252

53-
def test2_PersistentPgThreads(self):
53+
def test2_Threads(self):
5454
numThreads = 3
5555
persist = PersistentPg()
5656
from Queue import Queue, Empty
@@ -155,7 +155,7 @@ def runQueries(i):
155155
except TypeError:
156156
queryQueue[i].put(None, 1)
157157

158-
def test3_PersistentPgMaxUsage(self):
158+
def test3_MaxUsage(self):
159159
persist = PersistentPg(20)
160160
db = persist.connection()
161161
self.assertEqual(db._maxusage, 20)
@@ -167,7 +167,7 @@ def test3_PersistentPgMaxUsage(self):
167167
self.assertEqual(db._usage, j)
168168
self.assertEqual(db.num_queries, j)
169169

170-
def test4_PersistentPgSetSession(self):
170+
def test4_SetSession(self):
171171
persist = PersistentPg(3, ('set datestyle',))
172172
db = persist.connection()
173173
self.assertEqual(db._maxusage, 3)
@@ -179,7 +179,7 @@ def test4_PersistentPgSetSession(self):
179179
db.query('select test')
180180
self.assertEqual(db.db.session, ['datestyle'])
181181

182-
def test5_PersistentPgFailedTransaction(self):
182+
def test5_FailedTransaction(self):
183183
persist = PersistentPg()
184184
db = persist.connection()
185185
db._con.close()

DBUtils/Tests/TestPooledDB.py

Lines changed: 101 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
sys.path.insert(1, '../..')
2323
# The TestSteadyDB module serves as a mock object for the DB-API 2 module:
2424
from DBUtils.Tests import TestSteadyDB as dbapi
25-
from DBUtils.PooledDB import PooledDB, TooManyConnections
25+
from DBUtils.PooledDB import PooledDB, TooManyConnections, SharedDBConnection
2626

2727

2828
class TestPooledDB(unittest.TestCase):
@@ -393,7 +393,7 @@ def test06_ShareableConnection(self):
393393
else:
394394
self.assertEqual(len(pool._idle_cache), 3)
395395

396-
def test07_MinMaxCached(self):
396+
def test08_MinMaxCached(self):
397397
for threadsafety in (1, 2):
398398
dbapi.threadsafety = threadsafety
399399
shareable = threadsafety > 1
@@ -507,7 +507,26 @@ def test08_MaxShared(self):
507507
if shareable:
508508
self.assertEqual(len(pool._shared_cache), 7)
509509

510-
def test09_EquallyShared(self):
510+
def test09_SortShared(self):
511+
dbapi.threadsafety = 2
512+
pool = PooledDB(dbapi, 0, 4, 4)
513+
cache = []
514+
for i in range(6):
515+
db = pool.connection()
516+
db.cursor().execute('select test')
517+
cache.append(db)
518+
for i, db in enumerate(cache):
519+
self.assertEqual(db._shared_con.shared, 2 <= i < 4 and 1 or 2)
520+
cache[2].begin()
521+
cache[3].begin()
522+
db = pool.connection()
523+
self.assert_(db._con is cache[0]._con)
524+
db.close()
525+
cache[3].rollback()
526+
db = pool.connection()
527+
self.assert_(db._con is cache[3]._con)
528+
529+
def test10_EquallyShared(self):
511530
for threadsafety in (1, 2):
512531
dbapi.threadsafety = threadsafety
513532
shareable = threadsafety > 1
@@ -542,7 +561,7 @@ def test09_EquallyShared(self):
542561
if shareable:
543562
self.assertEqual(len(pool._shared_cache), 0)
544563

545-
def test10_SortShared(self):
564+
def test11_ManyShared(self):
546565
for threadsafety in (1, 2):
547566
dbapi.threadsafety = threadsafety
548567
shareable = threadsafety > 1
@@ -593,7 +612,7 @@ def test10_SortShared(self):
593612
else:
594613
self.assertEqual(len(pool._idle_cache), 35)
595614

596-
def test11_Rollback(self):
615+
def test12_Rollback(self):
597616
for threadsafety in (1, 2):
598617
dbapi.threadsafety = threadsafety
599618
pool = PooledDB(dbapi, 0, 1)
@@ -625,7 +644,7 @@ def test11_Rollback(self):
625644
'doit1', 'commit', 'dont1', 'rollback',
626645
'doit2', 'commit', 'rollback'])
627646

628-
def test12_MaxConnections(self):
647+
def test13_MaxConnections(self):
629648
for threadsafety in (1, 2):
630649
dbapi.threadsafety = threadsafety
631650
shareable = threadsafety > 1
@@ -827,7 +846,7 @@ def connection():
827846
self.assertEqual(session, ['rollback',
828847
'rollback', 'thread', 'rollback'])
829848

830-
def test13_MaxUsage(self):
849+
def test14_MaxUsage(self):
831850
for threadsafety in (1, 2):
832851
dbapi.threadsafety = threadsafety
833852
for maxusage in (0, 3, 7):
@@ -862,7 +881,7 @@ def test13_MaxUsage(self):
862881
self.assertEqual(db._con._con.num_uses, j + 1)
863882
self.assertEqual(db._con._con.num_queries, j)
864883

865-
def test14_SetSession(self):
884+
def test15_SetSession(self):
866885
for threadsafety in (1, 2):
867886
dbapi.threadsafety = threadsafety
868887
setsession = ('set time zone', 'set datestyle')
@@ -890,7 +909,7 @@ def test14_SetSession(self):
890909
self.assertEqual(db._con._con.session,
891910
['time zone', 'datestyle', 'test2'])
892911

893-
def test15_OneThreadTwoConnections(self):
912+
def test16_OneThreadTwoConnections(self):
894913
for threadsafety in (1, 2):
895914
dbapi.threadsafety = threadsafety
896915
shareable = threadsafety > 1
@@ -947,7 +966,7 @@ def test15_OneThreadTwoConnections(self):
947966
self.assertNotEqual(db1, db2)
948967
self.assertNotEqual(db1._con, db2._con)
949968

950-
def test16_ThreeThreadsTwoConnections(self):
969+
def test17_ThreeThreadsTwoConnections(self):
951970
for threadsafety in (1, 2):
952971
dbapi.threadsafety = threadsafety
953972
pool = PooledDB(dbapi, 2, 2, 0, 2, True)
@@ -1004,7 +1023,7 @@ def connection():
10041023
self.assertNotEqual(db1._con, db2._con)
10051024
self.assertEqual(db1._con, db1_con)
10061025

1007-
def test17_PingCheck(self):
1026+
def test18_PingCheck(self):
10081027
Connection = dbapi.Connection
10091028
Connection.has_ping = True
10101029
Connection.num_pings = 0
@@ -1072,7 +1091,7 @@ def test17_PingCheck(self):
10721091
Connection.has_ping = False
10731092
Connection.num_pings = 0
10741093

1075-
def test18_FailedTransaction(self):
1094+
def test19_FailedTransaction(self):
10761095
dbapi.threadsafety = 2
10771096
pool = PooledDB(dbapi, 0, 1, 1)
10781097
db = pool.connection()
@@ -1101,15 +1120,35 @@ def test18_FailedTransaction(self):
11011120
db._con._con.close()
11021121
cursor.execute('select test')
11031122

1104-
def test19_AllSharedInTransaction(self):
1123+
def test20_SharedInTransaction(self):
11051124
dbapi.threadsafety = 2
11061125
pool = PooledDB(dbapi, 0, 1, 1)
11071126
db = pool.connection()
11081127
db.begin()
1109-
pool.connection(0)
1128+
pool.connection(False)
1129+
self.assertRaises(TooManyConnections, pool.connection)
1130+
pool = PooledDB(dbapi, 0, 2, 2)
1131+
db1 = pool.connection()
1132+
db2 = pool.connection()
1133+
self.assert_(db2._con is not db1._con)
1134+
db2.close()
1135+
db2 = pool.connection()
1136+
self.assert_(db2._con is not db1._con)
1137+
db = pool.connection()
1138+
self.assert_(db._con is db1._con)
1139+
db.close()
1140+
db1.begin()
1141+
db = pool.connection()
1142+
self.assert_(db._con is db2._con)
1143+
db.close()
1144+
db2.begin()
1145+
pool.connection(False)
11101146
self.assertRaises(TooManyConnections, pool.connection)
1147+
db1.rollback()
1148+
db = pool.connection()
1149+
self.assert_(db._con is db1._con)
11111150

1112-
def test20_ResetTransaction(self):
1151+
def test21_ResetTransaction(self):
11131152
pool = PooledDB(dbapi, 1, 1, 0)
11141153
db = pool.connection()
11151154
db.begin()
@@ -1132,5 +1171,52 @@ def test20_ResetTransaction(self):
11321171
self.assertEqual(con._con.session, ['rollback'])
11331172

11341173

1174+
class TestSharedDBConnection(unittest.TestCase):
1175+
1176+
def test01_CreateConnection(self):
1177+
db_con = dbapi.connect()
1178+
con = SharedDBConnection(db_con)
1179+
self.assertEqual(con.con, db_con)
1180+
self.assertEqual(con.shared, 1)
1181+
1182+
def test01_ShareAndUnshare(self):
1183+
con = SharedDBConnection(dbapi.connect())
1184+
self.assertEqual(con.shared, 1)
1185+
con.share()
1186+
self.assertEqual(con.shared, 2)
1187+
con.share()
1188+
self.assertEqual(con.shared, 3)
1189+
con.unshare()
1190+
self.assertEqual(con.shared, 2)
1191+
con.unshare()
1192+
self.assertEqual(con.shared, 1)
1193+
1194+
def test02_Comparison(self):
1195+
con1 = SharedDBConnection(dbapi.connect())
1196+
con1.con._transaction = False
1197+
con2 = SharedDBConnection(dbapi.connect())
1198+
con2.con._transaction = False
1199+
self.assert_(con1 == con2)
1200+
self.assert_(con1 <= con2)
1201+
self.assert_(con1 >= con2)
1202+
self.assert_(not con1 != con2)
1203+
self.assert_(not con1 < con2)
1204+
self.assert_(not con1 > con2)
1205+
con2.share()
1206+
self.assert_(not con1 == con2)
1207+
self.assert_(con1 <= con2)
1208+
self.assert_(not con1 >= con2)
1209+
self.assert_(con1 != con2)
1210+
self.assert_(con1 < con2)
1211+
self.assert_(not con1 > con2)
1212+
con1.con._transaction = True
1213+
self.assert_(not con1 == con2)
1214+
self.assert_(not con1 <= con2)
1215+
self.assert_(con1 >= con2)
1216+
self.assert_(con1 != con2)
1217+
self.assert_(not con1 < con2)
1218+
self.assert_(con1 > con2)
1219+
1220+
11351221
if __name__ == '__main__':
11361222
unittest.main()

0 commit comments

Comments
 (0)