2222sys .path .insert (1 , '../..' )
2323# The TestSteadyDB module serves as a mock object for the DB-API 2 module:
2424from DBUtils .Tests import TestSteadyDB as dbapi
25- from DBUtils .PooledDB import PooledDB , TooManyConnections
25+ from DBUtils .PooledDB import PooledDB , TooManyConnections , SharedDBConnection
2626
2727
2828class 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+
11351221if __name__ == '__main__' :
11361222 unittest .main ()
0 commit comments