Skip to content

Commit 10fe9e2

Browse files
authored
Merge pull request #192 from MarketSquare/add_typing
Add type hints
2 parents 2286aed + ce260dd commit 10fe9e2

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

src/DatabaseLibrary/assertion.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from typing import Optional
1415

1516
from robot.api import logger
1617

@@ -20,7 +21,7 @@ class Assertion:
2021
Assertion handles all the assertions of Database Library.
2122
"""
2223

23-
def check_if_exists_in_database(self, selectStatement, sansTran=False, msg=None):
24+
def check_if_exists_in_database(self, selectStatement: str, sansTran: bool = False, msg: Optional[str] = None):
2425
"""
2526
Check if any row would be returned by given the input `selectStatement`. If there are no results, then this will
2627
throw an AssertionError. Set optional input `sansTran` to True to run command without an explicit transaction
@@ -50,7 +51,7 @@ def check_if_exists_in_database(self, selectStatement, sansTran=False, msg=None)
5051
msg or f"Expected to have have at least one row, " f"but got 0 rows from: '{selectStatement}'"
5152
)
5253

53-
def check_if_not_exists_in_database(self, selectStatement, sansTran=False, msg=None):
54+
def check_if_not_exists_in_database(self, selectStatement: str, sansTran: bool = False, msg: Optional[str] = None):
5455
"""
5556
This is the negation of `check_if_exists_in_database`.
5657
@@ -83,7 +84,7 @@ def check_if_not_exists_in_database(self, selectStatement, sansTran=False, msg=N
8384
msg or f"Expected to have have no rows from '{selectStatement}', but got some rows: {query_results}"
8485
)
8586

86-
def row_count_is_0(self, selectStatement, sansTran=False, msg=None):
87+
def row_count_is_0(self, selectStatement: str, sansTran: bool = False, msg: Optional[str] = None):
8788
"""
8889
Check if any rows are returned from the submitted `selectStatement`. If there are, then this will throw an
8990
AssertionError. Set optional input `sansTran` to True to run command without an explicit transaction commit or
@@ -112,7 +113,9 @@ def row_count_is_0(self, selectStatement, sansTran=False, msg=None):
112113
if num_rows > 0:
113114
raise AssertionError(msg or f"Expected 0 rows, but {num_rows} were returned from: '{selectStatement}'")
114115

115-
def row_count_is_equal_to_x(self, selectStatement, numRows, sansTran=False, msg=None):
116+
def row_count_is_equal_to_x(
117+
self, selectStatement: str, numRows: str, sansTran: bool = False, msg: Optional[str] = None
118+
):
116119
"""
117120
Check if the number of rows returned from `selectStatement` is equal to the value submitted. If not, then this
118121
will throw an AssertionError. Set optional input `sansTran` to True to run command without an explicit
@@ -144,7 +147,9 @@ def row_count_is_equal_to_x(self, selectStatement, numRows, sansTran=False, msg=
144147
msg or f"Expected {numRows} rows, but {num_rows} were returned from: '{selectStatement}'"
145148
)
146149

147-
def row_count_is_greater_than_x(self, selectStatement, numRows, sansTran=False, msg=None):
150+
def row_count_is_greater_than_x(
151+
self, selectStatement: str, numRows: str, sansTran: bool = False, msg: Optional[str] = None
152+
):
148153
"""
149154
Check if the number of rows returned from `selectStatement` is greater than the value submitted. If not, then
150155
this will throw an AssertionError. Set optional input `sansTran` to True to run command without an explicit
@@ -176,7 +181,9 @@ def row_count_is_greater_than_x(self, selectStatement, numRows, sansTran=False,
176181
msg or f"Expected more than {numRows} rows, but {num_rows} were returned from '{selectStatement}'"
177182
)
178183

179-
def row_count_is_less_than_x(self, selectStatement, numRows, sansTran=False, msg=None):
184+
def row_count_is_less_than_x(
185+
self, selectStatement: str, numRows: str, sansTran: bool = False, msg: Optional[str] = None
186+
):
180187
"""
181188
Check if the number of rows returned from `selectStatement` is less than the value submitted. If not, then this
182189
will throw an AssertionError. Set optional input `sansTran` to True to run command without an explicit
@@ -208,7 +215,7 @@ def row_count_is_less_than_x(self, selectStatement, numRows, sansTran=False, msg
208215
msg or f"Expected less than {numRows} rows, but {num_rows} were returned from '{selectStatement}'"
209216
)
210217

211-
def table_must_exist(self, tableName, sansTran=False, msg=None):
218+
def table_must_exist(self, tableName: str, sansTran: bool = False, msg: Optional[str] = None):
212219
"""
213220
Check if the table given exists in the database. Set optional input `sansTran` to True to run command without an
214221
explicit transaction commit or rollback. The default error message can be overridden with the `msg` argument.

src/DatabaseLibrary/connection_manager.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import importlib
16+
from typing import Optional
1617

1718
try:
1819
import ConfigParser
@@ -37,15 +38,15 @@ def __init__(self):
3738

3839
def connect_to_database(
3940
self,
40-
dbapiModuleName=None,
41-
dbName=None,
42-
dbUsername=None,
43-
dbPassword=None,
44-
dbHost=None,
45-
dbPort=None,
46-
dbCharset=None,
47-
dbDriver=None,
48-
dbConfigFile=None,
41+
dbapiModuleName: Optional[str] = None,
42+
dbName: Optional[str] = None,
43+
dbUsername: Optional[str] = None,
44+
dbPassword: Optional[str] = None,
45+
dbHost: Optional[str] = None,
46+
dbPort: Optional[int] = None,
47+
dbCharset: Optional[str] = None,
48+
dbDriver: Optional[str] = None,
49+
dbConfigFile: Optional[str] = None,
4950
):
5051
"""
5152
Loads the DB API 2.0 module given `dbapiModuleName` then uses it to
@@ -234,7 +235,9 @@ def connect_to_database(
234235
port=dbPort,
235236
)
236237

237-
def connect_to_database_using_custom_params(self, dbapiModuleName=None, db_connect_string=""):
238+
def connect_to_database_using_custom_params(
239+
self, dbapiModuleName: Optional[str] = None, db_connect_string: str = ""
240+
):
238241
"""
239242
Loads the DB API 2.0 module given `dbapiModuleName` then uses it to
240243
connect to the database using the map string `db_connect_string`
@@ -269,7 +272,9 @@ def connect_to_database_using_custom_params(self, dbapiModuleName=None, db_conne
269272

270273
self._dbconnection = eval(db_connect_string)
271274

272-
def connect_to_database_using_custom_connection_string(self, dbapiModuleName=None, db_connect_string=""):
275+
def connect_to_database_using_custom_connection_string(
276+
self, dbapiModuleName: Optional[str] = None, db_connect_string: str = ""
277+
):
273278
"""
274279
Loads the DB API 2.0 module given `dbapiModuleName` then uses it to
275280
connect to the database using the `db_connect_string`
@@ -290,7 +295,7 @@ def connect_to_database_using_custom_connection_string(self, dbapiModuleName=Non
290295
)
291296
self._dbconnection = db_api_2.connect(db_connect_string)
292297

293-
def disconnect_from_database(self, error_if_no_connection=False):
298+
def disconnect_from_database(self, error_if_no_connection: bool = False):
294299
"""
295300
Disconnects from the database.
296301
@@ -311,7 +316,7 @@ def disconnect_from_database(self, error_if_no_connection=False):
311316
self._dbconnection.close()
312317
self._dbconnection = None
313318

314-
def set_auto_commit(self, autoCommit=True):
319+
def set_auto_commit(self, autoCommit: bool = True):
315320
"""
316321
Turn the autocommit on the database connection ON or OFF.
317322

src/DatabaseLibrary/query.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import inspect
1616
import sys
17+
from typing import List, Optional
1718

1819
from robot.api import logger
1920

@@ -23,7 +24,7 @@ class Query:
2324
Query handles all the querying done by the Database Library.
2425
"""
2526

26-
def query(self, selectStatement, sansTran=False, returnAsDict=False):
27+
def query(self, selectStatement: str, sansTran: bool = False, returnAsDict: bool = False):
2728
"""
2829
Uses the input `selectStatement` to query for the values that will be returned as a list of tuples. Set optional
2930
input `sansTran` to True to run command without an explicit transaction commit or rollback.
@@ -71,7 +72,7 @@ def query(self, selectStatement, sansTran=False, returnAsDict=False):
7172
if cur and not sansTran:
7273
self._dbconnection.rollback()
7374

74-
def row_count(self, selectStatement, sansTran=False):
75+
def row_count(self, selectStatement: str, sansTran: bool = False):
7576
"""
7677
Uses the input `selectStatement` to query the database and returns the number of rows from the query. Set
7778
optional input `sansTran` to True to run command without an explicit transaction commit or rollback.
@@ -111,7 +112,7 @@ def row_count(self, selectStatement, sansTran=False):
111112
if cur and not sansTran:
112113
self._dbconnection.rollback()
113114

114-
def description(self, selectStatement, sansTran=False):
115+
def description(self, selectStatement: str, sansTran: bool = False):
115116
"""
116117
Uses the input `selectStatement` to query a table in the db which will be used to determine the description. Set
117118
optional input `sansTran` to True to run command without an explicit transaction commit or rollback.
@@ -146,7 +147,7 @@ def description(self, selectStatement, sansTran=False):
146147
if cur and not sansTran:
147148
self._dbconnection.rollback()
148149

149-
def delete_all_rows_from_table(self, tableName, sansTran=False):
150+
def delete_all_rows_from_table(self, tableName: str, sansTran: bool = False):
150151
"""
151152
Delete all the rows within a given table. Set optional input `sansTran` to True to run command without an
152153
explicit transaction commit or rollback.
@@ -181,7 +182,7 @@ def delete_all_rows_from_table(self, tableName, sansTran=False):
181182
if cur and not sansTran:
182183
self._dbconnection.rollback()
183184

184-
def execute_sql_script(self, sqlScriptFileName, sansTran=False):
185+
def execute_sql_script(self, sqlScriptFileName: str, sansTran: bool = False):
185186
"""
186187
Executes the content of the `sqlScriptFileName` as SQL commands. Useful for setting the database to a known
187188
state before running your tests, or clearing out your test data after running each a test. Set optional input
@@ -304,7 +305,7 @@ def execute_sql_script(self, sqlScriptFileName, sansTran=False):
304305
if cur and not sansTran:
305306
self._dbconnection.rollback()
306307

307-
def execute_sql_string(self, sqlString, sansTran=False):
308+
def execute_sql_string(self, sqlString: str, sansTran: bool = False):
308309
"""
309310
Executes the sqlString as SQL commands. Useful to pass arguments to your sql. Set optional input `sansTran` to
310311
True to run command without an explicit transaction commit or rollback.
@@ -331,7 +332,7 @@ def execute_sql_string(self, sqlString, sansTran=False):
331332
if cur and not sansTran:
332333
self._dbconnection.rollback()
333334

334-
def call_stored_procedure(self, spName, spParams=None, sansTran=False):
335+
def call_stored_procedure(self, spName: str, spParams: Optional[List[str]] = None, sansTran: bool = False):
335336
"""
336337
Calls a stored procedure `spName` with the `spParams` - a *list* of parameters the procedure requires.
337338
Use the special *CURSOR* value for OUT params, which should receive result sets -
@@ -469,7 +470,7 @@ def call_stored_procedure(self, spName, spParams=None, sansTran=False):
469470
if cur and not sansTran:
470471
self._dbconnection.rollback()
471472

472-
def __execute_sql(self, cur, sql_statement, omit_trailing_semicolon=None):
473+
def __execute_sql(self, cur, sql_statement: str, omit_trailing_semicolon: Optional[bool] = None):
473474
"""
474475
Runs the `sql_statement` using `cur` as Cursor object.
475476
Use `omit_trailing_semicolon` parameter (bool) for explicit instruction,

0 commit comments

Comments
 (0)