Skip to content

Commit

Permalink
fix #178 - keyword 'table must exist' can now handle DB's that don't …
Browse files Browse the repository at this point in the history
…support information schema
  • Loading branch information
amochin committed Jun 9, 2023
1 parent 0aea0ee commit 388dd88
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/DatabaseLibrary/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,26 @@ def table_must_exist(self, tableName, sansTran=False):
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)
table_exists = self.row_count(selectStatement, sansTran) > 0
elif self.db_api_module_name in ["sqlite3"]:
selectStatement = ("SELECT name FROM sqlite_master WHERE type='table' AND name='%s' COLLATE NOCASE" % tableName)
table_exists = self.row_count(selectStatement, sansTran) > 0
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)
table_exists = self.row_count(selectStatement, sansTran) > 0
elif self.db_api_module_name in ["teradata"]:
selectStatement = ("SELECT TableName FROM DBC.TablesV WHERE TableKind='T' AND TableName='%s'" % tableName)
table_exists = self.row_count(selectStatement, sansTran) > 0
else:
selectStatement = ("SELECT * FROM information_schema.tables WHERE table_name='%s'" % tableName)
num_rows = self.row_count(selectStatement, sansTran)
if num_rows == 0:
raise AssertionError("Table '%s' does not exist in the db" % tableName)
try:
selectStatement = (f"SELECT * FROM information_schema.tables WHERE table_name='{tableName}'")
table_exists = self.row_count(selectStatement, sansTran) > 0
except:
logger.info("Database doesn't support information schema, try using a simple SQL request")
try:
selectStatement = (f"SELECT 1 from {tableName} where 1=0")
num_rows = self.row_count(selectStatement, sansTran)
table_exists = True
except:
table_exists = False
assert table_exists, f"Table '{tableName}' does not exist in the db"
2 changes: 1 addition & 1 deletion test/Oracle_Custom_Params_Tests.robot
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ 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
[Tags] db smoke bug
[Tags] db smoke
Table Must Exist person

Verify Row Count is 0
Expand Down

0 comments on commit 388dd88

Please sign in to comment.