Skip to content

Commit 766e803

Browse files
committed
New 'assert_as_string' parameter in the 'Check Query Result' keyword
1 parent fc841cd commit 766e803

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

src/DatabaseLibrary/assertion.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ def check_query_result(
350350
retry_timeout="0 seconds",
351351
retry_pause="0.5 seconds",
352352
*,
353+
assert_as_string=False,
353354
replace_robot_variables=False,
354355
selectStatement: Optional[str] = None,
355356
sansTran: Optional[bool] = None,
@@ -359,9 +360,11 @@ def check_query_result(
359360
The value position in results can be adjusted using ``row`` and ``col`` parameters (0-based).
360361
See `Inline assertions` for more details.
361362
362-
*The assertion in this keyword is type sensitive!*
363-
The ``expected_value`` is taken as a string, no argument conversion is performed.
364-
Use RF syntax like ``${1}`` for numeric values.
363+
=== Assertions are type sensitive! ===
364+
Normally, the type of ``expected_value`` is taken as provided (string as RF default or e.g. ``${1}`` for numeric values)
365+
and the type of *actual value* is taken as returned by the ``select_statement``
366+
(depends on the DB table and the Python module).
367+
Set ``assert_as_string`` to _True_ to convert both *actual value* and ``expected_value`` to string before running the assertion.
365368
366369
Use optional ``assertion_message`` to override the default error message.
367370
@@ -397,15 +400,32 @@ def check_query_result(
397400
"""
398401
check_ok = False
399402
time_counter = 0
403+
404+
use_string_assertion_hint = "Consider using the 'assert_as_string' parameter."
405+
406+
def _log_possible_type_mismatch(expected, actual, suggest_string_assertion=True):
407+
if actual is not None:
408+
msg = (
409+
f"Possible type mismatch between expected value '{expected}' ({type(expected).__name__}) "
410+
f"and actual value returned by the sql statement '{actual}' ({type(actual).__name__})."
411+
)
412+
if suggest_string_assertion:
413+
msg += f"\n{use_string_assertion_hint}"
414+
if type(expected) != type(actual):
415+
logger.info(msg)
416+
400417
while not check_ok:
401418
try:
419+
actual_value = None
420+
402421
query_results = self.query(
403422
select_statement,
404423
no_transaction=no_transaction,
405424
alias=alias,
406425
parameters=parameters,
407426
replace_robot_variables=replace_robot_variables,
408427
)
428+
409429
row_count = len(query_results)
410430
assert (
411431
row < row_count
@@ -415,13 +435,33 @@ def check_query_result(
415435
col < col_count
416436
), f"Checking column '{col}' is not possible, as query results contain {col_count} columns only!"
417437
actual_value = query_results[row][col]
438+
if assert_as_string:
439+
actual_value = str(actual_value)
440+
expected_value = str(expected_value)
441+
442+
assert_log_msg = (
443+
f"'{actual_value}' ({type(actual_value).__name__}) "
444+
f"{assertion_operator.name} '{expected_value}' ({type(expected_value).__name__})"
445+
)
446+
logger.info(f"Run assertion: {assert_log_msg}")
447+
418448
verify_assertion(
419-
actual_value, assertion_operator, expected_value, "Wrong query result:", assertion_message
449+
actual_value,
450+
assertion_operator,
451+
expected_value,
452+
"Wrong query result:",
453+
assertion_message,
420454
)
421455
check_ok = True
456+
except TypeError as e:
457+
_log_possible_type_mismatch(expected_value, actual_value, suggest_string_assertion=False)
458+
msg = f"Invalid assertion: {assert_log_msg}.\n{use_string_assertion_hint}\n"
459+
raise TypeError(f"{msg}Original error: {e}") from e
460+
422461
except AssertionError as e:
423462
if time_counter >= timestr_to_secs(retry_timeout):
424463
logger.info(f"Timeout '{retry_timeout}' reached")
464+
_log_possible_type_mismatch(expected_value, actual_value)
425465
raise e
426466
BuiltIn().sleep(retry_pause)
427467
time_counter += timestr_to_secs(retry_pause)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
*** Settings ***
2+
Documentation Simulate data type mismatch, check error messages and try converting to strings
3+
4+
Resource ../../resources/common.resource
5+
6+
Suite Setup Connect To DB
7+
Suite Teardown Disconnect From Database
8+
Test Setup Create Person Table And Insert Data
9+
Test Teardown Drop Tables Person And Foobar
10+
11+
12+
*** Variables ***
13+
${sql} SELECT id FROM person WHERE first_name = 'Franz Allan'
14+
15+
16+
*** Test Cases ***
17+
Int Contains String - Type Error
18+
Run Keyword And Expect Error TypeError: Invalid assertion*
19+
... Check Query Result ${sql} contains 1
20+
21+
Assert As String - Int Contains String
22+
Check Query Result ${sql} contains 1 assert_as_string=True
23+
24+
Int Equals String - Not Equals Error Message
25+
Run Keyword And Expect Error Wrong query result*
26+
... Check Query Result ${sql} == 1
27+
28+
Assert As String - Int Equals String
29+
Check Query Result ${sql} == 1 assert_as_string=True
30+
31+
Int > String - Type Error
32+
Run Keyword And Expect Error TypeError: Invalid assertion*
33+
... Check Query Result ${sql} > 1
34+
35+
Int > String - Wrong Result Error
36+
Run Keyword And Expect Error Wrong query result*
37+
... Check Query Result ${sql} > 1 assert_as_string=True
38+
39+
Int Equals Int - Not Equals Error Message
40+
Run Keyword And Expect Error Wrong query result*
41+
... Check Query Result ${sql} == ${2}
42+
43+
Assert As String - Int Equals Int - Not Equals Error Message
44+
Run Keyword And Expect Error Wrong query result*
45+
... Check Query Result ${sql} == ${2} assert_as_string=True
46+
47+
Int Contains Int - Type Error
48+
Run Keyword And Expect Error TypeError: Invalid assertion*
49+
... Check Query Result ${sql} contains ${1}
50+
51+
Assert As String - Int Contains Int
52+
Check Query Result ${sql} contains ${1} assert_as_string=True
53+

0 commit comments

Comments
 (0)