MarketSquare / Robotframework-Database-Library

The Database Library for Robot Framework allows you to query a database and verify the results using different Python DB modules (installed separately).
http://marketsquare.github.io/Robotframework-Database-Library/
Other
157 stars 173 forks source link

Self managed connections #187

Open bhirsz opened 11 months ago

bhirsz commented 11 months ago

One of the major problems with using libraries like DatabaseLibrary is managing the connection. If we implement #149, it could cause additional problems. Typically QA need to implement following logic:

Connect To Database  (...)
Query
[Teardown]    Disconnect Connection

But often Disconnect Connection is not in teardown (so the connection can remain open after test fails), we could several long running connections even if we're not querying for anything. For that reason we could add optional option for 'self managed connection' - in short, we could open and close connection each time we perform an query.

The default behaviour of the library will not change, it will be only extra option that will open/close connection if it doesn't exist.

*** Settings ***
Library    DatabaseLibrary    auto_connect=${True}
Suite Setup    Configure Connection    ${params}  # need to configure default connection

*** Test Cases ***
Test 1
    Query    SELECT * FROM table1  # will open and close connection on its own

Test 2
    Query    SELECT * FROM table2  # will open and close connection on its own
amochin commented 11 months ago

This is a great idea! We could also consider setting the connection details as library input params.

wvanmourik-aa commented 10 months ago

+1

For our use case, the database(s) is what we are testing and it makes little sense to open and close connections per query or statement. Instead, we want the connection to stay open until the entire test has been completed; encompassing multiple queries/statements.

So for us, the new logged warnings for reusing connections are an annoyance in the test reports.

(We do have the close connection call in the teardown still, regardless.)

bhirsz commented 10 months ago

+1

For our use case, the database(s) is what we are testing and it makes little sense to open and close connections per query or statement. Instead, we want the connection to stay open until the entire test has been completed; encompassing multiple queries/statements.

So for us, the new logged warnings for reusing connections are an annoyance in the test reports.

(We do have the close connection call in the teardown still, regardless.)

Looking at implementation, you should only receive reusing connection if you're trying to open connection without closing previous one. For your case we could add more control over what happens in such case:

Connect To Database    &{params}    alias=conn1
Connect To Database    &{params}    alias=conn1

1) By default, warn you're overwriting open connection but proceed with it (as it is now) 2) Add flag to optionally disable warning from 1) 3) Add flag to automatically close previous connection (in this case conn1 will be closed and new connection will be open) 4) Add flag to not open connection if there is connection with given alias

It could done using one variable, ie if_conn_exist = "log:warn,old:ignore,new:open", "log:ignore,old:close,new:open", "log:warn:old:ignore,new:ignore" - but I need to think on how to make it more user friendly.. .

bhirsz commented 10 months ago

Also, from self managed connection perspective, the goal was to create short lived connections. But in your case it could be extended to allow long lived connection - configure connection when importing library and whener it's necessary it will be opened automatically and remain open till the library is deleted (when tests are finished) or disconnect is called manually.

wvanmourik-aa commented 10 months ago

Option 4 would be excellent for our use-case; if there already is a connection, then (re)use it.