Skip to content

Commit

Permalink
Update connection setup & teardown in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bhirsz committed Nov 9, 2023
1 parent d619a92 commit 32722b1
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 82 deletions.
30 changes: 0 additions & 30 deletions run_tests.sh

This file was deleted.

6 changes: 3 additions & 3 deletions src/DatabaseLibrary/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def row_count_is_equal_to_x(
numRows: str,
sansTran: bool = False,
msg: Optional[str] = None,
alias: str = "default",
alias: Optional[str] = None,
):
"""
Check if the number of rows returned from `selectStatement` is equal to the value submitted. If not, then this
Expand Down Expand Up @@ -180,7 +180,7 @@ def row_count_is_greater_than_x(
numRows: str,
sansTran: bool = False,
msg: Optional[str] = None,
alias: str = "default",
alias: Optional[str] = None,
):
"""
Check if the number of rows returned from `selectStatement` is greater than the value submitted. If not, then
Expand Down Expand Up @@ -223,7 +223,7 @@ def row_count_is_less_than_x(
numRows: str,
sansTran: bool = False,
msg: Optional[str] = None,
alias: str = "default",
alias: Optional[str] = None,
):
"""
Check if the number of rows returned from `selectStatement` is less than the value submitted. If not, then this
Expand Down
10 changes: 8 additions & 2 deletions src/DatabaseLibrary/connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def __init__(self):

def _register_connection(self, client: Any, module_name: str, alias: str):
if alias in self._connections:
logger.warn(f"Overwriting not closed connection for alias = '{alias}'")
if alias == self.default_alias:
logger.warn("Overwriting not closed connection.")
else:
logger.warn(f"Overwriting not closed connection for alias = '{alias}'")
self._connections[alias] = Connection(client, module_name)

def connect_to_database(
Expand Down Expand Up @@ -354,7 +357,10 @@ def disconnect_from_database(self, error_if_no_connection: bool = False, alias:
"""
logger.info("Executing : Disconnect From Database")
if not alias:
alias = self.default_alias
if not self._connections or self.default_alias in self._connections:
alias = self.default_alias
else:
alias = list(self._connections.keys())[-1]
try:
db_connection = self._connections.pop(alias)
db_connection.client.close()
Expand Down
10 changes: 10 additions & 0 deletions test/resources/common.resource
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ Connect To DB Aliased
Fail Unexpected mode - ${DB_MODULE_MODE}
END

Aliased Suite Setup
Connect To DB
Create Person Table
Disconnect From Database

Aliased Suite Teardown
Connect To DB
Drop Tables Person And Foobar
Disconnect From Database

Build Connection String
[Documentation] Returns the connection string variable depending on the DB module
... currently under test.
Expand Down
31 changes: 9 additions & 22 deletions test/tests/_old/DB2SQL_DB_Tests.robot
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
*** Settings ***
Suite Setup Connect To Database ibm_db_dbi ${DBName} ${DBUser} ${DBPass} ${DBHost} ${DBPort}
Suite Teardown Disconnect From Database
Resource DB2SQL_DB_Conf.txt
Library DatabaseLibrary
Library Collections
Force Tags optional

*** Test Cases ***
Create person table
Expand All @@ -12,8 +11,8 @@ Create person table
Should Be Equal As Strings ${output} None

Execute SQL Script - Insert Data person table
Comment ${output} = Execute SQL Script ${CURDIR}/my_db_test_insertData.sql
${output} = Execute SQL Script ${CURDIR}/my_db_test_insertData.sql
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} None

Expand Down Expand Up @@ -54,20 +53,11 @@ Verify person Description
@{queryResults} = Description SELECT * FROM person fetch first 1 rows only;
Log Many @{queryResults}
${output} = Set Variable ${queryResults[0]}
Should Be Equal As Strings ${output[0]} ID
${expected}= Evaluate ['DEC', 'NUMERIC', 'DECIMAL', 'NUM']
Lists Should Be Equal ${output[1].col_types} ${expected} ignore_order=True
Should Be Equal As Strings ${output[2:]} [12, 12, 10, 0, True]
Should Be Equal As Strings ${output} ['ID', DBAPITypeObject(['NUM', 'DECIMAL', 'DEC', 'NUMERIC']), 12, 12, 10, 0, True]
${output} = Set Variable ${queryResults[1]}
Should Be Equal As Strings ${output[0]} FIRST_NAME
${expected}= Evaluate ['VARCHAR', 'CHARACTER VARYING', 'STRING', 'CHARACTER', 'CHAR', 'CHAR VARYING']
Lists Should Be Equal ${output[1].col_types} ${expected} ignore_order=True
Should Be Equal As Strings ${output[2:]} [30, 30, 30, 0, True]
Should Be Equal As Strings ${output} ['FIRST_NAME', DBAPITypeObject(['CHARACTER VARYING', 'CHAR VARYING', 'VARCHAR', 'STRING', 'CHARACTER', 'CHAR']), 30, 30, 30, 0, True]
${output} = Set Variable ${queryResults[2]}
Should Be Equal As Strings ${output[0]} LAST_NAME
${expected}= Evaluate ['CHAR', 'CHAR VARYING', 'VARCHAR', 'CHARACTER VARYING', 'CHARACTER', 'STRING']
Lists Should Be Equal ${output[1].col_types} ${expected} ignore_order=True
Should Be Equal As Strings ${output[2:]} [30, 30, 30, 0, True]
Should Be Equal As Strings ${output} ['LAST_NAME', DBAPITypeObject(['CHARACTER VARYING', 'CHAR VARYING', 'VARCHAR', 'STRING', 'CHARACTER', 'CHAR']), 30, 30, 30, 0, True]
${NumColumns} = Get Length ${queryResults}
Should Be Equal As Integers ${NumColumns} 3

Expand All @@ -85,8 +75,8 @@ Verify Query - Get results as a list of dictionaries
[Tags] db smoke
${output} = Query SELECT * FROM person; \ True
Log ${output}
Should Be Equal As Strings ${output[0]['FIRST_NAME']} Franz Allan
Should Be Equal As Strings ${output[1]['FIRST_NAME']} Jerry
Should Be Equal As Strings &{output[0]}[first_name] Franz Allan
Should Be Equal As Strings &{output[1]}[first_name] Jerry

Insert Data Into Table foobar
${output} = Execute SQL String INSERT INTO foobar VALUES(1,'Jerry');
Expand All @@ -106,7 +96,4 @@ Verify Query - Row Count foobar table 0 row

Drop person and foobar table
Execute SQL String DROP TABLE person;
Execute SQL String DROP TABLE foobar;

Disconnect from all databases
Disconnect From All Databases
Execute SQL String DROP TABLE foobar;
3 changes: 0 additions & 3 deletions test/tests/_old/MySQL_DB_Tests.robot
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,3 @@ Drop person and foobar tables
${output} = Execute SQL String DROP TABLE IF EXISTS person,foobar;
Log ${output}
Should Be Equal As Strings ${output} None

Disconnect from all databases
Disconnect From All Databases
3 changes: 0 additions & 3 deletions test/tests/_old/PostgreSQL_DB_Tests.robot
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,3 @@ Drop person and foobar tables
${output} = Execute SQL String DROP TABLE IF EXISTS person,foobar;
Log ${output}
Should Be Equal As Strings ${output} None

Disconnect from all databases
Disconnect From All Databases
21 changes: 2 additions & 19 deletions test/tests/common_tests/aliased_connection.robot
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
*** Settings ***
Resource ../../resources/common.resource
Suite Setup Run Keywords
... Connect To DB
... AND
... Create Person Table
Suite Teardown Run Keywords
... Connect To DB
... AND
... Drop Tables Person And Foobar
Suite Setup Aliased Suite Setup
Suite Teardown Aliased Suite Teardown
Test Teardown Disconnect From All Databases


Expand Down Expand Up @@ -116,14 +110,3 @@ Verify Delete All Rows From Table
Connect To DB Aliased alias=aliased_conn
Delete All Rows From Table person alias=aliased_conn
Row Count Is 0 SELECT * FROM person alias=aliased_conn


*** Keywords ***
Aliases Suite Setup
Connect To DB Aliased
Create Person Table

Aliases Suite Teardown
Connect To DB Aliased
Drop Tables Person And Foobar
Disconnect From All Databases

0 comments on commit 32722b1

Please sign in to comment.