Skip to content

Commit ea5f795

Browse files
authored
Merge pull request MarketSquare#103 from denghj/master
Added Connect to Excel feature to the connection manager
2 parents 0a5ab93 + aa7bd12 commit ea5f795

File tree

5 files changed

+320
-8
lines changed

5 files changed

+320
-8
lines changed
162 Bytes
Binary file not shown.

src/DatabaseLibrary/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.1
1+
1.1.1

src/DatabaseLibrary/connection_manager.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,12 @@ def connect_to_database(self, dbapiModuleName=None, dbName=None, dbUsername=None
8585
dbHost = dbHost or config.get('default', 'dbHost') or 'localhost'
8686
dbPort = int(dbPort or config.get('default', 'dbPort'))
8787

88-
self.db_api_module_name = dbapiModuleName
89-
90-
db_api_2 = importlib.import_module(dbapiModuleName)
88+
if dbapiModuleName == "excel" or dbapiModuleName == "excelrw":
89+
self.db_api_module_name = "pyodbc"
90+
db_api_2 = importlib.import_module("pyodbc")
91+
else:
92+
self.db_api_module_name = dbapiModuleName
93+
db_api_2 = importlib.import_module(dbapiModuleName)
9194
if dbapiModuleName in ["MySQLdb", "pymysql"]:
9295
dbPort = dbPort or 3306
9396
logger.info('Connecting using : %s.connect(db=%s, user=%s, passwd=%s, host=%s, port=%s, charset=%s) ' % (dbapiModuleName, dbName, dbUsername, dbPassword, dbHost, dbPort, dbCharset))
@@ -100,6 +103,20 @@ def connect_to_database(self, dbapiModuleName=None, dbName=None, dbUsername=None
100103
dbPort = dbPort or 1433
101104
logger.info('Connecting using : %s.connect(DRIVER={SQL Server};SERVER=%s,%s;DATABASE=%s;UID=%s;PWD=%s)' % (dbapiModuleName, dbHost, dbPort, dbName, dbUsername, dbPassword))
102105
self._dbconnection = db_api_2.connect('DRIVER={SQL Server};SERVER=%s,%s;DATABASE=%s;UID=%s;PWD=%s' % (dbHost, dbPort, dbName, dbUsername, dbPassword))
106+
elif dbapiModuleName in ["excel"]:
107+
logger.info(
108+
'Connecting using : %s.connect(DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=%s;ReadOnly=1;Extended Properties="Excel 8.0;HDR=YES";)' % (
109+
dbapiModuleName, dbName))
110+
self._dbconnection = db_api_2.connect(
111+
'DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=%s;ReadOnly=1;Extended Properties="Excel 8.0;HDR=YES";)' % (
112+
dbName), autocommit=True)
113+
elif dbapiModuleName in ["excelrw"]:
114+
logger.info(
115+
'Connecting using : %s.connect(DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=%s;ReadOnly=0;Extended Properties="Excel 8.0;HDR=YES";)' % (
116+
dbapiModuleName, dbName))
117+
self._dbconnection = db_api_2.connect(
118+
'DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=%s;ReadOnly=0;Extended Properties="Excel 8.0;HDR=YES";)' % (
119+
dbName), autocommit=True)
103120
elif dbapiModuleName in ["ibm_db", "ibm_db_dbi"]:
104121
dbPort = dbPort or 50000
105122
logger.info('Connecting using : %s.connect(DATABASE=%s;HOSTNAME=%s;PORT=%s;PROTOCOL=TCPIP;UID=%s;PWD=%s;) ' % (dbapiModuleName, dbName, dbHost, dbPort, dbUsername, dbPassword))
@@ -146,19 +163,18 @@ def disconnect_from_database(self):
146163
def set_auto_commit(self, autoCommit=True):
147164
"""
148165
Turn the autocommit on the database connection ON or OFF.
149-
166+
150167
The default behaviour on a newly created database connection is to automatically start a
151168
transaction, which means that database actions that won't work if there is an active
152169
transaction will fail. Common examples of these actions are creating or deleting a database
153170
or database snapshot. By turning on auto commit on the database connection these actions
154171
can be performed.
155-
172+
156173
Example:
157174
| # Default behaviour, sets auto commit to true
158175
| Set Auto Commit
159-
160176
| # Explicitly set the desired state
161177
| Set Auto Commit | False
162178
"""
163179
logger.info('Executing : Set Auto Commit')
164-
self._dbconnection.autocommit = autoCommit
180+
self._dbconnection.autocommit = autoCommit

test/Excel_DB_Test.robot

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
*** Settings ***
2+
Suite Setup Setup testing excel
3+
Suite Teardown Cleanup testing excel
4+
Library DatabaseLibrary
5+
Library OperatingSystem
6+
Library ExcelLibrary
7+
8+
*** Variables ***
9+
${DBHost} dummy
10+
${DBName} ${EXECDIR}/test/Test_Excel.xls
11+
${DBPass} dummy
12+
${DBPort} 80
13+
${DBUser} dummy
14+
15+
*** Test Cases ***
16+
Create person table
17+
[Tags] db smoke
18+
${output} = Execute SQL String CREATE TABLE [person] (id integer,first_name varchar(20),last_name varchar(20));
19+
Log ${output}
20+
Should Be Equal As Strings ${output} None
21+
22+
Execute SQL Script - Insert Data person table
23+
[Tags] db smoke
24+
log to console ${DBName}
25+
Comment ${output} = Execute SQL Script ${EXECDIR}/test/excel_db_test_insertData.sql
26+
${output} = Execute SQL Script ${EXECDIR}/test/excel_db_test_insertData.sql
27+
Log ${output}
28+
Should Be Equal As Strings ${output} None
29+
30+
Execute SQL String - Create Table
31+
[Tags] db smoke
32+
${output} = Execute SQL String create table [foobar] ([id] integer, [firstname] varchar(20))
33+
Log ${output}
34+
Should Be Equal As Strings ${output} None
35+
36+
Check If Exists In DB - Franz Allan
37+
[Tags] db smoke
38+
Check If Exists In Database SELECT id FROM [person$] WHERE first_name = 'Franz Allan';
39+
40+
Check If Not Exists In DB - Joe
41+
[Tags] db smoke
42+
Check If Not Exists In Database SELECT id FROM [person$] WHERE first_name = 'Joe';
43+
44+
45+
Verify Row Count is 0
46+
[Tags] db smoke
47+
Row Count is 0 SELECT * FROM [person$] WHERE first_name = 'NotHere';
48+
49+
Verify Row Count is Equal to X
50+
[Tags] db smoke
51+
Row Count is Equal to X SELECT id FROM [person$]; 2
52+
53+
Verify Row Count is Less Than X
54+
[Tags] db smoke
55+
Row Count is Less Than X SELECT id FROM [person$]; 3
56+
57+
Verify Row Count is Greater Than X
58+
[Tags] db smoke
59+
Row Count is Greater Than X SELECT * FROM [person$]; 1
60+
61+
Retrieve Row Count
62+
[Tags] db smoke
63+
${output} = Row Count SELECT id FROM [person$];
64+
Log ${output}
65+
Should Be Equal As Strings ${output} 2
66+
67+
Retrieve records from person table
68+
[Tags] db smoke
69+
${output} = Execute SQL String SELECT * FROM [person$];
70+
Log ${output}
71+
Should Be Equal As Strings ${output} None
72+
73+
Verify person Description
74+
[Tags] db smoke
75+
Comment Query db for table column descriptions
76+
@{queryResults} = Description select TOP 1 * FROM [person$];
77+
Log Many @{queryResults}
78+
${output} = Set Variable ${queryResults[0]}
79+
Should Be Equal As Strings ${output} ('id', <type 'str'>, None, 255, 255, 0, True)
80+
${output} = Set Variable ${queryResults[1]}
81+
Should Be Equal As Strings ${output} ('first_name', <type 'str'>, None, 255, 255, 0, True)
82+
${output} = Set Variable ${queryResults[2]}
83+
Should Be Equal As Strings ${output} ('last_name', <type 'str'>, None, 255, 255, 0, True)
84+
${NumColumns} = Get Length ${queryResults}
85+
Should Be Equal As Integers ${NumColumns} 3
86+
87+
Verify foobar Description
88+
[Tags] db smoke
89+
Comment Query db for table column descriptions
90+
@{queryResults} = Description SELECT TOP 1 * FROM [foobar$];
91+
Log Many @{queryResults}
92+
${output} = Set Variable ${queryResults[0]}
93+
Should Be Equal As Strings ${output} ('id', <type 'str'>, None, 255, 255, 0, True)
94+
${output} = Set Variable ${queryResults[1]}
95+
Should Be Equal As Strings ${output} ('firstname', <type 'str'>, None, 255, 255, 0, True)
96+
${NumColumns} = Get Length ${queryResults}
97+
Should Be Equal As Integers ${NumColumns} 2
98+
99+
Verify Query - Row Count person table
100+
[Tags] db smoke
101+
${output} = Query SELECT COUNT(*) FROM [person$];
102+
Log ${output}
103+
Should Be Equal As Strings ${output} [(2, )]
104+
105+
Verify Query - Row Count foobar table
106+
[Tags] db smoke
107+
${output} = Query SELECT COUNT(*) FROM foobar;
108+
Log ${output}
109+
Should Be Equal As Strings ${output} [(0, )]
110+
111+
Verify Query - Get results as a list of dictionaries
112+
[Tags] db smoke
113+
${output} = Query SELECT * FROM [person$]; \ True
114+
Log ${output}
115+
Should Be Equal As Strings &{output[0]}[first_name] Franz Allan
116+
Should Be Equal As Strings &{output[1]}[first_name] Jerry
117+
118+
Verify Execute SQL String - Row Count person table
119+
[Tags] db smoke
120+
${output} = Execute SQL String SELECT COUNT(*) FROM [person$];
121+
Log ${output}
122+
Should Be Equal As Strings ${output} None
123+
124+
Verify Execute SQL String - Row Count foobar table
125+
[Tags] db smoke
126+
${output} = Execute SQL String SELECT COUNT(*) FROM [foobar$];
127+
Log ${output}
128+
Should Be Equal As Strings ${output} None
129+
130+
Insert Data Into Table foobar
131+
[Tags] db smoke
132+
${output} = Execute SQL String INSERT INTO [foobar$] VALUES(1,'Jerry');
133+
Log ${output}
134+
Should Be Equal As Strings ${output} None
135+
136+
Verify Query - Row Count foobar table 1 row
137+
[Tags] db smoke
138+
${output} = Query SELECT COUNT(*) FROM [foobar$];
139+
Log ${output}
140+
Should Be Equal As Strings ${output} [(1, )]
141+
142+
143+
Add person in first transaction
144+
[Tags] db smoke
145+
${output} = Execute SQL String INSERT INTO [person$] VALUES(101,'Bilbo','Baggins'); True
146+
Log ${output}
147+
Should Be Equal As Strings ${output} None
148+
149+
Verify person in first transaction
150+
[Tags] db smoke
151+
Row Count is Equal to X SELECT * FROM [person$] WHERE last_name = 'Baggins'; 1 True
152+
153+
#Begin second transaction
154+
# [Tags] db smoke
155+
# ${output} = Execute SQL String SAVEPOINT second True
156+
# Log ${output}
157+
# Should Be Equal As Strings ${output} None
158+
159+
Add person in second transaction
160+
[Tags] db smoke
161+
${output} = Execute SQL String INSERT INTO [person$] VALUES(102,'Frodo','Baggins'); True
162+
Log ${output}
163+
Should Be Equal As Strings ${output} None
164+
165+
Verify persons in first and second transactions
166+
[Tags] db smoke
167+
Row Count is Equal to X SELECT * FROM [person$] WHERE last_name = 'Baggins'; 2 True
168+
169+
Setup RO access to excel
170+
Disconnect From Database
171+
Connect To Database excel ${DBName} ${DBUser} ${DBPass} ${DBHost} ${DBPort}
172+
173+
174+
Check If Exists In RODB - Franz Allan
175+
[Tags] db smoke
176+
Check If Exists In Database SELECT id FROM [person$] WHERE first_name = 'Franz Allan';
177+
178+
Check If Not Exists In RODB - Joe
179+
[Tags] db smoke
180+
Check If Not Exists In Database SELECT id FROM [person$] WHERE first_name = 'Joe';
181+
182+
183+
Verify Row Count is 0 RODB
184+
[Tags] db smoke
185+
Row Count is 0 SELECT * FROM [person$] WHERE first_name = 'NotHere';
186+
187+
Verify Row Count is Equal to X RODB
188+
[Tags] db smoke
189+
Row Count is Equal to X SELECT id FROM [person$]; 4
190+
191+
Verify Row Count is Less Than X RODB
192+
[Tags] db smoke
193+
Row Count is Less Than X SELECT id FROM [person$]; 5
194+
195+
Verify Row Count is Greater Than X RODB
196+
[Tags] db smoke
197+
Row Count is Greater Than X SELECT * FROM [person$]; 1
198+
199+
Retrieve Row Count RODB
200+
[Tags] db smoke
201+
${output} = Row Count SELECT id FROM [person$];
202+
Log ${output}
203+
Should Be Equal As Strings ${output} 4
204+
205+
Retrieve records from person table RODB
206+
[Tags] db smoke
207+
${output} = Execute SQL String SELECT * FROM [person$];
208+
Log ${output}
209+
Should Be Equal As Strings ${output} None
210+
211+
Verify person Description RODB
212+
[Tags] db smoke
213+
Comment Query db for table column descriptions
214+
@{queryResults} = Description select TOP 1 * FROM [person$];
215+
Log Many @{queryResults}
216+
${output} = Set Variable ${queryResults[0]}
217+
Should Be Equal As Strings ${output} ('id', <type 'str'>, None, 255, 255, 0, True)
218+
${output} = Set Variable ${queryResults[1]}
219+
Should Be Equal As Strings ${output} ('first_name', <type 'str'>, None, 255, 255, 0, True)
220+
${output} = Set Variable ${queryResults[2]}
221+
Should Be Equal As Strings ${output} ('last_name', <type 'str'>, None, 255, 255, 0, True)
222+
${NumColumns} = Get Length ${queryResults}
223+
Should Be Equal As Integers ${NumColumns} 3
224+
225+
Verify foobar Description RODB
226+
[Tags] db smoke
227+
Comment Query db for table column descriptions
228+
@{queryResults} = Description SELECT TOP 1 * FROM [foobar$];
229+
Log Many @{queryResults}
230+
${output} = Set Variable ${queryResults[0]}
231+
Should Be Equal As Strings ${output} ('id', <type 'str'>, None, 255, 255, 0, True)
232+
${output} = Set Variable ${queryResults[1]}
233+
Should Be Equal As Strings ${output} ('firstname', <type 'str'>, None, 255, 255, 0, True)
234+
${NumColumns} = Get Length ${queryResults}
235+
Should Be Equal As Integers ${NumColumns} 2
236+
237+
Verify Query - Row Count person table RODB
238+
[Tags] db smoke
239+
${output} = Query SELECT COUNT(*) FROM [person$];
240+
Log ${output}
241+
Should Be Equal As Strings ${output} [(4, )]
242+
243+
Verify Query - Row Count foobar table RODB
244+
[Tags] db smoke
245+
${output} = Query SELECT COUNT(*) FROM [foobar$];
246+
Log ${output}
247+
Should Be Equal As Strings ${output} [(1, )]
248+
249+
Verify Query - Get results as a list of dictionaries RODB
250+
[Tags] db smoke
251+
${output} = Query SELECT * FROM [person$]; \ True
252+
Log ${output}
253+
Should Be Equal As Strings &{output[0]}[first_name] Franz Allan
254+
Should Be Equal As Strings &{output[1]}[first_name] Jerry
255+
256+
Verify Execute SQL String - Row Count person table RODB
257+
[Tags] db smoke
258+
${output} = Execute SQL String SELECT COUNT(*) FROM [person$];
259+
Log ${output}
260+
Should Be Equal As Strings ${output} None
261+
262+
Verify Execute SQL String - Row Count foobar table RODB
263+
[Tags] db smoke
264+
${output} = Execute SQL String SELECT COUNT(*) FROM [foobar$];
265+
Log ${output}
266+
Should Be Equal As Strings ${output} None
267+
268+
269+
Verify Query - Row Count foobar table 1 row RODB
270+
[Tags] db smoke
271+
${output} = Query SELECT COUNT(*) FROM [foobar$];
272+
Log ${output}
273+
Should Be Equal As Strings ${output} [(1, )]
274+
275+
Setup RW access to excel
276+
Disconnect From Database
277+
Connect To Database excelrw ${DBName} ${DBUser} ${DBPass} ${DBHost} ${DBPort}
278+
279+
Drop person and foobar tables
280+
[Tags] db smoke
281+
${output} = Execute SQL String DROP TABLE [person$],[foobar$]
282+
Log ${output}
283+
Should Be Equal As Strings ${output} None
284+
285+
286+
*** Keywords ***
287+
288+
Setup testing excel
289+
Create Excel Workbook Test_Excel
290+
Connect To Database excelrw ${DBName} ${DBUser} ${DBPass} ${DBHost} ${DBPort}
291+
292+
Cleanup testing excel
293+
Disconnect From Database
294+
Remove File ${DBName}

test/excel_db_test_insertData.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
INSERT INTO [person$] VALUES(1,'Franz Allan','See');
2+
INSERT INTO [person$] VALUES(2,'Jerry','Schneider');

0 commit comments

Comments
 (0)