@@ -36,6 +36,9 @@ def connect(database=None, user=None):
3636
3737class Connection :
3838
39+ has_ping = False
40+ num_pings = 0
41+
3942 def __init__ (self , database = None , user = None ):
4043 self .database = database
4144 self .user = user
@@ -45,6 +48,7 @@ def __init__(self, database=None, user=None):
4548 self .open_cursors = 0
4649 self .num_uses = 0
4750 self .num_queries = 0
51+ self .num_pings = 0
4852 self .session = []
4953 self .valid = True
5054
@@ -63,6 +67,14 @@ def commit(self):
6367 def rollback (self ):
6468 self .session .append ('rollback' )
6569
70+ def ping (self ):
71+ cls = self .__class__
72+ cls .num_pings += 1
73+ if not cls .has_ping :
74+ raise AttributeError
75+ if not self .valid :
76+ raise OperationalError
77+
6678 def cursor (self , name = None ):
6779 if not self .valid :
6880 raise InternalError
@@ -200,12 +212,23 @@ def test01_MockedDBConnection(self):
200212 self .assertRaises (InternalError , cursor .close )
201213 self .assertRaises (InternalError , cursor .execute , 'select test' )
202214 self .assert_ (db .valid )
215+ self .assert_ (not db .__class__ .has_ping )
216+ self .assertEqual (db .__class__ .num_pings , 0 )
217+ self .assertRaises (AttributeError , db .ping )
218+ self .assertEqual (db .__class__ .num_pings , 1 )
219+ db .__class__ .has_ping = True
220+ self .assert_ (db .ping () is None )
221+ self .assertEqual (db .__class__ .num_pings , 2 )
203222 db .close ()
204223 self .assert_ (not db .valid )
205224 self .assertEqual (db .num_uses , 0 )
206225 self .assertEqual (db .num_queries , 0 )
207226 self .assertRaises (InternalError , db .close )
208227 self .assertRaises (InternalError , db .cursor )
228+ self .assertRaises (OperationalError , db .ping )
229+ self .assertEqual (db .__class__ .num_pings , 3 )
230+ db .__class__ .has_ping = False
231+ db .__class__ .num_pings = 0
209232
210233 def test02_BrokenDBConnection (self ):
211234 self .assertRaises (TypeError , SteadyDBConnection , None )
@@ -239,7 +262,7 @@ def test03_SteadyDBClose(self):
239262 self .assert_ (not db ._con .valid )
240263
241264 def test04_SteadyDBConnection (self ):
242- db = SteadyDBconnect (dbapi , 0 , None , None , 1 ,
265+ db = SteadyDBconnect (dbapi , 0 , None , None , True , 0 ,
243266 'SteadyDBTestDB' , user = 'SteadyDBTestUser' )
244267 self .assert_ (isinstance (db , SteadyDBConnection ))
245268 self .assert_ (hasattr (db , '_con' ))
@@ -355,9 +378,9 @@ def test04_SteadyDBConnection(self):
355378 ['doit' , 'commit' , 'dont' , 'rollback' ])
356379
357380 def test05_SteadyDBConnectionCreatorFunction (self ):
358- db1 = SteadyDBconnect (dbapi , 0 , None , None , 1 ,
381+ db1 = SteadyDBconnect (dbapi , 0 , None , None , True ,
359382 'SteadyDBTestDB' , user = 'SteadyDBTestUser' )
360- db2 = SteadyDBconnect (connect , 0 , None , None , 1 ,
383+ db2 = SteadyDBconnect (connect , 0 , None , None , True ,
361384 'SteadyDBTestDB' , user = 'SteadyDBTestUser' )
362385 self .assertEqual (db1 .dbapi (), db2 .dbapi ())
363386 self .assertEqual (db1 .threadsafety (), db2 .threadsafety ())
@@ -520,6 +543,77 @@ def test10_SteadyDBConnectionSetSizes(self):
520543 result = cursor .fetchone ()
521544 self .assertEqual (result , ([6 , 42 , 7 ], {None : 7 , 3 : 15 , 9 : 42 }))
522545
546+ def test11_SteadyDBConnectionPing (self ):
547+ Connection .has_ping = False
548+ Connection .num_pings = 0
549+ db = SteadyDBconnect (dbapi )
550+ db .cursor ().execute ('select test' )
551+ self .assertEqual (Connection .num_pings , 0 )
552+ db .close ()
553+ db .cursor ().execute ('select test' )
554+ self .assertEqual (Connection .num_pings , 0 )
555+ db = SteadyDBconnect (dbapi , ping = 3 )
556+ db .cursor ().execute ('select test' )
557+ self .assertEqual (Connection .num_pings , 1 )
558+ db .close ()
559+ db .cursor ().execute ('select test' )
560+ self .assertEqual (Connection .num_pings , 1 )
561+ Connection .has_ping = True
562+ db = SteadyDBconnect (dbapi )
563+ db .cursor ().execute ('select test' )
564+ self .assertEqual (Connection .num_pings , 1 )
565+ db .close ()
566+ db .cursor ().execute ('select test' )
567+ self .assertEqual (Connection .num_pings , 1 )
568+ db = SteadyDBconnect (dbapi , ping = 3 )
569+ db .cursor ().execute ('select test' )
570+ self .assertEqual (Connection .num_pings , 3 )
571+ db .close ()
572+ db .cursor ().execute ('select test' )
573+ self .assertEqual (Connection .num_pings , 5 )
574+ db = SteadyDBconnect (dbapi , ping = 1 )
575+ self .assertEqual (Connection .num_pings , 5 )
576+ db .cursor ()
577+ self .assertEqual (Connection .num_pings , 6 )
578+ db .close ()
579+ cursor = db .cursor ()
580+ self .assertEqual (Connection .num_pings , 7 )
581+ cursor .execute ('select test' )
582+ self .assertEqual (Connection .num_pings , 7 )
583+ db = SteadyDBconnect (dbapi , ping = 2 )
584+ self .assertEqual (Connection .num_pings , 7 )
585+ db .cursor ()
586+ self .assertEqual (Connection .num_pings , 7 )
587+ db .close ()
588+ cursor = db .cursor ()
589+ self .assertEqual (Connection .num_pings , 7 )
590+ cursor .execute ('select test' )
591+ self .assertEqual (Connection .num_pings , 8 )
592+ db .close ()
593+ cursor = db .cursor ()
594+ self .assertEqual (Connection .num_pings , 8 )
595+ cursor .execute ('select test' )
596+ self .assertEqual (Connection .num_pings , 9 )
597+ db = SteadyDBconnect (dbapi , ping = 3 )
598+ self .assertEqual (Connection .num_pings , 9 )
599+ db .cursor ()
600+ self .assertEqual (Connection .num_pings , 10 )
601+ db .close ()
602+ cursor = db .cursor ()
603+ self .assertEqual (Connection .num_pings , 11 )
604+ cursor .execute ('select test' )
605+ self .assertEqual (Connection .num_pings , 12 )
606+ db .close ()
607+ cursor = db .cursor ()
608+ self .assertEqual (Connection .num_pings , 13 )
609+ cursor .execute ('select test' )
610+ self .assertEqual (Connection .num_pings , 14 )
611+ db .close ()
612+ cursor .execute ('select test' )
613+ self .assertEqual (Connection .num_pings , 16 )
614+ Connection .has_ping = False
615+ Connection .num_pings = 0
616+
523617
524618if __name__ == '__main__' :
525619 unittest .main ()
0 commit comments