Skip to content

Commit

Permalink
Merge pull request #50 from bomb0069/master
Browse files Browse the repository at this point in the history
add ibm_db and ibm_db_dbi to DatabaseLibrary for support IBM DB2
  • Loading branch information
jerry57 committed Aug 19, 2015
2 parents d322b4c + b763f53 commit ce18d70
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/DatabaseLibrary/assertion.py
Expand Up @@ -172,6 +172,8 @@ def table_must_exist(self,tableName):
selectStatement = ("SELECT * FROM all_objects WHERE object_type IN ('TABLE','VIEW') AND owner = SYS_CONTEXT('USERENV', 'SESSION_USER') AND object_name = UPPER('%s')" % tableName)
elif self.db_api_module_name in ["sqlite3"]:
selectStatement = ("SELECT name FROM sqlite_master WHERE type='table' AND name='%s' COLLATE NOCASE" % tableName)
elif self.db_api_module_name in ["ibm_db", "ibm_db_dbi"]:
selectStatement = ("SELECT name FROM SYSIBM.SYSTABLES WHERE type='T' AND name=UPPER('%s')" % tableName)
else:
selectStatement = ("SELECT * FROM information_schema.tables WHERE table_name='%s'" % tableName)
num_rows = self.row_count(selectStatement)
Expand Down
4 changes: 4 additions & 0 deletions src/DatabaseLibrary/connection_manager.py
Expand Up @@ -84,6 +84,10 @@ def connect_to_database(self, dbapiModuleName=None, dbName=None, dbUsername=None
dbPort = dbPort or 1433
logger.debug ('Connecting using : %s.connect(DRIVER={SQL Server};SERVER=%s,%s;DATABASE=%s;UID=%s;PWD=%s)' % (dbapiModuleName,dbHost,dbPort,dbName,dbPort,dbUsername, dbPassword))
self._dbconnection = db_api_2.connect('DRIVER={SQL Server};SERVER=%s,%s;DATABASE=%s;UID=%s;PWD=%s'%(dbHost,dbPort,dbName,dbUsername,dbPassword))
elif dbapiModuleName in ["ibm_db", "ibm_db_dbi"]:
dbPort = dbPort or 50000
logger.debug ('Connecting using : %s.connect(DATABASE=%s;HOSTNAME=%s;PORT=%s;PROTOCOL=TCPIP;UID=%s;PWD=%s;) ' % (dbapiModuleName, dbName, dbHost, dbPort, dbUsername, dbPassword))
self._dbconnection = db_api_2.connect ('DATABASE=%s;HOSTNAME=%s;PORT=%s;PROTOCOL=TCPIP;UID=%s;PWD=%s;' % (dbName, dbHost, dbPort, dbUsername, dbPassword), '', '')
else:
logger.debug ('Connecting using : %s.connect(database=%s, user=%s, password=%s, host=%s, port=%s) ' % (dbapiModuleName, dbName, dbUsername, dbPassword, dbHost, dbPort))
self._dbconnection = db_api_2.connect (database=dbName, user=dbUsername, password=dbPassword, host=dbHost, port=dbPort)
Expand Down
2 changes: 1 addition & 1 deletion src/DatabaseLibrary/query.py
Expand Up @@ -88,7 +88,7 @@ def row_count(self, selectStatement):
cur = self._dbconnection.cursor()
self.__execute_sql(cur, selectStatement)
data = cur.fetchall()
if self.db_api_module_name in ["sqlite3"]:
if self.db_api_module_name in ["sqlite3", "ibm_db", "ibm_db_dbi"]:
rowCount = len(data)
else:
rowCount = cur.rowcount
Expand Down
6 changes: 6 additions & 0 deletions test/DB2SQL_DB_Conf.txt
@@ -0,0 +1,6 @@
*** Variables ***
${DBName} dbname
${DBUser} user
${DBPass} password
${DBHost} host
${DBPort} port
94 changes: 94 additions & 0 deletions test/DB2SQL_DB_Tests.robot
@@ -0,0 +1,94 @@
*** Settings ***
Resource DB2SQL_DB_Conf.txt
Library DatabaseLibrary

Suite Setup Connect To Database ibm_db_dbi ${DBName} ${DBUser} ${DBPass} ${DBHost} ${DBPort}
Suite Teardown Disconnect From Database

*** Test Cases ***
Create person table
${output} = Execute SQL String CREATE TABLE person (id decimal(10,0),first_name varchar(30),last_name varchar(30));
Log ${output}
Should Be Equal As Strings ${output} None

Execute SQL Script - Insert Data person table
Comment ${output} = Execute SQL Script ./my_db_test_insertData.sql
${output} = Execute SQL Script ../test/my_db_test_insertData.sql
Log ${output}
Should Be Equal As Strings ${output} None

Execute SQL String - Create Table
${output} = Execute SQL String create table foobar (id integer , firstname varchar(20) )
Log ${output}
Should Be Equal As Strings ${output} None

Check If Exists In DB - Franz Allan
Check If Exists In Database SELECT id FROM person WHERE first_name = 'Franz Allan';

Check If Not Exists In DB - Joe
Check If Not Exists In Database SELECT id FROM person WHERE first_name = 'Joe';

Table Must Exist - person
Table Must Exist person

Verify Row Count is 0
Row Count is 0 SELECT * FROM person WHERE first_name = 'NotHere';

Verify Row Count is Equal to X
Row Count is Equal to X SELECT id FROM person; 2

Verify Row Count is Less Than X
Row Count is Less Than X SELECT id FROM person; 3

Verify Row Count is Greater Than X
Row Count is Greater Than X SELECT * FROM person; 1

Retrieve Row Count
${output} = Row Count SELECT id FROM person;
Log ${output}
Should Be Equal As Strings ${output} 2

Verify person Description
[Tags] db smoke
Comment Query db for table column descriptions
@{queryResults} = Description SELECT * FROM person fetch first 1 rows only;
Log Many @{queryResults}
${output} = Set Variable ${queryResults[0]}
Should Be Equal As Strings ${output} ['ID', DBAPITypeObject(['NUM', 'DECIMAL', 'DEC', 'NUMERIC']), 12, 12, 10, 0, True]
${output} = Set Variable ${queryResults[1]}
Should Be Equal As Strings ${output} ['FIRST_NAME', DBAPITypeObject(['CHARACTER VARYING', 'CHAR VARYING', 'VARCHAR', 'STRING', 'CHARACTER', 'CHAR']), 30, 30, 30, 0, True]
${output} = Set Variable ${queryResults[2]}
Should Be Equal As Strings ${output} ['LAST_NAME', DBAPITypeObject(['CHARACTER VARYING', 'CHAR VARYING', 'VARCHAR', 'STRING', 'CHARACTER', 'CHAR']), 30, 30, 30, 0, True]
${NumColumns} = Get Length ${queryResults}
Should Be Equal As Integers ${NumColumns} 3

Verify Query - Row Count person table
${output} = Query SELECT COUNT(*) FROM person;
Log ${output}
Should Be Equal As Strings ${output} [(2,)]

Verify Query - Row Count foobar table
${output} = Query SELECT COUNT(*) FROM foobar;
Log ${output}
Should Be Equal As Strings ${output} [(0,)]

Insert Data Into Table foobar
${output} = Execute SQL String INSERT INTO foobar VALUES(1,'Jerry');
Log ${output}
Should Be Equal As Strings ${output} None

Verify Query - Row Count foobar table 1 row
${output} = Query SELECT COUNT(*) FROM foobar;
Log ${output}
Should Be Equal As Strings ${output} [(1,)]


Verify Delete All Rows From Table - foobar
Delete All Rows From Table foobar

Verify Query - Row Count foobar table 0 row
Row Count Is 0 SELECT * FROM foobar;

Drop person and foobar table
Execute SQL String DROP TABLE person;
Execute SQL String DROP TABLE foobar;

0 comments on commit ce18d70

Please sign in to comment.