Skip to content

Commit

Permalink
Remove redundant logging and improve log formatting for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
amochin committed Aug 15, 2024
1 parent 60ba7af commit e4fbe12
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 25 deletions.
12 changes: 0 additions & 12 deletions src/DatabaseLibrary/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def check_if_exists_in_database(
| @{parameters} | Create List | John |
| Check If Exists In Database | SELECT id FROM person WHERE first_name = %s | parameters=${parameters} |
"""
logger.info(f"Executing : Check If Exists In Database | {selectStatement}")
if not self.query(selectStatement, sansTran, alias=alias, parameters=parameters):
raise AssertionError(
msg or f"Expected to have have at least one row, but got 0 rows from: '{selectStatement}'"
Expand Down Expand Up @@ -99,7 +98,6 @@ def check_if_not_exists_in_database(
| @{parameters} | Create List | John |
| Check If Not Exists In Database | SELECT id FROM person WHERE first_name = %s | parameters=${parameters} |
"""
logger.info(f"Executing : Check If Not Exists In Database | {selectStatement}")
query_results = self.query(selectStatement, sansTran, alias=alias, parameters=parameters)
if query_results:
raise AssertionError(
Expand Down Expand Up @@ -140,7 +138,6 @@ def row_count_is_0(
| @{parameters} | Create List | John |
| Row Count is 0 | SELECT id FROM person WHERE first_name = %s | parameters=${parameters} |
"""
logger.info(f"Executing : Row Count Is 0 | {selectStatement}")
num_rows = self.row_count(selectStatement, sansTran, alias=alias, parameters=parameters)
if num_rows > 0:
raise AssertionError(msg or f"Expected 0 rows, but {num_rows} were returned from: '{selectStatement}'")
Expand Down Expand Up @@ -179,7 +176,6 @@ def row_count_is_equal_to_x(
| @{parameters} | Create List | John |
| Row Count Is Equal To X | SELECT id FROM person WHERE first_name = %s | 0 | parameters=${parameters} |
"""
logger.info(f"Executing : Row Count Is Equal To X | {selectStatement} | {numRows}")
num_rows = self.row_count(selectStatement, sansTran, alias=alias, parameters=parameters)
if num_rows != int(numRows.encode("ascii")):
raise AssertionError(
Expand Down Expand Up @@ -220,7 +216,6 @@ def row_count_is_greater_than_x(
| @{parameters} | Create List | John |
| Row Count Is Greater Than X | SELECT id FROM person WHERE first_name = %s | 0 | parameters=${parameters} |
"""
logger.info(f"Executing : Row Count Is Greater Than X | {selectStatement} | {numRows}")
num_rows = self.row_count(selectStatement, sansTran, alias=alias, parameters=parameters)
if num_rows <= int(numRows.encode("ascii")):
raise AssertionError(
Expand Down Expand Up @@ -261,7 +256,6 @@ def row_count_is_less_than_x(
| @{parameters} | Create List | John |
| Row Count Is Less Than X | SELECT id FROM person WHERE first_name = %s | 5 | parameters=${parameters} |
"""
logger.info(f"Executing : Row Count Is Less Than X | {selectStatement} | {numRows}")
num_rows = self.row_count(selectStatement, sansTran, alias=alias, parameters=parameters)
if num_rows >= int(numRows.encode("ascii")):
raise AssertionError(
Expand Down Expand Up @@ -305,7 +299,6 @@ def check_row_count(
| @{parameters} | Create List | John |
| Check Row Count | SELECT id FROM person WHERE first_name = %s | *equals* | 5 | parameters=${parameters} |
"""
logger.info(f"Executing : Check Row Count | {selectStatement} | {assertion_operator} | {expected_value}")
check_ok = False
time_counter = 0
while not check_ok:
Expand Down Expand Up @@ -367,10 +360,6 @@ def check_query_result(
| @{parameters} | Create List | John |
| Check Query Result | SELECT first_name FROM person | *contains* | Allan | parameters=${parameters} |
"""
logger.info(
f"Executing : Check Query Results | {selectStatement} | {assertion_operator} | {expected_value} | row = {row} | col = {col} "
)

check_ok = False
time_counter = 0
while not check_ok:
Expand Down Expand Up @@ -416,7 +405,6 @@ def table_must_exist(
| Table Must Exist | person | alias=my_alias |
| Table Must Exist | person | sansTran=True |
"""
logger.info(f"Executing : Table Must Exist | {tableName}")
db_connection = self.connection_store.get_connection(alias)
if db_connection.module_name in ["cx_Oracle", "oracledb"]:
query = (
Expand Down
3 changes: 0 additions & 3 deletions src/DatabaseLibrary/connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,6 @@ def disconnect_from_database(self, error_if_no_connection: bool = False, alias:
| Disconnect From Database | # disconnects from current connection to the database |
| Disconnect From Database | alias=my_alias | # disconnects from current connection to the database |
"""
logger.info("Executing : Disconnect From Database")
db_connection = self.connection_store.pop_connection(alias)
if db_connection is None:
log_msg = "No open database connection to close"
Expand All @@ -437,7 +436,6 @@ def disconnect_from_all_databases(self):
For example:
| Disconnect From All Databases | # Closes connections to all databases |
"""
logger.info("Executing : Disconnect From All Databases")
for db_connection in self.connection_store:
db_connection.client.close()
self.connection_store.clear()
Expand All @@ -459,7 +457,6 @@ def set_auto_commit(self, autoCommit: bool = True, alias: Optional[str] = None):
| # Explicitly set the desired state
| Set Auto Commit | False
"""
logger.info("Executing : Set Auto Commit")
db_connection = self.connection_store.get_connection(alias)
db_connection.client.autocommit = autoCommit

Expand Down
15 changes: 5 additions & 10 deletions src/DatabaseLibrary/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ def query(
cur = None
try:
cur = db_connection.client.cursor()
logger.info(f"Executing : Query | {selectStatement} ")
self._execute_sql(cur, selectStatement, parameters=parameters)
all_rows = cur.fetchall()
col_names = [c[0] for c in cur.description]
Expand Down Expand Up @@ -127,7 +126,6 @@ def row_count(
cur = None
try:
cur = db_connection.client.cursor()
logger.info(f"Executing : Row Count | {selectStatement}")
self._execute_sql(cur, selectStatement, parameters=parameters)
data = cur.fetchall()
col_names = [c[0] for c in cur.description]
Expand Down Expand Up @@ -181,7 +179,6 @@ def description(
cur = None
try:
cur = db_connection.client.cursor()
logger.info("Executing : Description | {selectStatement}")
self._execute_sql(cur, selectStatement, parameters=parameters)
description = list(cur.description)
if sys.version_info[0] < 3:
Expand Down Expand Up @@ -211,7 +208,6 @@ def delete_all_rows_from_table(self, tableName: str, sansTran: bool = False, ali
query = f"DELETE FROM {tableName}"
try:
cur = db_connection.client.cursor()
logger.info(f"Executing : Delete All Rows From Table | {query}")
result = self._execute_sql(cur, query)
if result is not None:
if not sansTran:
Expand Down Expand Up @@ -293,7 +289,6 @@ def execute_sql_script(
cur = None
try:
cur = db_connection.client.cursor()
logger.info(f"Executing : Execute SQL Script | {sqlScriptFileName}")
if not split:
logger.info("Statements splitting disabled - pass entire script content to the database module")
self._execute_sql(cur, sql_file.read())
Expand Down Expand Up @@ -359,7 +354,6 @@ def execute_sql_script(
statements_to_execute.append(current_statement)

for statement in statements_to_execute:
logger.info(f"Executing statement from script file: {statement}")
line_ends_with_proc_end = re.compile(r"(\s|;)" + proc_end_pattern.pattern + "$")
omit_semicolon = not line_ends_with_proc_end.search(statement.lower())
self._execute_sql(cur, statement, omit_semicolon)
Expand Down Expand Up @@ -406,7 +400,6 @@ def execute_sql_string(
cur = None
try:
cur = db_connection.client.cursor()
logger.info(f"Executing : Execute SQL String | {sqlString}")
self._execute_sql(cur, sqlString, omit_trailing_semicolon=omitTrailingSemicolon, parameters=parameters)
if not sansTran:
db_connection.client.commit()
Expand Down Expand Up @@ -461,7 +454,6 @@ def call_stored_procedure(
spParams = []
cur = None
try:
logger.info(f"Executing : Call Stored Procedure | {spName} | {spParams}")
if db_connection.module_name == "pymssql":
cur = db_connection.client.cursor(as_dict=False)
else:
Expand Down Expand Up @@ -593,10 +585,13 @@ def _execute_sql(
if omit_trailing_semicolon:
sql_statement = sql_statement.rstrip(";")
if parameters is None:
logger.debug(f"Executing sql '{sql_statement}' without parameters")
logger.info(f'Executing sql:<br><code style="font-weight: bold;">{sql_statement}</code>', html=True)
return cur.execute(sql_statement)
else:
logger.debug(f"Executing sql '{sql_statement}' with parameters: {parameters}")
logger.info(
f'Executing sql:<br><code style="font-weight: bold;">{sql_statement}</code><br>Parameters: <code style="font-weight: bold;">{parameters}</code>',
html=True,
)
return cur.execute(sql_statement, parameters)

def _log_query_results(self, col_names, result_rows, log_head: Optional[int] = None):
Expand Down

0 comments on commit e4fbe12

Please sign in to comment.