Skip to content

Commit dd69a3d

Browse files
committed
2 parents 583fe97 + 83311d1 commit dd69a3d

File tree

2 files changed

+148
-38
lines changed

2 files changed

+148
-38
lines changed

src/DatabaseLibrary/assertion.py

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ class Assertion(object):
2020
Assertion handles all the assertions of Database Library.
2121
"""
2222

23-
def check_if_exists_in_database(self, selectStatement, sansTran=False):
23+
def check_if_exists_in_database(self, selectStatement, sansTran=False, msg=None):
2424
"""
2525
Check if any row would be returned by given the input `selectStatement`. If there are no results, then this will
2626
throw an AssertionError. Set optional input `sansTran` to True to run command without an explicit transaction
27-
commit or rollback.
27+
commit or rollback. The default error message can be overridden with the `msg` argument.
2828
2929
For example, given we have a table `person` with the following data:
3030
| id | first_name | last_name |
@@ -40,19 +40,22 @@ def check_if_exists_in_database(self, selectStatement, sansTran=False):
4040
4141
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
4242
| Check If Exists In Database | SELECT id FROM person WHERE first_name = 'John' | True |
43+
44+
Using optional `msg` to override the default error message:
45+
| Check If Exists In Database | SELECT id FROM person WHERE first_name = 'John' | msg=my error message |
4346
"""
4447
logger.info ('Executing : Check If Exists In Database | %s ' % selectStatement)
4548
if not self.query(selectStatement, sansTran):
46-
raise AssertionError("Expected to have have at least one row from '%s' "
47-
"but got 0 rows." % selectStatement)
49+
raise AssertionError(msg or "Expected to have have at least one row from '%s' "
50+
"but got 0 rows." % selectStatement)
4851

49-
def check_if_not_exists_in_database(self, selectStatement, sansTran=False):
52+
def check_if_not_exists_in_database(self, selectStatement, sansTran=False, msg=None):
5053
"""
5154
This is the negation of `check_if_exists_in_database`.
5255
5356
Check if no rows would be returned by given the input `selectStatement`. If there are any results, then this
5457
will throw an AssertionError. Set optional input `sansTran` to True to run command without an explicit
55-
transaction commit or rollback.
58+
transaction commit or rollback. The default error message can be overridden with the `msg` argument.
5659
5760
For example, given we have a table `person` with the following data:
5861
| id | first_name | last_name |
@@ -68,18 +71,21 @@ def check_if_not_exists_in_database(self, selectStatement, sansTran=False):
6871
6972
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
7073
| Check If Not Exists In Database | SELECT id FROM person WHERE first_name = 'John' | True |
74+
75+
Using optional `msg` to override the default error message:
76+
| Check If Not Exists In Database | SELECT id FROM person WHERE first_name = 'Franz Allan' | msg=my error message |
7177
"""
7278
logger.info('Executing : Check If Not Exists In Database | %s ' % selectStatement)
7379
queryResults = self.query(selectStatement, sansTran)
7480
if queryResults:
75-
raise AssertionError("Expected to have have no rows from '%s' "
76-
"but got some rows : %s." % (selectStatement, queryResults))
81+
raise AssertionError(msg or "Expected to have have no rows from '%s' "
82+
"but got some rows : %s." % (selectStatement, queryResults))
7783

78-
def row_count_is_0(self, selectStatement, sansTran=False):
84+
def row_count_is_0(self, selectStatement, sansTran=False, msg=None):
7985
"""
8086
Check if any rows are returned from the submitted `selectStatement`. If there are, then this will throw an
8187
AssertionError. Set optional input `sansTran` to True to run command without an explicit transaction commit or
82-
rollback.
88+
rollback. The default error message can be overridden with the `msg` argument.
8389
8490
For example, given we have a table `person` with the following data:
8591
| id | first_name | last_name |
@@ -95,18 +101,21 @@ def row_count_is_0(self, selectStatement, sansTran=False):
95101
96102
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
97103
| Row Count is 0 | SELECT id FROM person WHERE first_name = 'John' | True |
104+
105+
Using optional `msg` to override the default error message:
106+
| Row Count is 0 | SELECT id FROM person WHERE first_name = 'Franz Allan' | msg=my error message |
98107
"""
99108
logger.info('Executing : Row Count Is 0 | %s ' % selectStatement)
100109
num_rows = self.row_count(selectStatement, sansTran)
101110
if num_rows > 0:
102-
raise AssertionError("Expected zero rows to be returned from '%s' "
103-
"but got rows back. Number of rows returned was %s" % (selectStatement, num_rows))
111+
raise AssertionError(msg or "Expected zero rows to be returned from '%s' "
112+
"but got rows back. Number of rows returned was %s" % (selectStatement, num_rows))
104113

105-
def row_count_is_equal_to_x(self, selectStatement, numRows, sansTran=False):
114+
def row_count_is_equal_to_x(self, selectStatement, numRows, sansTran=False, msg=None):
106115
"""
107116
Check if the number of rows returned from `selectStatement` is equal to the value submitted. If not, then this
108117
will throw an AssertionError. Set optional input `sansTran` to True to run command without an explicit
109-
transaction commit or rollback.
118+
transaction commit or rollback. The default error message can be overridden with the `msg` argument.
110119
111120
For example, given we have a table `person` with the following data:
112121
| id | first_name | last_name |
@@ -123,18 +132,21 @@ def row_count_is_equal_to_x(self, selectStatement, numRows, sansTran=False):
123132
124133
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
125134
| Row Count Is Equal To X | SELECT id FROM person WHERE first_name = 'John' | 0 | True |
135+
136+
Using optional `msg` to override the default error message:
137+
| Row Count Is Equal To X | SELECT id FROM person | 1 | msg=my error message |
126138
"""
127139
logger.info('Executing : Row Count Is Equal To X | %s | %s ' % (selectStatement, numRows))
128140
num_rows = self.row_count(selectStatement, sansTran)
129141
if num_rows != int(numRows.encode('ascii')):
130-
raise AssertionError("Expected same number of rows to be returned from '%s' "
131-
"than the returned rows of %s" % (selectStatement, num_rows))
142+
raise AssertionError(msg or "Expected same number of rows to be returned from '%s' "
143+
"than the returned rows of %s" % (selectStatement, num_rows))
132144

133-
def row_count_is_greater_than_x(self, selectStatement, numRows, sansTran=False):
145+
def row_count_is_greater_than_x(self, selectStatement, numRows, sansTran=False, msg=None):
134146
"""
135147
Check if the number of rows returned from `selectStatement` is greater than the value submitted. If not, then
136148
this will throw an AssertionError. Set optional input `sansTran` to True to run command without an explicit
137-
transaction commit or rollback.
149+
transaction commit or rollback. The default error message can be overridden with the `msg` argument.
138150
139151
For example, given we have a table `person` with the following data:
140152
| id | first_name | last_name |
@@ -151,14 +163,17 @@ def row_count_is_greater_than_x(self, selectStatement, numRows, sansTran=False):
151163
152164
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
153165
| Row Count Is Greater Than X | SELECT id FROM person | 1 | True |
166+
167+
Using optional `msg` to override the default error message:
168+
| Row Count Is Greater Than X | SELECT id FROM person WHERE first_name = 'John' | 0 | msg=my error message |
154169
"""
155170
logger.info('Executing : Row Count Is Greater Than X | %s | %s ' % (selectStatement, numRows))
156171
num_rows = self.row_count(selectStatement, sansTran)
157172
if num_rows <= int(numRows.encode('ascii')):
158-
raise AssertionError("Expected more rows to be returned from '%s' "
159-
"than the returned rows of %s" % (selectStatement, num_rows))
173+
raise AssertionError(msg or "Expected more rows to be returned from '%s' "
174+
"than the returned rows of %s" % (selectStatement, num_rows))
160175

161-
def row_count_is_less_than_x(self, selectStatement, numRows, sansTran=False):
176+
def row_count_is_less_than_x(self, selectStatement, numRows, sansTran=False, msg=None):
162177
"""
163178
Check if the number of rows returned from `selectStatement` is less than the value submitted. If not, then this
164179
will throw an AssertionError. Set optional input `sansTran` to True to run command without an explicit
@@ -179,17 +194,20 @@ def row_count_is_less_than_x(self, selectStatement, numRows, sansTran=False):
179194
180195
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
181196
| Row Count Is Less Than X | SELECT id FROM person | 3 | True |
197+
198+
Using optional `msg` to override the default error message:
199+
| Row Count Is Less Than X | SELECT id FROM person WHERE first_name = 'John' | 1 | msg=my error message |
182200
"""
183201
logger.info('Executing : Row Count Is Less Than X | %s | %s ' % (selectStatement, numRows))
184202
num_rows = self.row_count(selectStatement, sansTran)
185203
if num_rows >= int(numRows.encode('ascii')):
186-
raise AssertionError("Expected less rows to be returned from '%s' "
187-
"than the returned rows of %s" % (selectStatement, num_rows))
204+
raise AssertionError(msg or "Expected less rows to be returned from '%s' "
205+
"than the returned rows of %s" % (selectStatement, num_rows))
188206

189-
def table_must_exist(self, tableName, sansTran=False):
207+
def table_must_exist(self, tableName, sansTran=False, msg=None):
190208
"""
191209
Check if the table given exists in the database. Set optional input `sansTran` to True to run command without an
192-
explicit transaction commit or rollback.
210+
explicit transaction commit or rollback. The default error message can be overridden with the `msg` argument.
193211
194212
For example, given we have a table `person` in a database
195213
@@ -202,6 +220,9 @@ def table_must_exist(self, tableName, sansTran=False):
202220
203221
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
204222
| Table Must Exist | person | True |
223+
224+
Using optional `msg` to override the default error message:
225+
| Table Must Exist | first_name | msg=my error message |
205226
"""
206227
logger.info('Executing : Table Must Exist | %s ' % tableName)
207228
if self.db_api_module_name in ["cx_Oracle"]:
@@ -228,4 +249,5 @@ def table_must_exist(self, tableName, sansTran=False):
228249
table_exists = True
229250
except:
230251
table_exists = False
231-
assert table_exists, f"Table '{tableName}' does not exist in the db"
252+
assert table_exists, msg or f"Table '{tableName}' does not exist in the db"
253+

test/tests/_old/SQLite3_DB_Tests.robot

Lines changed: 100 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,117 @@ Check If Exists In DB - Franz Allan
4040
[Tags] db smoke
4141
Check If Exists In Database SELECT id FROM person WHERE first_name = 'Franz Allan';
4242

43+
Check If Exists In DB Fails
44+
[Tags] db smoke
45+
${expected_error} = Catenate
46+
... Expected to have have at least one row from 'SELECT id FROM person WHERE first_name = 'Joe';'
47+
... but got 0 rows.
48+
Run Keyword And Expect Error ${expected_error} Check If Exists In Database
49+
... SELECT id FROM person WHERE first_name = 'Joe';
50+
51+
Check If Exists In DB Fails With Message
52+
[Tags] db smoke
53+
Run Keyword And Expect Error my error message Check If Exists In Database
54+
... SELECT id FROM person WHERE first_name = 'Joe'; msg=my error message
55+
4356
Check If Not Exists In DB - Joe
4457
[Tags] db smoke
4558
Check If Not Exists In Database SELECT id FROM person WHERE first_name = 'Joe';
4659

60+
Check If Not Exists In DB Fails
61+
[Tags] db smoke
62+
${expected_error} = Catenate
63+
... Expected to have have no rows from 'SELECT id FROM person WHERE first_name = 'Franz Allan';'
64+
... but got some rows : [(1,)*
65+
Run Keyword And Expect Error ${expected_error} Check If Not Exists In Database
66+
... SELECT id FROM person WHERE first_name = 'Franz Allan';
67+
68+
Check If Not Exists In DB Fails With Message
69+
[Tags] db smoke
70+
Run Keyword And Expect Error my error message Check If Not Exists In Database
71+
... SELECT id FROM person WHERE first_name = 'Franz Allan'; msg=my error message
72+
4773
Table Must Exist - person
4874
[Tags] db smoke
4975
Table Must Exist person
5076

51-
Verify Row Count is 0
77+
Table Must Exist Fails
78+
[Tags] db smoke
79+
Run Keyword And Expect Error Table 'nonexistent' does not exist in the db
80+
... Table Must Exist nonexistent
81+
82+
Table Must Exist Fails With Message
83+
[Tags] db smoke
84+
Run Keyword And Expect Error my error message
85+
... Table Must Exist nonexistent msg=my error message
86+
87+
Verify Row Count Is 0
88+
[Tags] db smoke
89+
Row Count Is 0 SELECT * FROM person WHERE first_name = 'NotHere';
90+
91+
Verify Row Count Is 0 Fails
92+
[Tags] db smoke
93+
${expected_error} = Catenate
94+
... Expected zero rows to be returned from 'SELECT * FROM person WHERE first_name = 'Franz Allan';'
95+
... but got rows back. Number of rows returned was 1
96+
Run Keyword And Expect Error ${expected_error} Row Count Is 0
97+
... SELECT * FROM person WHERE first_name = 'Franz Allan';
98+
99+
Verify Row Count Is 0 Fails With Message
100+
[Tags] db smoke
101+
Run Keyword And Expect Error my error message Row Count Is 0
102+
... SELECT * FROM person WHERE first_name = 'Franz Allan'; msg=my error message
103+
104+
Verify Row Count Is Equal To X
105+
[Tags] db smoke
106+
Row Count Is Equal To X SELECT id FROM person; 2
107+
108+
Verify Row Count Is Equal To X Fails
109+
[Tags] db smoke
110+
${expected_error} = Catenate
111+
... Expected same number of rows to be returned from 'SELECT id FROM person;'
112+
... than the returned rows of 2
113+
Run Keyword And Expect Error ${expected_error}
114+
... Row Count Is Equal To X SELECT id FROM person; 3
115+
116+
Verify Row Count Is Equal To X Fails With Message
117+
[Tags] db smoke
118+
Run Keyword And Expect Error my error message Row Count Is Equal To X
119+
... SELECT id FROM person; 3 msg=my error message
120+
121+
Verify Row Count Is Less Than X
122+
[Tags] db smoke
123+
Row Count Is Less Than X SELECT id FROM person; 3
124+
125+
Verify Row Count Is Less Than X Fails
126+
[Tags] db smoke
127+
${expected_error} = Catenate
128+
... Expected less rows to be returned from 'SELECT id FROM person;'
129+
... than the returned rows of 2
130+
Run Keyword And Expect Error ${expected_error} Row Count Is Less Than X
131+
... SELECT id FROM person; 2
132+
133+
Verify Row Count Is Less Than X Fails With Message
52134
[Tags] db smoke
53-
Row Count is 0 SELECT * FROM person WHERE first_name = 'NotHere';
135+
Run Keyword And Expect Error my error message Row Count Is Less Than X
136+
... SELECT id FROM person; 2 msg=my error message
54137

55-
Verify Row Count is Equal to X
138+
Verify Row Count Is Greater Than X
56139
[Tags] db smoke
57-
Row Count is Equal to X SELECT id FROM person; 2
140+
Row Count Is Greater Than X SELECT * FROM person; 1
58141

59-
Verify Row Count is Less Than X
142+
Verify Row Count Is Greater Than X Fails
60143
[Tags] db smoke
61-
Row Count is Less Than X SELECT id FROM person; 3
144+
${expected_error} = Catenate
145+
... Expected more rows to be returned from 'SELECT * FROM person;'
146+
... than the returned rows of 2
147+
Run Keyword And Expect Error ${expected_error}
148+
... Row Count Is Greater Than X SELECT * FROM person; 3
62149

63-
Verify Row Count is Greater Than X
150+
Verify Row Count Is Greater Than X Fails With Message
64151
[Tags] db smoke
65-
Row Count is Greater Than X SELECT * FROM person; 1
152+
Run Keyword And Expect Error my error message Row Count Is Greater Than X
153+
... SELECT * FROM person; 3 msg=my error message
66154

67155
Retrieve Row Count
68156
[Tags] db smoke
@@ -168,7 +256,7 @@ Add person in first transaction
168256

169257
Verify person in first transaction
170258
[Tags] db smoke
171-
Row Count is Equal to X SELECT * FROM person WHERE last_name = 'Baggins'; 1 True
259+
Row Count Is Equal To X SELECT * FROM person WHERE last_name = 'Baggins'; 1 True
172260

173261
Begin second transaction
174262
[Tags] db smoke
@@ -184,7 +272,7 @@ Add person in second transaction
184272

185273
Verify persons in first and second transactions
186274
[Tags] db smoke
187-
Row Count is Equal to X SELECT * FROM person WHERE last_name = 'Baggins'; 2 True
275+
Row Count Is Equal To X SELECT * FROM person WHERE last_name = 'Baggins'; 2 True
188276

189277
Rollback second transaction
190278
[Tags] db smoke
@@ -194,7 +282,7 @@ Rollback second transaction
194282

195283
Verify second transaction rollback
196284
[Tags] db smoke
197-
Row Count is Equal to X SELECT * FROM person WHERE last_name = 'Baggins'; 1 True
285+
Row Count Is Equal To X SELECT * FROM person WHERE last_name = 'Baggins'; 1 True
198286

199287
Rollback first transaction
200288
[Tags] db smoke
@@ -204,7 +292,7 @@ Rollback first transaction
204292

205293
Verify first transaction rollback
206294
[Tags] db smoke
207-
Row Count is 0 SELECT * FROM person WHERE last_name = 'Baggins'; True
295+
Row Count Is 0 SELECT * FROM person WHERE last_name = 'Baggins'; True
208296

209297
Drop person and foobar tables
210298
[Tags] db smoke

0 commit comments

Comments
 (0)