Skip to content

Commit

Permalink
Allow custom params for keyword "Connect To Database" (#220)
Browse files Browse the repository at this point in the history
The entire connection logic and implementation was refactored

* There is only one mandatory parameter left - dbapiModuleName, it must be set - either as keyword argument or in config file.
* All other parameters are optional now. So if some connection data was missing, the error would come not from the Database Library, but from the Python DB module.
* If some params are not provided, they are not set to None - they are just not passed to the Python DB module at all.
* Other custom params from keyword arguments and config file are passed to the Python DB module as provided
* All parameters can be now set in a config file - including any custom params
* If same custom parameter is provided both as a keyword argument and in config file, the keyword argument value takes precedence.

Other changes

* Deprecate the Connect To Database Using Custom Params Keyword - it's not needed anymore, the updated Connect To Database keyword replaces it fully
* Stop using localhost as fallback value for DB host
* Stop using {SQL Server} as fallback value for pyodbc driver
* Update docs for the Connect To Database keyword, move docs for using the config file in a separate section
  • Loading branch information
amochin authored Sep 27, 2024
1 parent 2171d9f commit c044109
Show file tree
Hide file tree
Showing 42 changed files with 749 additions and 184 deletions.
51 changes: 36 additions & 15 deletions doc/index.html

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions src/DatabaseLibrary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,55 @@ class DatabaseLibrary(ConnectionManager, Query, Assertion):
| Execute Sql String drop table XYZ
|
= Using configuration file =
The `Connect To Database` keyword allows providing the connection parameters in two ways:
- As keyword arguments
- In a configuration file - a simple list of _key=value_ pairs, set inside an _alias_ section.
You can use only one way or you can combine them:
- The keyword arguments are taken by default
- If no keyword argument is provided, a parameter value is searched in the config file
Along with commonly used connection parameters, named exactly as keyword arguments, a config file
can contain any other DB module specific parameters as key/value pairs.
If same custom parameter is provided both as a keyword argument *and* in config file,
the *keyword argument value takes precedence*.
The path to the config file is set by default to `./resources/db.cfg`.
You can change it using an according parameter in the `Connect To Database` keyword.
A config file *must* contain at least one section name -
the connection alias, if used (see `Handling multiple database connections`), or
`[default]` if no aliases are used.
== Config file examples ==
=== Config file with default alias (equal to using no aliases at all) ===
| [default]
| dbapiModuleName=psycopg2
| dbName=yourdbname
| dbUsername=yourusername
| dbPassword=yourpassword
| dbHost=yourhost
| dbPort=yourport
=== Config file with a specific alias ===
| [myoracle]
| dbapiModuleName=oracledb
| dbName=yourdbname
| dbUsername=yourusername
| dbPassword=yourpassword
| dbHost=yourhost
| dbPort=yourport
=== Config file with some params only ===
| [default]
| dbPassword=mysecret
=== Config file with some custom DB module specific params ===
| [default]
| my_custom_param=value
= Inline assertions =
Keywords, that accept arguments ``assertion_operator`` <`AssertionOperator`> and ``expected_value``,
perform a check according to the specified condition - using the [https://github.com/MarketSquare/AssertionEngine|Assertion Engine].
Expand Down
384 changes: 232 additions & 152 deletions src/DatabaseLibrary/connection_manager.py

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions test/resources/common.resource
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Library Collections
Library OperatingSystem
Library DatabaseLibrary
Library DateTime
Resource config_files/connect_config_file.resource


*** Variables ***
Expand All @@ -26,8 +27,10 @@ Connect To DB
[Documentation] Connects to the database based on the current DB module under test
... and connection params set in global variables with alias
[Arguments] ${alias}=${None}
${DB_KWARGS} Create Dictionary
IF $alias is not None Set To Dictionary ${DB_KWARGS} alias=${alias}
${DB_KWARGS}= Create Dictionary
IF $alias is not None
Set To Dictionary ${DB_KWARGS} alias=${alias}
END
IF "${DB_MODULE_MODE}" == "custom"
IF "${DB_MODULE}" == "sqlite3"
Remove File ${DBName}.db
Expand All @@ -38,8 +41,16 @@ Connect To DB
Connect To Database Using Custom Connection String ${DB_MODULE} ${Connection String} &{DB_KWARGS}
END
ELSE IF "${DB_MODULE_MODE}" == "standard"
${DB_ARGS} Create List ${DB_MODULE} ${DB_NAME} ${DB_USER} ${DB_PASS} ${DB_HOST} ${DB_PORT}
IF "${DB_MODULE}" == "pyodbc" Set To Dictionary ${DB_KWARGS} dbDriver=${DB_DRIVER}
${DB_ARGS}= Create List
... ${DB_MODULE}
... ${DB_NAME}
... ${DB_USER}
... ${DB_PASS}
... ${DB_HOST}
... ${DB_PORT}
IF "${DB_MODULE}" == "pyodbc"
Set To Dictionary ${DB_KWARGS} dbDriver=${DB_DRIVER}
END
Connect To Database @{DB_ARGS} &{DB_KWARGS}
ELSE
Fail Unexpected mode - ${DB_MODULE_MODE}
Expand Down
11 changes: 11 additions & 0 deletions test/resources/config_files/connect_config_file.resource
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*** Settings ***
Resource ../common.resource


*** Keywords ***
Connect Using Config File
[Documentation] `File name` is only name without extension,
... the path is build relative to the resource directory
[Arguments] ${File name}=${None} &{Params}
${Path}= Set Variable ${CURDIR}/${File name}.cfg
Connect To Database dbConfigFile=${Path} &{Params}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[default]
dbapiModuleName=oracledb
dbName=db
password=pass
dbHost=127.0.0.1
dbPort=1521
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[default]
dbapiModuleName=oracledb
dbName=db
dbUsername=db_user
dbPassword=pass
dbHost=127.0.0.1
dbPort=1521
blah=blah
8 changes: 8 additions & 0 deletions test/resources/config_files/oracledb/simple_default_alias.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[default]
dbapiModuleName=oracledb
dbName=db
dbUsername=db_user
dbPassword=pass
dbHost=127.0.0.1
dbPort=1521
driverMode=thin
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[default]
dbapiModuleName=oracledb
dbName=db
8 changes: 8 additions & 0 deletions test/resources/config_files/oracledb/thick_mode.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[default]
dbapiModuleName=oracledb
dbName=db
dbUsername=db_user
dbPassword=pass
dbHost=127.0.0.1
dbPort=1521
driverMode=thick
7 changes: 7 additions & 0 deletions test/resources/config_files/oracledb/valid_custom_params.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[default]
dbapiModuleName=oracledb
dbName=db
user=db_user
password=pass
dbHost=127.0.0.1
dbPort=1521
7 changes: 7 additions & 0 deletions test/resources/config_files/oracledb/wrong_password.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[default]
dbapiModuleName=oracledb
dbName=db
dbUsername=db_user
dbPassword=wrong
dbHost=127.0.0.1
dbPort=1521
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[default]
dbapiModuleName=psycopg2
dbName=db
password=pass
dbHost=127.0.0.1
dbPort=5432
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[default]
dbapiModuleName=psycopg2
dbName=db
dbUsername=db_user
dbPassword=pass
dbHost=127.0.0.1
dbPort=5432
blah=blah
7 changes: 7 additions & 0 deletions test/resources/config_files/psycopg2/simple_default_alias.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[default]
dbapiModuleName=psycopg2
dbName=db
dbUsername=db_user
dbPassword=pass
dbHost=127.0.0.1
dbPort=5432
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[default]
dbapiModuleName=psycopg2
dbName=db
7 changes: 7 additions & 0 deletions test/resources/config_files/psycopg2/valid_custom_params.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[default]
dbapiModuleName=psycopg2
dbName=db
user=db_user
password=pass
dbHost=127.0.0.1
dbPort=5432
7 changes: 7 additions & 0 deletions test/resources/config_files/psycopg2/wrong_password.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[default]
dbapiModuleName=psycopg2
dbName=db
dbUsername=db_user
dbPassword=wrong
dbHost=127.0.0.1
dbPort=5432
8 changes: 8 additions & 0 deletions test/resources/config_files/pymssql/charset_invalid.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[default]
dbapiModuleName=pymssql
dbName=db
user=SA
password=MyPass1234!
dbHost=127.0.0.1
dbPort=1433
dbCharset=wrong
6 changes: 6 additions & 0 deletions test/resources/config_files/pymssql/custom_param_password.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[default]
dbapiModuleName=pymssql
dbName=db
password=MyPass1234!
dbHost=127.0.0.1
dbPort=1433
8 changes: 8 additions & 0 deletions test/resources/config_files/pymssql/invalid_custom_params.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[default]
dbapiModuleName=pymssql
dbName=db
dbUsername=SA
dbPassword=MyPass1234!
dbHost=127.0.0.1
dbPort=1433
blah=blah
7 changes: 7 additions & 0 deletions test/resources/config_files/pymssql/simple_default_alias.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[default]
dbapiModuleName=pymssql
dbName=db
dbUsername=SA
dbPassword=MyPass1234!
dbHost=127.0.0.1
dbPort=1433
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[default]
dbapiModuleName=pymssql
dbName=db
7 changes: 7 additions & 0 deletions test/resources/config_files/pymssql/valid_custom_params.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[default]
dbapiModuleName=pymssql
dbName=db
user=SA
password=MyPass1234!
dbHost=127.0.0.1
dbPort=1433
7 changes: 7 additions & 0 deletions test/resources/config_files/pymssql/wrong_password.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[default]
dbapiModuleName=pymssql
dbName=db
dbUsername=SA
dbPassword=wrong
dbHost=127.0.0.1
dbPort=1433
8 changes: 8 additions & 0 deletions test/resources/config_files/pymysql/charset_invalid.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[default]
dbapiModuleName=pymysql
dbName=db
user=db_user
password=pass
dbHost=127.0.0.1
dbPort=3306
dbCharset=wrong
6 changes: 6 additions & 0 deletions test/resources/config_files/pymysql/custom_param_password.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[default]
dbapiModuleName=pymysql
dbName=db
password=pass
dbHost=127.0.0.1
dbPort=3306
8 changes: 8 additions & 0 deletions test/resources/config_files/pymysql/invalid_custom_params.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[default]
dbapiModuleName=pymysql
dbName=db
dbUsername=db_user
dbPassword=pass
dbHost=127.0.0.1
dbPort=3306
blah=blah
7 changes: 7 additions & 0 deletions test/resources/config_files/pymysql/simple_default_alias.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[default]
dbapiModuleName=pymysql
dbName=db
dbUsername=db_user
dbPassword=pass
dbHost=127.0.0.1
dbPort=3306
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[default]
dbapiModuleName=pymysql
dbName=db
7 changes: 7 additions & 0 deletions test/resources/config_files/pymysql/valid_custom_params.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[default]
dbapiModuleName=pymysql
dbName=db
user=db_user
password=pass
dbHost=127.0.0.1
dbPort=3306
7 changes: 7 additions & 0 deletions test/resources/config_files/pymysql/wrong_password.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[default]
dbapiModuleName=pymysql
dbName=db
dbUsername=db_user
dbPassword=wrong
dbHost=127.0.0.1
dbPort=3306
9 changes: 9 additions & 0 deletions test/resources/config_files/pyodbc/charset_invalid.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[default]
dbapiModuleName=pyodbc
dbName=db
user=db_user
password=pass
dbHost=127.0.0.1
dbPort=3306
dbDriver={MySQL ODBC 8.0 ANSI Driver}
dbCharset=wrong
7 changes: 7 additions & 0 deletions test/resources/config_files/pyodbc/custom_param_password.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[default]
dbapiModuleName=pyodbc
dbName=db
PWD=pass
dbHost=127.0.0.1
dbPort=3306
dbDriver={MySQL ODBC 8.0 ANSI Driver}
9 changes: 9 additions & 0 deletions test/resources/config_files/pyodbc/invalid_custom_params.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[default]
dbapiModuleName=pyodbc
dbName=db
dbUsername=db_user
dbPassword=pass
dbHost=127.0.0.1
dbPort=3306
dbDriver={MySQL ODBC 8.0 ANSI Driver}
blah=blah
8 changes: 8 additions & 0 deletions test/resources/config_files/pyodbc/simple_default_alias.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[default]
dbapiModuleName=pyodbc
dbName=db
dbUsername=db_user
dbPassword=pass
dbHost=127.0.0.1
dbPort=3306
dbDriver={MySQL ODBC 8.0 ANSI Driver}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[default]
dbapiModuleName=pyodbc
dbName=db
8 changes: 8 additions & 0 deletions test/resources/config_files/pyodbc/valid_custom_params.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[default]
dbapiModuleName=pyodbc
dbName=db
UID=db_user
PWD=pass
dbHost=127.0.0.1
dbPort=3306
dbDriver={MySQL ODBC 8.0 ANSI Driver}
Loading

0 comments on commit c044109

Please sign in to comment.