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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ test/log.html
test/my_db_test.db
test/output.xml
test/report.html
.vscode/settings.json
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this should be in .gitignore

"python.linting.pylintEnabled": true,
"python.pythonPath": "/usr/bin/python3"
}
1 change: 1 addition & 0 deletions src/DatabaseLibrary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
__version_file_path__ = os.path.join(os.path.dirname(__file__), 'VERSION')
__version__ = open(__version_file_path__, 'r').read().strip()


class DatabaseLibrary(ConnectionManager, Query, Assertion):
"""
Database Library contains utilities meant for Robot Framework's usage.
Expand Down
78 changes: 49 additions & 29 deletions src/DatabaseLibrary/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Assertion(object):
Assertion handles all the assertions of Database Library.
"""

def check_if_exists_in_database(self, selectStatement, sansTran=False):
def check_if_exists_in_database(self, selectStatement, sansTran=False, alias=None):
"""
Check if any row would be returned by given the input `selectStatement`. If there are no results, then this will

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd better have to document this change also because this is a breaking change that introduces a new mandatory field.

throw an AssertionError. Set optional input `sansTran` to True to run command without an explicit transaction
Expand All @@ -41,12 +41,13 @@ def check_if_exists_in_database(self, selectStatement, sansTran=False):
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
| Check If Exists In Database | SELECT id FROM person WHERE first_name = 'John' | True |
"""
logger.info ('Executing : Check If Exists In Database | %s ' % selectStatement)
if not self.query(selectStatement, sansTran):
logger.info('Executing : Check If Exists In Database | %s | %s ' % (selectStatement,alias))

if not self.query(selectStatement=selectStatement, sansTran=sansTran, alias=alias):
raise AssertionError("Expected to have have at least one row from '%s' "
"but got 0 rows." % selectStatement)

def check_if_not_exists_in_database(self, selectStatement, sansTran=False):
def check_if_not_exists_in_database(self, selectStatement, sansTran=False, alias=None):
"""
This is the negation of `check_if_exists_in_database`.

Expand All @@ -69,13 +70,15 @@ def check_if_not_exists_in_database(self, selectStatement, sansTran=False):
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
| Check If Not Exists In Database | SELECT id FROM person WHERE first_name = 'John' | True |
"""
logger.info('Executing : Check If Not Exists In Database | %s ' % selectStatement)
queryResults = self.query(selectStatement, sansTran)
logger.info(
'Executing : Check If Not Exists In Database | %s | %s ' % (selectStatement,alias))
queryResults = self.query(
selectStatement=selectStatement, sansTran=sansTran, alias=alias)
if queryResults:
raise AssertionError("Expected to have have no rows from '%s' "
"but got some rows : %s." % (selectStatement, queryResults))

def row_count_is_0(self, selectStatement, sansTran=False):
def row_count_is_0(self, selectStatement, sansTran=False, alias=None):
"""
Check if any rows are returned from the submitted `selectStatement`. If there are, then this will throw an
AssertionError. Set optional input `sansTran` to True to run command without an explicit transaction commit or
Expand All @@ -96,13 +99,15 @@ def row_count_is_0(self, selectStatement, sansTran=False):
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
| Row Count is 0 | SELECT id FROM person WHERE first_name = 'John' | True |
"""
logger.info('Executing : Row Count Is 0 | %s ' % selectStatement)
num_rows = self.row_count(selectStatement, sansTran)
logger.info('Executing : Row Count Is 0 | %s | %s ' % (selectStatement,alias))
logger.info(
'Connection: Row Count Is 0 | %s' % alias)
num_rows = self.row_count(selectStatement, sansTran, alias)
if num_rows > 0:
raise AssertionError("Expected zero rows to be returned from '%s' "
"but got rows back. Number of rows returned was %s" % (selectStatement, num_rows))

def row_count_is_equal_to_x(self, selectStatement, numRows, sansTran=False):
def row_count_is_equal_to_x(self, selectStatement, numRows, sansTran=False, alias=None):
"""
Check if the number of rows returned from `selectStatement` is equal to the value submitted. If not, then this
will throw an AssertionError. Set optional input `sansTran` to True to run command without an explicit
Expand All @@ -124,13 +129,15 @@ def row_count_is_equal_to_x(self, selectStatement, numRows, sansTran=False):
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
| Row Count Is Equal To X | SELECT id FROM person WHERE first_name = 'John' | 0 | True |
"""
logger.info('Executing : Row Count Is Equal To X | %s | %s ' % (selectStatement, numRows))
num_rows = self.row_count(selectStatement, sansTran)
logger.info('Executing : Row Count Is Equal To X | %s | %s | %s ' %
(selectStatement, numRows,alias))

num_rows = self.row_count(selectStatement, sansTran, alias)
if num_rows != int(numRows.encode('ascii')):
raise AssertionError("Expected same number of rows to be returned from '%s' "
"than the returned rows of %s" % (selectStatement, num_rows))

def row_count_is_greater_than_x(self, selectStatement, numRows, sansTran=False):
def row_count_is_greater_than_x(self, selectStatement, numRows, sansTran=False, alias=None):
"""
Check if the number of rows returned from `selectStatement` is greater than the value submitted. If not, then
this will throw an AssertionError. Set optional input `sansTran` to True to run command without an explicit
Expand All @@ -152,13 +159,15 @@ def row_count_is_greater_than_x(self, selectStatement, numRows, sansTran=False):
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
| Row Count Is Greater Than X | SELECT id FROM person | 1 | True |
"""
logger.info('Executing : Row Count Is Greater Than X | %s | %s ' % (selectStatement, numRows))
num_rows = self.row_count(selectStatement, sansTran)
logger.info('Executing : Row Count Is Greater Than X | %s | %s | %s ' % (
selectStatement, numRows,alias))

num_rows = self.row_count(selectStatement, sansTran, alias)
if num_rows <= int(numRows.encode('ascii')):
raise AssertionError("Expected more rows to be returned from '%s' "
"than the returned rows of %s" % (selectStatement, num_rows))

def row_count_is_less_than_x(self, selectStatement, numRows, sansTran=False):
def row_count_is_less_than_x(self, selectStatement, numRows, sansTran=False, alias=None):
"""
Check if the number of rows returned from `selectStatement` is less than the value submitted. If not, then this
will throw an AssertionError. Set optional input `sansTran` to True to run command without an explicit
Expand All @@ -180,13 +189,15 @@ def row_count_is_less_than_x(self, selectStatement, numRows, sansTran=False):
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
| Row Count Is Less Than X | SELECT id FROM person | 3 | True |
"""
logger.info('Executing : Row Count Is Less Than X | %s | %s ' % (selectStatement, numRows))
num_rows = self.row_count(selectStatement, sansTran)
logger.info('Executing : Row Count Is Less Than X | %s | %s | %s ' % (
selectStatement, numRows,alias))
num_rows = self.row_count(selectStatement, sansTran, alias)
logger.info('Row Num: %s ' % str(num_rows))
if num_rows >= int(numRows.encode('ascii')):
raise AssertionError("Expected less rows to be returned from '%s' "
"than the returned rows of %s" % (selectStatement, num_rows))

def table_must_exist(self, tableName, sansTran=False):
def table_must_exist(self, tableName, sansTran=False, alias=None):
"""
Check if the table given exists in the database. Set optional input `sansTran` to True to run command without an
explicit transaction commit or rollback.
Expand All @@ -203,15 +214,24 @@ def table_must_exist(self, tableName, sansTran=False):
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
| Table Must Exist | person | True |
"""
logger.info('Executing : Table Must Exist | %s ' % tableName)
if self.db_api_module_name in ["cx_Oracle"]:
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)
logger.info('Executing : Table Must Exist | %s | %s ' % (tableName,alias))

connection, module_api = self._get_cache(alias)

if module_api in ["cx_Oracle"]:
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 module_api in ["sqlite3"]:
selectStatement = (
"SELECT name FROM sqlite_master WHERE type='table' AND name='%s' COLLATE NOCASE" % tableName)
elif module_api 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, sansTran)
selectStatement = (
"SELECT * FROM information_schema.tables WHERE table_name='%s'" % tableName)
num_rows = self.row_count(selectStatement, sansTran, alias)
logger.info('Row Num: %s ' % str(num_rows))
if num_rows == 0:
raise AssertionError("Table '%s' does not exist in the db" % tableName)
raise AssertionError(
"Table '%s' does not exist in the db" % tableName)
Loading