Skip to content

Commit

Permalink
Merge pull request #66 from jerry57/master
Browse files Browse the repository at this point in the history
increase logging to info
  • Loading branch information
jerry57 committed Jan 24, 2017
2 parents c4981ab + 552cf9d commit 2a8ee0d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/DatabaseLibrary/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from robot.api import logger

class Assertion(object):
"""
Assertion handles all the assertions of Database Library.
Expand All @@ -35,6 +37,7 @@ def check_if_exists_in_database(self,selectStatement):
| Check If Exists In Database | SELECT id FROM person WHERE first_name = 'Franz Allan' | # PASS |
| Check If Exists In Database | SELECT id FROM person WHERE first_name = 'John' | # FAIL |
"""
logger.info ('Executing : Check If Exists In Database | %s ' % (selectStatement))
if not self.query(selectStatement):
raise AssertionError("Expected to have have at least one row from '%s' "
"but got 0 rows." % selectStatement)
Expand All @@ -59,6 +62,7 @@ def check_if_not_exists_in_database(self,selectStatement):
| Check If Not Exists In Database | SELECT id FROM person WHERE first_name = 'John' | # PASS |
| Check If Not Exists In Database | SELECT id FROM person WHERE first_name = 'Franz Allan' | # FAIL |
"""
logger.info('Executing : Check If Not Exists In Database | %s ' % (selectStatement))
queryResults = self.query(selectStatement)
if queryResults:
raise AssertionError("Expected to have have no rows from '%s' "
Expand All @@ -81,6 +85,7 @@ def row_count_is_0(self,selectStatement):
| Row Count is 0 | SELECT id FROM person WHERE first_name = 'Franz Allan' | # FAIL |
| Row Count is 0 | SELECT id FROM person WHERE first_name = 'John' | # PASS |
"""
logger.info('Executing : Row Count Is 0 | %s ' % (selectStatement))
num_rows = self.row_count(selectStatement)
if (num_rows > 0):
raise AssertionError("Expected zero rows to be returned from '%s' "
Expand All @@ -104,6 +109,7 @@ def row_count_is_equal_to_x(self,selectStatement,numRows):
| Row Count Is Equal To X | SELECT id FROM person | 1 | # FAIL |
| Row Count Is Equal To X | SELECT id FROM person WHERE first_name = 'John' | 0 | # PASS |
"""
logger.info('Executing : Row Count Is Equal To X | %s | %s ' % (selectStatement, numRows))
num_rows = self.row_count(selectStatement)
if (num_rows != int(numRows.encode('ascii'))):
raise AssertionError("Expected same number of rows to be returned from '%s' "
Expand All @@ -127,6 +133,7 @@ def row_count_is_greater_than_x(self,selectStatement,numRows):
| Row Count Is Greater Than X | SELECT id FROM person | 1 | # PASS |
| Row Count Is Greater Than X | SELECT id FROM person WHERE first_name = 'John' | 0 | # FAIL |
"""
logger.info('Executing : Row Count Is Greater Than X | %s | %s ' % (selectStatement, numRows))
num_rows = self.row_count(selectStatement)
if (num_rows <= int(numRows.encode('ascii'))):
raise AssertionError("Expected more rows to be returned from '%s' "
Expand All @@ -150,6 +157,7 @@ def row_count_is_less_than_x(self,selectStatement,numRows):
| Row Count Is Less Than X | SELECT id FROM person | 3 | # PASS |
| Row Count Is Less Than X | SELECT id FROM person WHERE first_name = 'John' | 1 | # FAIL |
"""
logger.info('Executing : Row Count Is Less Than X | %s | %s ' % (selectStatement, numRows))
num_rows = self.row_count(selectStatement)
if (num_rows >= int(numRows.encode('ascii'))):
raise AssertionError("Expected less rows to be returned from '%s' "
Expand All @@ -168,6 +176,7 @@ def table_must_exist(self,tableName):
| Table Must Exist | person | # PASS |
| Table Must Exist | first_name | # FAIL |
"""
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"]:
Expand Down
12 changes: 7 additions & 5 deletions src/DatabaseLibrary/connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,22 @@ def connect_to_database(self, dbapiModuleName=None, dbName=None, dbUsername=None
db_api_2 = importlib.import_module(dbapiModuleName)
if dbapiModuleName in ["MySQLdb", "pymysql"]:
dbPort = dbPort or 3306
logger.debug ('Connecting using : %s.connect(db=%s, user=%s, passwd=%s, host=%s, port=%s, charset=%s) ' % (dbapiModuleName, dbName, dbUsername, dbPassword, dbHost, dbPort, dbCharset))
logger.info ('Connecting using : %s.connect(db=%s, user=%s, passwd=%s, host=%s, port=%s, charset=%s) ' % (dbapiModuleName, dbName, dbUsername, dbPassword, dbHost, dbPort, dbCharset))
self._dbconnection = db_api_2.connect (db=dbName, user=dbUsername, passwd=dbPassword, host=dbHost, port=dbPort, charset=dbCharset)
elif dbapiModuleName in ["psycopg2"]:
dbPort = dbPort or 5432
logger.debug ('Connecting using : %s.connect(database=%s, user=%s, password=%s, host=%s, port=%s) ' % (dbapiModuleName, dbName, dbUsername, dbPassword, dbHost, dbPort))
logger.info ('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)
elif dbapiModuleName in ["pyodbc"]:
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,dbUsername, dbPassword))
logger.info ('Connecting using : %s.connect(DRIVER={SQL Server};SERVER=%s,%s;DATABASE=%s;UID=%s;PWD=%s)' % (dbapiModuleName,dbHost,dbPort,dbName,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))
logger.info ('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))
logger.info ('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)

def connect_to_database_using_custom_params(self, dbapiModuleName=None, db_connect_string=''):
Expand All @@ -116,6 +116,7 @@ def connect_to_database_using_custom_params(self, dbapiModuleName=None, db_conne
db_connect_string = 'db_api_2.connect(%s)' % db_connect_string

self.db_api_module_name = dbapiModuleName
logger.info('Executing : Connect To Database Using Custom Params : %s.connect(%s) ' % (db_connect_string))
self._dbconnection = eval(db_connect_string)

def disconnect_from_database(self):
Expand All @@ -125,4 +126,5 @@ def disconnect_from_database(self):
For example:
| Disconnect From Database | # disconnects from current connection to the database |
"""
logger.info('Executing : Disconnect From Database')
self._dbconnection.close()
8 changes: 7 additions & 1 deletion src/DatabaseLibrary/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def query(self, selectStatement):
cur = None
try:
cur = self._dbconnection.cursor()
logger.info ('Executing : Query | %s ' % (selectStatement))
self.__execute_sql(cur, selectStatement)
allRows = cur.fetchall()
return allRows
Expand Down Expand Up @@ -86,6 +87,7 @@ def row_count(self, selectStatement):
cur = None
try:
cur = self._dbconnection.cursor()
logger.info ('Executing : Row Count | %s ' % (selectStatement))
self.__execute_sql(cur, selectStatement)
data = cur.fetchall()
if self.db_api_module_name in ["sqlite3", "ibm_db", "ibm_db_dbi"]:
Expand Down Expand Up @@ -118,6 +120,7 @@ def description(self, selectStatement):
cur = None
try:
cur = self._dbconnection.cursor()
logger.info ('Executing : Description | %s ' % (selectStatement))
self.__execute_sql(cur, selectStatement)
description = cur.description
return description
Expand All @@ -144,6 +147,7 @@ def delete_all_rows_from_table(self, tableName):
selectStatement = ("DELETE FROM %s;" % tableName)
try:
cur = self._dbconnection.cursor()
logger.info ('Executing : Delete All Rows From Table | %s ' % (selectStatement))
result = self.__execute_sql(cur, selectStatement)
if result is not None:
self._dbconnection.commit()
Expand Down Expand Up @@ -210,6 +214,7 @@ def execute_sql_script(self, sqlScriptFileName):
cur = None
try:
cur = self._dbconnection.cursor()
logger.info('Executing : Execute SQL Script | %s ' % (sqlScriptFileName))
sqlStatement = ''
for line in sqlScriptFile:
line = line.strip()
Expand Down Expand Up @@ -256,12 +261,13 @@ def execute_sql_string(self, sqlString):
"""
try:
cur = self._dbconnection.cursor()
logger.info('Executing : Execute SQL String | %s ' % (sqlString))
self.__execute_sql(cur, sqlString)
self._dbconnection.commit()
finally:
if cur:
self._dbconnection.rollback()

def __execute_sql(self, cur, sqlStatement):
logger.debug("Executing : %s" % sqlStatement)
#logger.info("Executing : %s" % sqlStatement)
return cur.execute(sqlStatement)

0 comments on commit 2a8ee0d

Please sign in to comment.