Skip to content
This repository was archived by the owner on Nov 7, 2020. It is now read-only.

Commit 85a580e

Browse files
committed
Fixes to Pool and Unittests for Pool
1 parent d88f27c commit 85a580e

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

src/PySQLPool/connection.py

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ def __getattr__(self, name):
8181

8282
def getKey(self):
8383
return self.key
84-
8584

8685
connection_timeout = datetime.timedelta(seconds=20)
8786

src/PySQLPool/pool.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def GetConnection(self, ConnectionObj):
176176
def _getConnectionFromPoolSet(self, key):
177177
connection = None
178178

179-
for conn in self.connections[key].values():
179+
for conn in self.connections[key]:
180180
#Grab an active connection if maxActivePerConnection is not meet
181181
#TODO: Implement a max usage per connection object
182182
if not conn.is_locked():

test/testPool.py

+50-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@author: nick
55
'''
66
import unittest
7-
import PySQLPool
7+
from PySQLPool import pool, connection
88
from mock import Mock
99

1010
def suite():
@@ -14,15 +14,56 @@ def suite():
1414

1515
return alltests
1616

17-
class Pool:
17+
class Pool(unittest.TestCase):
1818
def setUp(self):
19-
pass
19+
20+
self.connDict = {
21+
"host":'localhost',
22+
"user":'unittest',
23+
"passwd":'zEzaj37u',
24+
"db":'employees'}
2025

2126
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+
2333

2434
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+
2667

2768
def testPoolTerminate(self):
2869
pass
@@ -40,4 +81,7 @@ def testPoolMultiThreadGetConnection(self):
4081
pass
4182

4283
def testPoolMultiThreadGetConnectionWithTransactions(self):
43-
pass
84+
pass
85+
86+
if __name__ == "__main__":
87+
unittest.main(defaultTest='suite')

0 commit comments

Comments
 (0)