Skip to content

Commit 9c99cd6

Browse files
authored
Merge pull request #193 from MarketSquare/oracle-mode-switch
Oracle mode switch
2 parents 10fe9e2 + 4fe39e1 commit 9c99cd6

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Therefore there are some modules, which are "natively" supported in the library
5252
## Python modules currently "natively" supported
5353
### Oracle
5454
- [oracledb](https://oracle.github.io/python-oracledb/)
55+
- Both thick and thin client modes are supported - you can select one using the `driverMode` parameter.
56+
- However, due to current limitations of the oracledb module, **it's not possible to switch between thick and thin modes during a test execution session** - even in different suites.
5557
- [cx_Oracle](https://oracle.github.io/python-cx_Oracle/)
5658
### MySQL
5759
- [pymysql](https://github.com/PyMySQL/PyMySQL)

src/DatabaseLibrary/connection_manager.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,18 @@ def connect_to_database(
4747
dbCharset: Optional[str] = None,
4848
dbDriver: Optional[str] = None,
4949
dbConfigFile: Optional[str] = None,
50+
driverMode: Optional[str] = None,
5051
):
5152
"""
5253
Loads the DB API 2.0 module given `dbapiModuleName` then uses it to
5354
connect to the database using `dbName`, `dbUsername`, and `dbPassword`.
5455
56+
The `driverMode` is used to select the *oracledb* client mode.
57+
Allowed values are:
58+
- _thin_ (default if omitted)
59+
- _thick_
60+
- _thick,lib_dir=<PATH_TO_ORACLE_CLIENT>_
61+
5562
Optionally, you can specify a `dbConfigFile` wherein it will load the
5663
default property values for `dbapiModuleName`, `dbName` `dbUsername`
5764
and `dbPassword` (note: specifying `dbapiModuleName`, `dbName`
@@ -186,12 +193,33 @@ def connect_to_database(
186193
self.omit_trailing_semicolon = True
187194
elif dbapiModuleName in ["oracledb"]:
188195
dbPort = dbPort or 1521
196+
driverMode = driverMode or "thin"
189197
oracle_connection_params = db_api_2.ConnectParams(host=dbHost, port=dbPort, service_name=dbName)
198+
if "thick" in driverMode.lower():
199+
logger.info("Using thick Oracle client mode")
200+
mode_param = driverMode.lower().split(",lib_dir=")
201+
if len(mode_param) == 2 and mode_param[0].lower() == "thick":
202+
lib_dir = mode_param[1]
203+
logger.info(f"Oracle client lib dir specified: {lib_dir}")
204+
db_api_2.init_oracle_client(lib_dir=lib_dir)
205+
else:
206+
logger.info("No Oracle client lib dir specified, oracledb will search it in usual places")
207+
db_api_2.init_oracle_client()
208+
oracle_thin_mode = False
209+
elif "thin" in driverMode.lower():
210+
oracle_thin_mode = True
211+
logger.info("Using thin Oracle client mode")
212+
else:
213+
raise ValueError(f"Invalid Oracle client mode provided: {driverMode}")
190214
logger.info(
191215
f"Connecting using: {dbapiModuleName}.connect("
192216
f"user={dbUsername}, password=***, params={oracle_connection_params})"
193217
)
194218
self._dbconnection = db_api_2.connect(user=dbUsername, password=dbPassword, params=oracle_connection_params)
219+
assert self._dbconnection.thin == oracle_thin_mode, (
220+
"Expected oracledb to run in thin mode: {oracle_thin_mode}, "
221+
f"but the connection has thin mode: {self._dbconnection.thin}"
222+
)
195223
self.omit_trailing_semicolon = True
196224
elif dbapiModuleName in ["teradata"]:
197225
dbPort = dbPort or 1025
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
*** Settings ***
2+
Documentation Tests of switching between thin and thick mode of oracledb client.
3+
... Require the oracle client libraries installed.
4+
... See more here: https://python-oracledb.readthedocs.io/en/latest/user_guide/initialization.html#initialization
5+
...
6+
... Due to current limitations of the oracledb module it's not possible to switch between thick and thin modes
7+
... during a test execution session - even in different suites.
8+
... So theses tests should be run separated only.
9+
10+
Resource ../../resources/common.resource
11+
Test Teardown Drop Tables And Disconnect
12+
13+
14+
*** Variables ***
15+
${DB_MODULE} oracledb
16+
${DB_HOST} 127.0.0.1
17+
${DB_PORT} 1521
18+
${DB_PASS} pass
19+
${DB_USER} db_user
20+
${DB_NAME} db
21+
${ORACLE_LIB_DIR} ${EMPTY}
22+
23+
24+
*** Test Cases ***
25+
Thick Mode Without Client Dir Specified
26+
[Documentation] No client dir --> oracledb will search it in usual places
27+
Connect And Run Simple Query driverMode=thick
28+
29+
Thick Mode With Client Dir Specified
30+
[Documentation] Client dir specified --> oracledb will search it in this place
31+
Connect And Run Simple Query driverMode=thick,lib_dir=${ORACLE_LIB_DIR}
32+
33+
Thin Mode - Default
34+
[Documentation] No mode specified --> thin mode is used
35+
Connect And Run Simple Query
36+
37+
Thin Mode Explicitely Specified
38+
[Documentation] Thin mode specified --> thin mode is used
39+
Connect And Run Simple Query driverMode=thin
40+
41+
Wrong Mode
42+
[Documentation] Wrong mode --> proper error message from the library
43+
Run Keyword And Expect Error ValueError: Invalid Oracle client mode provided: wrong
44+
... Connect And Run Simple Query driverMode=wrong
45+
46+
47+
*** Keywords ***
48+
Connect And Run Simple Query
49+
[Documentation] Connect using usual params and client mode if provided
50+
[Arguments] &{Extra params}
51+
Connect To Database
52+
... ${DB_MODULE}
53+
... ${DB_NAME}
54+
... ${DB_USER}
55+
... ${DB_PASS}
56+
... ${DB_HOST}
57+
... ${DB_PORT}
58+
... &{Extra params}
59+
Create Person Table
60+
Query SELECT * FROM person
61+
62+
Drop Tables And Disconnect
63+
[Documentation] Clean data and disconnect
64+
Drop Tables Person And Foobar
65+
Disconnect From Database

0 commit comments

Comments
 (0)