4
4
@author: nick
5
5
'''
6
6
import unittest
7
- import PySQLPool
7
+ from PySQLPool import pool , connection
8
8
from mock import Mock
9
9
10
10
def suite ():
@@ -14,15 +14,56 @@ def suite():
14
14
15
15
return alltests
16
16
17
- class Pool :
17
+ class Pool ( unittest . TestCase ) :
18
18
def setUp (self ):
19
- pass
19
+
20
+ self .connDict = {
21
+ "host" :'localhost' ,
22
+ "user" :'unittest' ,
23
+ "passwd" :'zEzaj37u' ,
24
+ "db" :'employees' }
20
25
21
26
def testPoolBorg (self ):
22
- pass
27
+ poolObj = pool .Pool ()
28
+ poolObj2 = pool .Pool ()
29
+
30
+ self .assertTrue (poolObj .connections is poolObj2 .connections , msg = "Connections don't match" )
31
+ self .assertTrue (poolObj .lock is poolObj2 .lock , msg = "Lock dosn't match" )
32
+
23
33
24
34
def testPoolGetConnection (self ):
25
- pass
35
+ #Create a mock connection object
36
+ connObj = Mock (spec = connection .Connection )
37
+ connObj .getKey .return_value = 'test_key'
38
+
39
+ #Override the ConnectionManager for the Pool
40
+ connManager = Mock (spec = connection .ConnectionManager )
41
+ connManager .return_value = connManager
42
+ connManager .is_locked .return_value = False
43
+ pool .ConnectionManager = connManager
44
+
45
+ #Make sure we get a ConnectionManager Object back
46
+ connManObj = pool .Pool ().GetConnection (connObj )
47
+ self .assertTrue (isinstance (connManObj , connection .ConnectionManager ), msg = "Didn't get a ConnectionManager Object back" )
48
+
49
+ #Make sure our pool set persisted
50
+ self .assertTrue (pool .Pool ().connections .has_key ('test_key' ), msg = "Pool doesn't contain our pool set" )
51
+
52
+ #Make sure our pool set only contains 1 connection object
53
+ self .assertTrue (len (pool .Pool ().connections ['test_key' ]) == 1 , msg = "Pool doesn't contain only 1 connection for our pool set" )
54
+
55
+ #Re-fetch our ConnectionManager to make sure 2nd lookup work
56
+ connManObj = pool .Pool ().GetConnection (connObj )
57
+
58
+ #Make sure our pool set only contains 1 connection object even after a 2nd call
59
+ self .assertTrue (len (pool .Pool ().connections ['test_key' ]) == 1 , msg = "Pool doesn't contain only 1 connection for our pool set on 2nd call" )
60
+
61
+ #Make sure the correct methods where called as needed on the ConnectionManager
62
+ connManager .Connect .assert_called_once_with ()
63
+ connManager .lock .assert_called_once_with ()
64
+ connManager .TestConnection .assert_called_once_with ()
65
+ connManager .release .assert_called_once_with ()
66
+
26
67
27
68
def testPoolTerminate (self ):
28
69
pass
@@ -40,4 +81,7 @@ def testPoolMultiThreadGetConnection(self):
40
81
pass
41
82
42
83
def testPoolMultiThreadGetConnectionWithTransactions (self ):
43
- pass
84
+ pass
85
+
86
+ if __name__ == "__main__" :
87
+ unittest .main (defaultTest = 'suite' )
0 commit comments