Skip to content

Commit 26b49ef

Browse files
authored
Merge pull request #212 from MarketSquare/multi-params
Fix handling query params for MS SQL #211
2 parents 17a70a9 + c6170a3 commit 26b49ef

File tree

7 files changed

+51
-37
lines changed

7 files changed

+51
-37
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ output.xml
1212
report.html
1313
venv
1414
.runNumber
15+
.DS_Store

requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
robotframework
2-
robotframework-excellib
2+
robotframework-excellib
3+
pre-commit
4+
build
5+
twine

src/DatabaseLibrary/assertion.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +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 List, Optional
14+
from typing import Optional, Tuple
1515

1616
from robot.api import logger
1717

@@ -27,7 +27,7 @@ def check_if_exists_in_database(
2727
sansTran: bool = False,
2828
msg: Optional[str] = None,
2929
alias: Optional[str] = None,
30-
parameters: Optional[List] = None,
30+
parameters: Optional[Tuple] = None,
3131
):
3232
"""
3333
Check if any row would be returned by given the input ``selectStatement``. If there are no results, then this will
@@ -64,7 +64,7 @@ def check_if_not_exists_in_database(
6464
sansTran: bool = False,
6565
msg: Optional[str] = None,
6666
alias: Optional[str] = None,
67-
parameters: Optional[List] = None,
67+
parameters: Optional[Tuple] = None,
6868
):
6969
"""
7070
This is the negation of `check_if_exists_in_database`.
@@ -103,7 +103,7 @@ def row_count_is_0(
103103
sansTran: bool = False,
104104
msg: Optional[str] = None,
105105
alias: Optional[str] = None,
106-
parameters: Optional[List] = None,
106+
parameters: Optional[Tuple] = None,
107107
):
108108
"""
109109
Check if any rows are returned from the submitted ``selectStatement``. If there are, then this will throw an
@@ -140,7 +140,7 @@ def row_count_is_equal_to_x(
140140
sansTran: bool = False,
141141
msg: Optional[str] = None,
142142
alias: Optional[str] = None,
143-
parameters: Optional[List] = None,
143+
parameters: Optional[Tuple] = None,
144144
):
145145
"""
146146
Check if the number of rows returned from ``selectStatement`` is equal to the value submitted. If not, then this
@@ -178,7 +178,7 @@ def row_count_is_greater_than_x(
178178
sansTran: bool = False,
179179
msg: Optional[str] = None,
180180
alias: Optional[str] = None,
181-
parameters: Optional[List] = None,
181+
parameters: Optional[Tuple] = None,
182182
):
183183
"""
184184
Check if the number of rows returned from ``selectStatement`` is greater than the value submitted. If not, then
@@ -216,7 +216,7 @@ def row_count_is_less_than_x(
216216
sansTran: bool = False,
217217
msg: Optional[str] = None,
218218
alias: Optional[str] = None,
219-
parameters: Optional[List] = None,
219+
parameters: Optional[Tuple] = None,
220220
):
221221
"""
222222
Check if the number of rows returned from ``selectStatement`` is less than the value submitted. If not, then this

src/DatabaseLibrary/query.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import inspect
1616
import re
1717
import sys
18-
from typing import List, Optional
18+
from typing import List, Optional, Tuple
1919

2020
from robot.api import logger
2121

@@ -31,7 +31,7 @@ def query(
3131
sansTran: bool = False,
3232
returnAsDict: bool = False,
3333
alias: Optional[str] = None,
34-
parameters: Optional[List] = None,
34+
parameters: Optional[Tuple] = None,
3535
):
3636
"""
3737
Runs a query with the ``selectStatement`` and returns the result as a list of rows.
@@ -98,7 +98,7 @@ def row_count(
9898
selectStatement: str,
9999
sansTran: bool = False,
100100
alias: Optional[str] = None,
101-
parameters: Optional[List] = None,
101+
parameters: Optional[Tuple] = None,
102102
):
103103
"""
104104
Uses the input ``selectStatement`` to query the database and returns the number of rows from the query.
@@ -137,7 +137,7 @@ def description(
137137
selectStatement: str,
138138
sansTran: bool = False,
139139
alias: Optional[str] = None,
140-
parameters: Optional[List] = None,
140+
parameters: Optional[Tuple] = None,
141141
):
142142
"""
143143
Uses the input ``selectStatement`` to query a table in the db which will be used to determine the description.
@@ -364,7 +364,7 @@ def execute_sql_string(
364364
sqlString: str,
365365
sansTran: bool = False,
366366
alias: Optional[str] = None,
367-
parameters: Optional[List] = None,
367+
parameters: Optional[Tuple] = None,
368368
omitTrailingSemicolon: Optional[bool] = None,
369369
):
370370
"""
@@ -547,7 +547,11 @@ def call_stored_procedure(
547547
db_connection.client.rollback()
548548

549549
def __execute_sql(
550-
self, cur, sql_statement: str, omit_trailing_semicolon: Optional[bool] = None, parameters: Optional[List] = None
550+
self,
551+
cur,
552+
sql_statement: str,
553+
omit_trailing_semicolon: Optional[bool] = None,
554+
parameters: Optional[Tuple] = None,
551555
):
552556
"""
553557
Runs the `sql_statement` using `cur` as Cursor object.

src/DatabaseLibrary/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "1.4.3"
1+
VERSION = "1.4.4"

test/resources/create_stored_procedures_mssql.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ END;
3838
DROP PROCEDURE IF EXISTS check_condition;
3939
CREATE PROCEDURE check_condition
4040
AS
41-
DECLARE @v_condition BIT = 1;
41+
BEGIN
42+
DECLARE @v_condition BIT;
43+
SET @v_condition = 1;
4244
IF @v_condition = 1
4345
BEGIN
4446
PRINT 'Condition is true';
Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
*** Settings ***
2-
Documentation Keywords with query params as seperate arguments work across all databases.
2+
Documentation Keywords with query params as separate arguments work across all databases.
33
44
Resource ../../resources/common.resource
55

@@ -10,58 +10,62 @@ Test Teardown Drop Tables Person And Foobar
1010

1111

1212
*** Variables ***
13-
@{PARAMS} Franz Allan
13+
@{SINGLE_PARAM} Franz Allan
14+
@{MULTI_PARAM} Jerry Schneider
1415

1516

1617
*** Keywords ***
1718
Connect To DB And Build Query
1819
Connect To DB
19-
Build Query String With Params
20+
Build Query Strings With Params
2021

21-
Build Query String With Params
22-
${sql}= Set Variable SELECT id FROM person WHERE FIRST_NAME=
22+
Build Query Strings With Params
23+
${placeholder}= Set Variable %s
2324
IF "${DB_MODULE}" in ["oracledb", "cx_Oracle"]
24-
${sql}= Catenate ${sql} :id
25+
${placeholder}= Set Variable :id
2526
ELSE IF "${DB_MODULE}" in ["sqlite3", "pyodbc"]
26-
${sql}= Catenate ${sql} ?
27-
ELSE
28-
${sql}= Catenate ${sql} %s
27+
${placeholder}= Set Variable ?
2928
END
30-
Set Suite Variable ${QUERY} ${sql}
29+
Set Suite Variable ${QUERY_SINGLE_PARAM} SELECT id FROM person WHERE FIRST_NAME=${placeholder}
30+
Set Suite Variable ${QUERY_MULTI_PARAM} ${QUERY_SINGLE_PARAM} AND LAST_NAME=${placeholder}
3131

3232

3333
*** Test Cases ***
34-
Query
35-
${out}= Query ${QUERY} parameters=${PARAMS}
34+
Query Single Param
35+
${out}= Query ${QUERY_SINGLE_PARAM} parameters=${SINGLE_PARAM}
36+
Length Should Be ${out} 1
37+
38+
Query Multiple Params
39+
${out}= Query ${QUERY_MULTI_PARAM} parameters=${MULTI_PARAM}
3640
Length Should Be ${out} 1
3741

3842
Row Count
39-
${out}= Row Count ${QUERY} parameters=${PARAMS}
43+
${out}= Row Count ${QUERY_SINGLE_PARAM} parameters=${SINGLE_PARAM}
4044
Should Be Equal As Strings ${out} 1
4145

4246
Description
43-
${out}= Description ${QUERY} parameters=${PARAMS}
47+
${out}= Description ${QUERY_SINGLE_PARAM} parameters=${SINGLE_PARAM}
4448
Length Should Be ${out} 1
4549

4650
Execute SQL String
47-
Execute Sql String ${QUERY} parameters=${PARAMS}
51+
Execute Sql String ${QUERY_SINGLE_PARAM} parameters=${SINGLE_PARAM}
4852

4953
Check If Exists In DB
50-
Check If Exists In Database ${QUERY} parameters=${PARAMS}
54+
Check If Exists In Database ${QUERY_SINGLE_PARAM} parameters=${SINGLE_PARAM}
5155

5256
Check If Not Exists In DB
5357
@{Wrong params}= Create List Joe
54-
Check If Not Exists In Database ${QUERY} parameters=${Wrong params}
58+
Check If Not Exists In Database ${QUERY_SINGLE_PARAM} parameters=${Wrong params}
5559

5660
Row Count is 0
5761
@{Wrong params}= Create List Joe
58-
Row Count is 0 ${QUERY} parameters=${Wrong params}
62+
Row Count is 0 ${QUERY_SINGLE_PARAM} parameters=${Wrong params}
5963

6064
Row Count is Equal to X
61-
Row Count is Equal to X ${QUERY} 1 parameters=${PARAMS}
65+
Row Count is Equal to X ${QUERY_SINGLE_PARAM} 1 parameters=${SINGLE_PARAM}
6266

6367
Row Count is Less Than X
64-
Row Count is Less Than X ${QUERY} 5 parameters=${PARAMS}
68+
Row Count is Less Than X ${QUERY_SINGLE_PARAM} 5 parameters=${SINGLE_PARAM}
6569

6670
Row Count is Greater Than X
67-
Row Count is Greater Than X ${QUERY} 0 parameters=${PARAMS}
71+
Row Count is Greater Than X ${QUERY_SINGLE_PARAM} 0 parameters=${SINGLE_PARAM}

0 commit comments

Comments
 (0)