@@ -20,11 +20,11 @@ class Assertion(object):
20
20
Assertion handles all the assertions of Database Library.
21
21
"""
22
22
23
- def check_if_exists_in_database (self , selectStatement , sansTran = False ):
23
+ def check_if_exists_in_database (self , selectStatement , sansTran = False , msg = None ):
24
24
"""
25
25
Check if any row would be returned by given the input `selectStatement`. If there are no results, then this will
26
26
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.
28
28
29
29
For example, given we have a table `person` with the following data:
30
30
| id | first_name | last_name |
@@ -40,19 +40,22 @@ def check_if_exists_in_database(self, selectStatement, sansTran=False):
40
40
41
41
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
42
42
| 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 |
43
46
"""
44
47
logger .info ('Executing : Check If Exists In Database | %s ' % selectStatement )
45
48
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 )
48
51
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 ):
50
53
"""
51
54
This is the negation of `check_if_exists_in_database`.
52
55
53
56
Check if no rows would be returned by given the input `selectStatement`. If there are any results, then this
54
57
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.
56
59
57
60
For example, given we have a table `person` with the following data:
58
61
| id | first_name | last_name |
@@ -68,18 +71,21 @@ def check_if_not_exists_in_database(self, selectStatement, sansTran=False):
68
71
69
72
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
70
73
| 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 |
71
77
"""
72
78
logger .info ('Executing : Check If Not Exists In Database | %s ' % selectStatement )
73
79
queryResults = self .query (selectStatement , sansTran )
74
80
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 ))
77
83
78
- def row_count_is_0 (self , selectStatement , sansTran = False ):
84
+ def row_count_is_0 (self , selectStatement , sansTran = False , msg = None ):
79
85
"""
80
86
Check if any rows are returned from the submitted `selectStatement`. If there are, then this will throw an
81
87
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.
83
89
84
90
For example, given we have a table `person` with the following data:
85
91
| id | first_name | last_name |
@@ -95,18 +101,21 @@ def row_count_is_0(self, selectStatement, sansTran=False):
95
101
96
102
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
97
103
| 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 |
98
107
"""
99
108
logger .info ('Executing : Row Count Is 0 | %s ' % selectStatement )
100
109
num_rows = self .row_count (selectStatement , sansTran )
101
110
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 ))
104
113
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 ):
106
115
"""
107
116
Check if the number of rows returned from `selectStatement` is equal to the value submitted. If not, then this
108
117
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.
110
119
111
120
For example, given we have a table `person` with the following data:
112
121
| id | first_name | last_name |
@@ -123,18 +132,21 @@ def row_count_is_equal_to_x(self, selectStatement, numRows, sansTran=False):
123
132
124
133
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
125
134
| 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 |
126
138
"""
127
139
logger .info ('Executing : Row Count Is Equal To X | %s | %s ' % (selectStatement , numRows ))
128
140
num_rows = self .row_count (selectStatement , sansTran )
129
141
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 ))
132
144
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 ):
134
146
"""
135
147
Check if the number of rows returned from `selectStatement` is greater than the value submitted. If not, then
136
148
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.
138
150
139
151
For example, given we have a table `person` with the following data:
140
152
| id | first_name | last_name |
@@ -151,14 +163,17 @@ def row_count_is_greater_than_x(self, selectStatement, numRows, sansTran=False):
151
163
152
164
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
153
165
| 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 |
154
169
"""
155
170
logger .info ('Executing : Row Count Is Greater Than X | %s | %s ' % (selectStatement , numRows ))
156
171
num_rows = self .row_count (selectStatement , sansTran )
157
172
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 ))
160
175
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 ):
162
177
"""
163
178
Check if the number of rows returned from `selectStatement` is less than the value submitted. If not, then this
164
179
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):
179
194
180
195
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
181
196
| 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 |
182
200
"""
183
201
logger .info ('Executing : Row Count Is Less Than X | %s | %s ' % (selectStatement , numRows ))
184
202
num_rows = self .row_count (selectStatement , sansTran )
185
203
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 ))
188
206
189
- def table_must_exist (self , tableName , sansTran = False ):
207
+ def table_must_exist (self , tableName , sansTran = False , msg = None ):
190
208
"""
191
209
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.
193
211
194
212
For example, given we have a table `person` in a database
195
213
@@ -202,6 +220,9 @@ def table_must_exist(self, tableName, sansTran=False):
202
220
203
221
Using optional `sansTran` to run command without an explicit transaction commit or rollback:
204
222
| 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 |
205
226
"""
206
227
logger .info ('Executing : Table Must Exist | %s ' % tableName )
207
228
if self .db_api_module_name in ["cx_Oracle" ]:
@@ -228,4 +249,5 @@ def table_must_exist(self, tableName, sansTran=False):
228
249
table_exists = True
229
250
except :
230
251
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
+
0 commit comments