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
Revert "Make Execute SQL keywords return number of rows affected"
  • Loading branch information
jerry57 authored Jan 30, 2019
commit 870987c4e268688af40cd0801c610478f1b8eb4b
24 changes: 8 additions & 16 deletions src/DatabaseLibrary/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,9 @@ 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 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.
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.

Sample usage :
| Execute Sql Script | ${EXECDIR}${/}resources${/}DDL-setup.sql |
Expand Down Expand Up @@ -253,7 +251,6 @@ 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 @@ -279,27 +276,24 @@ def execute_sql_script(self, sqlScriptFileName, sansTran=False):

sqlStatement += sqlFragment + ' '

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

sqlStatement = sqlStatement.strip()
if len(sqlStatement) != 0:
result = self.__execute_sql(cur, sqlStatement)
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 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.
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.

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

Expand All @@ -313,18 +307,16 @@ 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)
result = self.__execute_sql(cur, sqlString)
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} 0
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} 2
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} 0
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';
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} 1
Should Be Equal As Strings ${output} None

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

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

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

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

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

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

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

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} 0
Should Be Equal As Strings ${output} None
${output} = Execute SQL String DROP TABLE IF EXISTS foobar;
Log ${output}
Should Be Equal As Strings ${output} 0
Should Be Equal As Strings ${output} None
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} 0
Should Be Equal As Strings ${output} None

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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