Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Make Execute SQL keywords return number of rows affected
  • Loading branch information
kivipe committed Dec 5, 2018
commit 0988750502c08d7825b87c2daa0176422f56bd6c
24 changes: 16 additions & 8 deletions src/DatabaseLibrary/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,11 @@ def delete_all_rows_from_table(self, tableName, sansTran=False):

def execute_sql_script(self, sqlScriptFileName, sansTran=False):
"""
Executes the content of the `sqlScriptFileName` as SQL commands. Useful for setting the database to a known
state before running your tests, or clearing out your test data after running each a test. Set optional input
`sansTran` to True to run command without an explicit transaction commit or rollback.
Executes the content of the `sqlScriptFileName` as SQL commands and
returns number of rows affected. Useful for setting the database to
a known state before running your tests, or clearing out your test
data after running each a test. Set optional input `sansTran` to
True to run command without an explicit transaction commit or rollback.

Sample usage :
| Execute Sql Script | ${EXECDIR}${/}resources${/}DDL-setup.sql |
Expand Down Expand Up @@ -251,6 +253,7 @@ def execute_sql_script(self, sqlScriptFileName, sansTran=False):
sqlScriptFile = open(sqlScriptFileName)

cur = None
result = 0
try:
cur = self._dbconnection.cursor()
logger.info('Executing : Execute SQL Script | %s ' % sqlScriptFileName)
Expand All @@ -276,24 +279,27 @@ def execute_sql_script(self, sqlScriptFileName, sansTran=False):

sqlStatement += sqlFragment + ' '

self.__execute_sql(cur, sqlStatement)
result = result + self.__execute_sql(cur, sqlStatement)
sqlStatement = ''

sqlStatement = sqlStatement.strip()
if len(sqlStatement) != 0:
self.__execute_sql(cur, sqlStatement)
result = self.__execute_sql(cur, sqlStatement)

if not sansTran:
self._dbconnection.commit()
finally:
if cur:
if not sansTran:
self._dbconnection.rollback()
return result

def execute_sql_string(self, sqlString, sansTran=False):
"""
Executes the sqlString as SQL commands. Useful to pass arguments to your sql. Set optional input `sansTran` to
True to run command without an explicit transaction commit or rollback.
Executes the sqlString as SQL commands and returns number of rows
affected. Useful to pass arguments to your sql. Set optional input
`sansTran` to True to run command without an explicit transaction
commit or rollback.

SQL commands are expected to be delimited by a semi-colon (';').

Expand All @@ -307,16 +313,18 @@ def execute_sql_string(self, sqlString, sansTran=False):
| Execute Sql String | DELETE FROM person_employee_table; DELETE FROM person_table | True |
"""
cur = None
result = 0
try:
cur = self._dbconnection.cursor()
logger.info('Executing : Execute SQL String | %s ' % sqlString)
self.__execute_sql(cur, sqlString)
result = self.__execute_sql(cur, sqlString)
if not sansTran:
self._dbconnection.commit()
finally:
if cur:
if not sansTran:
self._dbconnection.rollback()
return result

def call_stored_procedure(self, spName, spParams=None, sansTran=False):
"""
Expand Down
8 changes: 4 additions & 4 deletions test/DB2SQL_DB_Tests.robot
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ Library DatabaseLibrary
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
Should Be Equal As Strings ${output} 0

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
Should Be Equal As Strings ${output} 2

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
Should Be Equal As Strings ${output} 0

Check If Exists In DB - Franz Allan
Check If Exists In Database SELECT id FROM person WHERE first_name = 'Franz Allan';
Expand Down Expand Up @@ -81,7 +81,7 @@ Verify Query - Get results as a list of dictionaries
Insert Data Into Table foobar
${output} = Execute SQL String INSERT INTO foobar VALUES(1,'Jerry');
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 1

Verify Query - Row Count foobar table 1 row
${output} = Query SELECT COUNT(*) FROM foobar;
Expand Down
18 changes: 9 additions & 9 deletions test/MSSQL_DB_Tests.robot
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ Create person table
[Tags] db smoke
${output} = Execute SQL String CREATE TABLE person (id integer unique, first_name varchar(20), last_name varchar(20));
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0

Execute SQL Script - Insert Data person table
[Tags] db smoke
${output} = Execute SQL Script ./my_db_test_insertData.sql
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 2

Execute SQL String - Create Table
[Tags] db smoke
${output} = Execute SQL String create table foobar (id integer primary key, firstname varchar(20) unique)
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0

Check If Exists In DB - Franz Allan
[Tags] db smoke
Expand Down Expand Up @@ -125,13 +125,13 @@ Verify Execute SQL String - Row Count foobar table
[Tags] db smoke
${output} = Execute SQL String SELECT COUNT(*) FROM foobar;
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 1

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

Verify Query - Row Count foobar table 1 row
[Tags] db smoke
Expand All @@ -152,13 +152,13 @@ Begin first transaction
[Tags] db smoke
${output} = Execute SQL String SAVE TRANSACTION first True
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0

Add person in first transaction
[Tags] db smoke
${output} = Execute SQL String INSERT INTO person VALUES(101,'Bilbo','Baggins'); True
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 1

Verify person in first transaction
[Tags] db smoke
Expand Down Expand Up @@ -204,7 +204,7 @@ Drop person and foobar tables
[Tags] db smoke
${output} = Execute SQL String DROP TABLE IF EXISTS person;
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0
${output} = Execute SQL String DROP TABLE IF EXISTS foobar;
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0
28 changes: 14 additions & 14 deletions test/MySQL_DB_Tests.robot
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ Create person table
[Tags] db smoke
${output} = Execute SQL String CREATE TABLE person (id integer unique,first_name varchar(20),last_name varchar(20));
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0

Execute SQL Script - Insert Data person table
[Tags] db smoke
Comment ${output} = Execute SQL Script ./${DBName}_insertData.sql
${output} = Execute SQL Script ./my_db_test_insertData.sql
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 2

Execute SQL String - Create Table
[Tags] db smoke
${output} = Execute SQL String create table foobar (id integer primary key, firstname varchar(20) unique)
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0

Check If Exists In DB - Franz Allan
[Tags] db smoke
Expand Down Expand Up @@ -69,7 +69,7 @@ Retrieve records from person table
[Tags] db smoke
${output} = Execute SQL String SELECT * FROM person;
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 2

Verify person Description
[Tags] db smoke
Expand Down Expand Up @@ -120,19 +120,19 @@ Verify Execute SQL String - Row Count person table
[Tags] db smoke
${output} = Execute SQL String SELECT COUNT(*) FROM person;
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 1

Verify Execute SQL String - Row Count foobar table
[Tags] db smoke
${output} = Execute SQL String SELECT COUNT(*) FROM foobar;
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 1

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

Verify Query - Row Count foobar table 1 row
[Tags] db smoke
Expand All @@ -156,13 +156,13 @@ Begin first transaction
[Tags] db smoke
${output} = Execute SQL String SAVEPOINT first True
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0

Add person in first transaction
[Tags] db smoke
${output} = Execute SQL String INSERT INTO person VALUES(101,'Bilbo','Baggins'); True
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 1

Verify person in first transaction
[Tags] db smoke
Expand All @@ -172,13 +172,13 @@ Begin second transaction
[Tags] db smoke
${output} = Execute SQL String SAVEPOINT second True
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0

Add person in second transaction
[Tags] db smoke
${output} = Execute SQL String INSERT INTO person VALUES(102,'Frodo','Baggins'); True
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 1

Verify persons in first and second transactions
[Tags] db smoke
Expand All @@ -188,7 +188,7 @@ Rollback second transaction
[Tags] db smoke
${output} = Execute SQL String ROLLBACK TO SAVEPOINT second True
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0

Verify second transaction rollback
[Tags] db smoke
Expand All @@ -198,7 +198,7 @@ Rollback first transaction
[Tags] db smoke
${output} = Execute SQL String ROLLBACK TO SAVEPOINT first True
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0

Verify first transaction rollback
[Tags] db smoke
Expand All @@ -208,4 +208,4 @@ Drop person and foobar tables
[Tags] db smoke
${output} = Execute SQL String DROP TABLE IF EXISTS person,foobar;
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0
12 changes: 6 additions & 6 deletions test/PostgreSQL_DB_Tests.robot
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ ${DBUser} postgres
Create person table
${output} = Execute SQL String CREATE TABLE person (id integer unique,first_name varchar,last_name varchar);
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0

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

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

Check If Exists In DB - Franz Allan
Check If Exists In Database SELECT id FROM person WHERE first_name = 'Franz Allan';
Expand Down Expand Up @@ -117,12 +117,12 @@ Verify Execute SQL String - Row Count person table
Verify Execute SQL String - Row Count foobar table
${output} = Execute SQL String SELECT COUNT(*) FROM foobar;
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 1

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

Verify Query - Row Count foobar table 1 row
${output} = Query SELECT COUNT(*) FROM foobar;
Expand All @@ -145,4 +145,4 @@ Verify Query - Row Count foobar table 0 row
Drop person and foobar tables
${output} = Execute SQL String DROP TABLE IF EXISTS person,foobar;
Log ${output}
Should Be Equal As Strings ${output} None
Should Be Equal As Strings ${output} 0
Loading