Skip to content

Commit c044109

Browse files
authored
Allow custom params for keyword "Connect To Database" (#220)
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
1 parent 2171d9f commit c044109

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+749
-184
lines changed

doc/index.html

Lines changed: 36 additions & 15 deletions
Large diffs are not rendered by default.

src/DatabaseLibrary/__init__.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,55 @@ class DatabaseLibrary(ConnectionManager, Query, Assertion):
9393
| Execute Sql String drop table XYZ
9494
|
9595
96+
= Using configuration file =
97+
The `Connect To Database` keyword allows providing the connection parameters in two ways:
98+
- As keyword arguments
99+
- In a configuration file - a simple list of _key=value_ pairs, set inside an _alias_ section.
100+
101+
You can use only one way or you can combine them:
102+
- The keyword arguments are taken by default
103+
- If no keyword argument is provided, a parameter value is searched in the config file
104+
105+
Along with commonly used connection parameters, named exactly as keyword arguments, a config file
106+
can contain any other DB module specific parameters as key/value pairs.
107+
If same custom parameter is provided both as a keyword argument *and* in config file,
108+
the *keyword argument value takes precedence*.
109+
110+
The path to the config file is set by default to `./resources/db.cfg`.
111+
You can change it using an according parameter in the `Connect To Database` keyword.
112+
113+
A config file *must* contain at least one section name -
114+
the connection alias, if used (see `Handling multiple database connections`), or
115+
`[default]` if no aliases are used.
116+
117+
== Config file examples ==
118+
=== Config file with default alias (equal to using no aliases at all) ===
119+
| [default]
120+
| dbapiModuleName=psycopg2
121+
| dbName=yourdbname
122+
| dbUsername=yourusername
123+
| dbPassword=yourpassword
124+
| dbHost=yourhost
125+
| dbPort=yourport
126+
127+
=== Config file with a specific alias ===
128+
| [myoracle]
129+
| dbapiModuleName=oracledb
130+
| dbName=yourdbname
131+
| dbUsername=yourusername
132+
| dbPassword=yourpassword
133+
| dbHost=yourhost
134+
| dbPort=yourport
135+
136+
=== Config file with some params only ===
137+
| [default]
138+
| dbPassword=mysecret
139+
140+
=== Config file with some custom DB module specific params ===
141+
| [default]
142+
| my_custom_param=value
143+
144+
96145
= Inline assertions =
97146
Keywords, that accept arguments ``assertion_operator`` <`AssertionOperator`> and ``expected_value``,
98147
perform a check according to the specified condition - using the [https://github.com/MarketSquare/AssertionEngine|Assertion Engine].

src/DatabaseLibrary/connection_manager.py

Lines changed: 232 additions & 152 deletions
Large diffs are not rendered by default.

test/resources/common.resource

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Library Collections
66
Library OperatingSystem
77
Library DatabaseLibrary
88
Library DateTime
9+
Resource config_files/connect_config_file.resource
910

1011

1112
*** Variables ***
@@ -26,8 +27,10 @@ Connect To DB
2627
[Documentation] Connects to the database based on the current DB module under test
2728
... and connection params set in global variables with alias
2829
[Arguments] ${alias}=${None}
29-
${DB_KWARGS} Create Dictionary
30-
IF $alias is not None Set To Dictionary ${DB_KWARGS} alias=${alias}
30+
${DB_KWARGS}= Create Dictionary
31+
IF $alias is not None
32+
Set To Dictionary ${DB_KWARGS} alias=${alias}
33+
END
3134
IF "${DB_MODULE_MODE}" == "custom"
3235
IF "${DB_MODULE}" == "sqlite3"
3336
Remove File ${DBName}.db
@@ -38,8 +41,16 @@ Connect To DB
3841
Connect To Database Using Custom Connection String ${DB_MODULE} ${Connection String} &{DB_KWARGS}
3942
END
4043
ELSE IF "${DB_MODULE_MODE}" == "standard"
41-
${DB_ARGS} Create List ${DB_MODULE} ${DB_NAME} ${DB_USER} ${DB_PASS} ${DB_HOST} ${DB_PORT}
42-
IF "${DB_MODULE}" == "pyodbc" Set To Dictionary ${DB_KWARGS} dbDriver=${DB_DRIVER}
44+
${DB_ARGS}= Create List
45+
... ${DB_MODULE}
46+
... ${DB_NAME}
47+
... ${DB_USER}
48+
... ${DB_PASS}
49+
... ${DB_HOST}
50+
... ${DB_PORT}
51+
IF "${DB_MODULE}" == "pyodbc"
52+
Set To Dictionary ${DB_KWARGS} dbDriver=${DB_DRIVER}
53+
END
4354
Connect To Database @{DB_ARGS} &{DB_KWARGS}
4455
ELSE
4556
Fail Unexpected mode - ${DB_MODULE_MODE}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*** Settings ***
2+
Resource ../common.resource
3+
4+
5+
*** Keywords ***
6+
Connect Using Config File
7+
[Documentation] `File name` is only name without extension,
8+
... the path is build relative to the resource directory
9+
[Arguments] ${File name}=${None} &{Params}
10+
${Path}= Set Variable ${CURDIR}/${File name}.cfg
11+
Connect To Database dbConfigFile=${Path} &{Params}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[default]
2+
dbapiModuleName=oracledb
3+
dbName=db
4+
password=pass
5+
dbHost=127.0.0.1
6+
dbPort=1521
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[default]
2+
dbapiModuleName=oracledb
3+
dbName=db
4+
dbUsername=db_user
5+
dbPassword=pass
6+
dbHost=127.0.0.1
7+
dbPort=1521
8+
blah=blah
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[default]
2+
dbapiModuleName=oracledb
3+
dbName=db
4+
dbUsername=db_user
5+
dbPassword=pass
6+
dbHost=127.0.0.1
7+
dbPort=1521
8+
driverMode=thin
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[default]
2+
dbapiModuleName=oracledb
3+
dbName=db
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[default]
2+
dbapiModuleName=oracledb
3+
dbName=db
4+
dbUsername=db_user
5+
dbPassword=pass
6+
dbHost=127.0.0.1
7+
dbPort=1521
8+
driverMode=thick

0 commit comments

Comments
 (0)