From b763f531a24427f7b585768b881187d7309a4974 Mon Sep 17 00:00:00 2001 From: Karan Sivarat Date: Wed, 10 Jun 2015 11:13:11 +0700 Subject: [PATCH] add ibm_db and ibm_db_dbi to connection_manager add test Check If Exists Or Not In DB add Test Table Must Exist fixbug row_count for Table Must Exists Verify Row Count Verify Description Verify Insert/Update Data change DB Conf to general --- src/DatabaseLibrary/assertion.py | 2 + src/DatabaseLibrary/connection_manager.py | 4 + src/DatabaseLibrary/query.py | 2 +- test/DB2SQL_DB_Conf.txt | 6 ++ test/DB2SQL_DB_Tests.robot | 94 +++++++++++++++++++++++ 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 test/DB2SQL_DB_Conf.txt create mode 100644 test/DB2SQL_DB_Tests.robot diff --git a/src/DatabaseLibrary/assertion.py b/src/DatabaseLibrary/assertion.py index eeae68c..73a919d 100644 --- a/src/DatabaseLibrary/assertion.py +++ b/src/DatabaseLibrary/assertion.py @@ -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) diff --git a/src/DatabaseLibrary/connection_manager.py b/src/DatabaseLibrary/connection_manager.py index 39097d8..d6d48fb 100644 --- a/src/DatabaseLibrary/connection_manager.py +++ b/src/DatabaseLibrary/connection_manager.py @@ -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) diff --git a/src/DatabaseLibrary/query.py b/src/DatabaseLibrary/query.py index 13241f9..fbd8ddb 100644 --- a/src/DatabaseLibrary/query.py +++ b/src/DatabaseLibrary/query.py @@ -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 diff --git a/test/DB2SQL_DB_Conf.txt b/test/DB2SQL_DB_Conf.txt new file mode 100644 index 0000000..3a22bea --- /dev/null +++ b/test/DB2SQL_DB_Conf.txt @@ -0,0 +1,6 @@ +*** Variables *** +${DBName} dbname +${DBUser} user +${DBPass} password +${DBHost} host +${DBPort} port diff --git a/test/DB2SQL_DB_Tests.robot b/test/DB2SQL_DB_Tests.robot new file mode 100644 index 0000000..3aee501 --- /dev/null +++ b/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;