Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling query params for MS SQL #211 #212

Merged
merged 9 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ output.xml
report.html
venv
.runNumber
.DS_Store
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
robotframework
robotframework-excellib
robotframework-excellib
pre-commit
build
twine
14 changes: 7 additions & 7 deletions src/DatabaseLibrary/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List, Optional
from typing import Optional, Tuple

from robot.api import logger

Expand All @@ -27,7 +27,7 @@ def check_if_exists_in_database(
sansTran: bool = False,
msg: Optional[str] = None,
alias: Optional[str] = None,
parameters: Optional[List] = None,
parameters: Optional[Tuple] = None,
):
"""
Check if any row would be returned by given the input ``selectStatement``. If there are no results, then this will
Expand Down Expand Up @@ -64,7 +64,7 @@ def check_if_not_exists_in_database(
sansTran: bool = False,
msg: Optional[str] = None,
alias: Optional[str] = None,
parameters: Optional[List] = None,
parameters: Optional[Tuple] = None,
):
"""
This is the negation of `check_if_exists_in_database`.
Expand Down Expand Up @@ -103,7 +103,7 @@ def row_count_is_0(
sansTran: bool = False,
msg: Optional[str] = None,
alias: Optional[str] = None,
parameters: Optional[List] = None,
parameters: Optional[Tuple] = None,
):
"""
Check if any rows are returned from the submitted ``selectStatement``. If there are, then this will throw an
Expand Down Expand Up @@ -140,7 +140,7 @@ def row_count_is_equal_to_x(
sansTran: bool = False,
msg: Optional[str] = None,
alias: Optional[str] = None,
parameters: Optional[List] = None,
parameters: Optional[Tuple] = None,
):
"""
Check if the number of rows returned from ``selectStatement`` is equal to the value submitted. If not, then this
Expand Down Expand Up @@ -178,7 +178,7 @@ def row_count_is_greater_than_x(
sansTran: bool = False,
msg: Optional[str] = None,
alias: Optional[str] = None,
parameters: Optional[List] = None,
parameters: Optional[Tuple] = None,
):
"""
Check if the number of rows returned from ``selectStatement`` is greater than the value submitted. If not, then
Expand Down Expand Up @@ -216,7 +216,7 @@ def row_count_is_less_than_x(
sansTran: bool = False,
msg: Optional[str] = None,
alias: Optional[str] = None,
parameters: Optional[List] = None,
parameters: Optional[Tuple] = None,
):
"""
Check if the number of rows returned from ``selectStatement`` is less than the value submitted. If not, then this
Expand Down
16 changes: 10 additions & 6 deletions src/DatabaseLibrary/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import inspect
import re
import sys
from typing import List, Optional
from typing import List, Optional, Tuple

from robot.api import logger

Expand All @@ -31,7 +31,7 @@ def query(
sansTran: bool = False,
returnAsDict: bool = False,
alias: Optional[str] = None,
parameters: Optional[List] = None,
parameters: Optional[Tuple] = None,
):
"""
Runs a query with the ``selectStatement`` and returns the result as a list of rows.
Expand Down Expand Up @@ -98,7 +98,7 @@ def row_count(
selectStatement: str,
sansTran: bool = False,
alias: Optional[str] = None,
parameters: Optional[List] = None,
parameters: Optional[Tuple] = None,
):
"""
Uses the input ``selectStatement`` to query the database and returns the number of rows from the query.
Expand Down Expand Up @@ -137,7 +137,7 @@ def description(
selectStatement: str,
sansTran: bool = False,
alias: Optional[str] = None,
parameters: Optional[List] = None,
parameters: Optional[Tuple] = None,
):
"""
Uses the input ``selectStatement`` to query a table in the db which will be used to determine the description.
Expand Down Expand Up @@ -364,7 +364,7 @@ def execute_sql_string(
sqlString: str,
sansTran: bool = False,
alias: Optional[str] = None,
parameters: Optional[List] = None,
parameters: Optional[Tuple] = None,
omitTrailingSemicolon: Optional[bool] = None,
):
"""
Expand Down Expand Up @@ -547,7 +547,11 @@ def call_stored_procedure(
db_connection.client.rollback()

def __execute_sql(
self, cur, sql_statement: str, omit_trailing_semicolon: Optional[bool] = None, parameters: Optional[List] = None
self,
cur,
sql_statement: str,
omit_trailing_semicolon: Optional[bool] = None,
parameters: Optional[Tuple] = None,
):
"""
Runs the `sql_statement` using `cur` as Cursor object.
Expand Down
2 changes: 1 addition & 1 deletion src/DatabaseLibrary/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "1.4.3"
VERSION = "1.4.4"
4 changes: 3 additions & 1 deletion test/resources/create_stored_procedures_mssql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ END;
DROP PROCEDURE IF EXISTS check_condition;
CREATE PROCEDURE check_condition
AS
DECLARE @v_condition BIT = 1;
BEGIN
DECLARE @v_condition BIT;
SET @v_condition = 1;
IF @v_condition = 1
BEGIN
PRINT 'Condition is true';
Expand Down
46 changes: 25 additions & 21 deletions test/tests/common_tests/query_params.robot
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
*** Settings ***
Documentation Keywords with query params as seperate arguments work across all databases.
Documentation Keywords with query params as separate arguments work across all databases.

Resource ../../resources/common.resource

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


*** Variables ***
@{PARAMS} Franz Allan
@{SINGLE_PARAM} Franz Allan
@{MULTI_PARAM} Jerry Schneider


*** Keywords ***
Connect To DB And Build Query
Connect To DB
Build Query String With Params
Build Query Strings With Params

Build Query String With Params
${sql}= Set Variable SELECT id FROM person WHERE FIRST_NAME=
Build Query Strings With Params
${placeholder}= Set Variable %s
IF "${DB_MODULE}" in ["oracledb", "cx_Oracle"]
${sql}= Catenate ${sql} :id
${placeholder}= Set Variable :id
ELSE IF "${DB_MODULE}" in ["sqlite3", "pyodbc"]
${sql}= Catenate ${sql} ?
ELSE
${sql}= Catenate ${sql} %s
${placeholder}= Set Variable ?
END
Set Suite Variable ${QUERY} ${sql}
Set Suite Variable ${QUERY_SINGLE_PARAM} SELECT id FROM person WHERE FIRST_NAME=${placeholder}
Set Suite Variable ${QUERY_MULTI_PARAM} ${QUERY_SINGLE_PARAM} AND LAST_NAME=${placeholder}


*** Test Cases ***
Query
${out}= Query ${QUERY} parameters=${PARAMS}
Query Single Param
${out}= Query ${QUERY_SINGLE_PARAM} parameters=${SINGLE_PARAM}
Length Should Be ${out} 1

Query Multiple Params
${out}= Query ${QUERY_MULTI_PARAM} parameters=${MULTI_PARAM}
Length Should Be ${out} 1

Row Count
${out}= Row Count ${QUERY} parameters=${PARAMS}
${out}= Row Count ${QUERY_SINGLE_PARAM} parameters=${SINGLE_PARAM}
Should Be Equal As Strings ${out} 1

Description
${out}= Description ${QUERY} parameters=${PARAMS}
${out}= Description ${QUERY_SINGLE_PARAM} parameters=${SINGLE_PARAM}
Length Should Be ${out} 1

Execute SQL String
Execute Sql String ${QUERY} parameters=${PARAMS}
Execute Sql String ${QUERY_SINGLE_PARAM} parameters=${SINGLE_PARAM}

Check If Exists In DB
Check If Exists In Database ${QUERY} parameters=${PARAMS}
Check If Exists In Database ${QUERY_SINGLE_PARAM} parameters=${SINGLE_PARAM}

Check If Not Exists In DB
@{Wrong params}= Create List Joe
Check If Not Exists In Database ${QUERY} parameters=${Wrong params}
Check If Not Exists In Database ${QUERY_SINGLE_PARAM} parameters=${Wrong params}

Row Count is 0
@{Wrong params}= Create List Joe
Row Count is 0 ${QUERY} parameters=${Wrong params}
Row Count is 0 ${QUERY_SINGLE_PARAM} parameters=${Wrong params}

Row Count is Equal to X
Row Count is Equal to X ${QUERY} 1 parameters=${PARAMS}
Row Count is Equal to X ${QUERY_SINGLE_PARAM} 1 parameters=${SINGLE_PARAM}

Row Count is Less Than X
Row Count is Less Than X ${QUERY} 5 parameters=${PARAMS}
Row Count is Less Than X ${QUERY_SINGLE_PARAM} 5 parameters=${SINGLE_PARAM}

Row Count is Greater Than X
Row Count is Greater Than X ${QUERY} 0 parameters=${PARAMS}
Row Count is Greater Than X ${QUERY_SINGLE_PARAM} 0 parameters=${SINGLE_PARAM}
Loading