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 , Tuple
14+ from typing import Any , Optional , Tuple
1515
16+ from assertionengine import AssertionOperator , verify_assertion
1617from robot .api import logger
1718
1819
@@ -30,6 +31,9 @@ def check_if_exists_in_database(
3031 parameters : Optional [Tuple ] = None ,
3132 ):
3233 """
34+ *DEPRECATED* Use new `Check Row Count` keyword with assertion engine instead.
35+ The deprecated keyword will be removed in future versions.
36+
3337 Check if any row would be returned by given the input ``selectStatement``. If there are no results, then this will
3438 throw an AssertionError.
3539
@@ -67,6 +71,9 @@ def check_if_not_exists_in_database(
6771 parameters : Optional [Tuple ] = None ,
6872 ):
6973 """
74+ *DEPRECATED* Use new `Check Row Count` keyword with assertion engine instead.
75+ The deprecated keyword will be removed in future versions.
76+
7077 This is the negation of `check_if_exists_in_database`.
7178
7279 Check if no rows would be returned by given the input ``selectStatement``. If there are any results, then this
@@ -106,6 +113,9 @@ def row_count_is_0(
106113 parameters : Optional [Tuple ] = None ,
107114 ):
108115 """
116+ *DEPRECATED* Use new `Check Row Count` keyword with assertion engine instead.
117+ The deprecated keyword will be removed in future versions.
118+
109119 Check if any rows are returned from the submitted ``selectStatement``. If there are, then this will throw an
110120 AssertionError.
111121
@@ -143,6 +153,9 @@ def row_count_is_equal_to_x(
143153 parameters : Optional [Tuple ] = None ,
144154 ):
145155 """
156+ *DEPRECATED* Use new `Check Row Count` keyword with assertion engine instead.
157+ The deprecated keyword will be removed in future versions.
158+
146159 Check if the number of rows returned from ``selectStatement`` is equal to the value submitted. If not, then this
147160 will throw an AssertionError.
148161
@@ -181,6 +194,9 @@ def row_count_is_greater_than_x(
181194 parameters : Optional [Tuple ] = None ,
182195 ):
183196 """
197+ *DEPRECATED* Use new `Check Row Count` keyword with assertion engine instead.
198+ The deprecated keyword will be removed in future versions.
199+
184200 Check if the number of rows returned from ``selectStatement`` is greater than the value submitted. If not, then
185201 this will throw an AssertionError.
186202
@@ -219,6 +235,9 @@ def row_count_is_less_than_x(
219235 parameters : Optional [Tuple ] = None ,
220236 ):
221237 """
238+ *DEPRECATED* Use new `Check Row Count` keyword with assertion engine instead.
239+ The deprecated keyword will be removed in future versions.
240+
222241 Check if the number of rows returned from ``selectStatement`` is less than the value submitted. If not, then this
223242 will throw an AssertionError.
224243
@@ -247,6 +266,101 @@ def row_count_is_less_than_x(
247266 msg or f"Expected less than { numRows } rows, but { num_rows } were returned from '{ selectStatement } '"
248267 )
249268
269+ def check_row_count (
270+ self ,
271+ selectStatement : str ,
272+ assertion_operator : AssertionOperator ,
273+ expected_value : int ,
274+ assertion_message : Optional [str ] = None ,
275+ sansTran : bool = False ,
276+ alias : Optional [str ] = None ,
277+ parameters : Optional [Tuple ] = None ,
278+ ):
279+ """
280+ Check the number of rows returned from ``selectStatement`` using ``assertion_operator``
281+ and ``expected_value``. See `Inline assertions` for more details.
282+
283+ Use optional ``assertion_message`` to override the default error message.
284+
285+ Set optional input ``sansTran`` to _True_ to run command without an explicit transaction commit or rollback.
286+
287+ Use optional ``alias`` parameter to specify what connection should be used for the query if you have more
288+ than one connection open.
289+
290+ Use optional ``parameters`` for query variable substitution (variable substitution syntax may be different
291+ depending on the database client).
292+
293+ Examples:
294+ | Check Row Count | SELECT id FROM person WHERE first_name = 'John' | *==* | 1 |
295+ | Check Row Count | SELECT id FROM person WHERE first_name = 'John' | *>=* | 2 | assertion_message=my error message |
296+ | Check Row Count | SELECT id FROM person WHERE first_name = 'John' | *inequal* | 3 | alias=my_alias |
297+ | Check Row Count | SELECT id FROM person WHERE first_name = 'John' | *less than* | 4 | sansTran=True |
298+ | @{parameters} | Create List | John |
299+ | Check Row Count | SELECT id FROM person WHERE first_name = %s | *equals* | 5 | parameters=${parameters} |
300+ """
301+ logger .info (f"Executing : Check Row Count | { selectStatement } | { assertion_operator } | { expected_value } " )
302+ num_rows = self .row_count (selectStatement , sansTran , alias = alias , parameters = parameters )
303+ return verify_assertion (num_rows , assertion_operator , expected_value , "Wrong row count:" , assertion_message )
304+
305+ def check_query_result (
306+ self ,
307+ selectStatement ,
308+ assertion_operator : AssertionOperator ,
309+ expected_value : Any ,
310+ row = 0 ,
311+ col = 0 ,
312+ assertion_message : Optional [str ] = None ,
313+ sansTran : bool = False ,
314+ alias : Optional [str ] = None ,
315+ parameters : Optional [Tuple ] = None ,
316+ ):
317+ """
318+ Check value in query result returned from ``selectStatement`` using ``assertion_operator`` and ``expected_value``.
319+ The value position in results can be adjusted using ``row`` and ``col`` parameters (0-based).
320+ See `Inline assertions` for more details.
321+
322+ *The assertion in this keyword is type sensitive!*
323+ The ``expected_value`` is taken as a string, no argument conversion is performed.
324+ Use RF syntax like ``${1}`` for numeric values.
325+
326+ Use optional ``assertion_message`` to override the default error message.
327+
328+ Set optional input ``sansTran`` to _True_ to run command without an explicit transaction commit or rollback.
329+
330+ Use optional ``alias`` parameter to specify what connection should be used for the query if you have more
331+ than one connection open.
332+
333+ Use optional ``parameters`` for query variable substitution (variable substitution syntax may be different
334+ depending on the database client).
335+
336+ Examples:
337+ | Check Query Result | SELECT first_name FROM person | *contains* | Allan |
338+ | Check Query Result | SELECT first_name, last_name FROM person | *==* | Schneider | row=1 | col=1 |
339+ | Check Query Result | SELECT id FROM person WHERE first_name = 'John' | *==* | 2 | # Fails, if query returns an integer value |
340+ | Check Query Result | SELECT id FROM person WHERE first_name = 'John' | *==* | ${2} | # Works, if query returns an integer value |
341+ | Check Query Result | SELECT first_name FROM person | *equal* | Franz Allan | assertion_message=my error message |
342+ | Check Query Result | SELECT first_name FROM person | *inequal* | John | alias=my_alias |
343+ | Check Query Result | SELECT first_name FROM person | *contains* | Allan | sansTran=True |
344+ | @{parameters} | Create List | John |
345+ | Check Query Result | SELECT first_name FROM person | *contains* | Allan | parameters=${parameters} |
346+ """
347+ logger .info (
348+ f"Executing : Check Query Results | { selectStatement } | { assertion_operator } | { expected_value } | row = { row } | col = { col } "
349+ )
350+ query_results = self .query (selectStatement , sansTran , alias = alias , parameters = parameters )
351+
352+ row_count = len (query_results )
353+ assert row < row_count , f"Checking row '{ row } ' is not possible, as query results contain { row_count } rows only!"
354+ col_count = len (query_results [row ])
355+ assert (
356+ col < col_count
357+ ), f"Checking column '{ col } ' is not possible, as query results contain { col_count } columns only!"
358+
359+ actual_value = query_results [row ][col ]
360+ return verify_assertion (
361+ actual_value , assertion_operator , expected_value , "Wrong query result:" , assertion_message
362+ )
363+
250364 def table_must_exist (
251365 self , tableName : str , sansTran : bool = False , msg : Optional [str ] = None , alias : Optional [str ] = None
252366 ):
0 commit comments