21
21
22
22
import pytest
23
23
24
- from airflow .exceptions import AirflowException
24
+ from airflow .exceptions import AirflowException , AirflowSkipException
25
25
from airflow .models .dag import DAG
26
26
from airflow .providers .common .sql .hooks .sql import DbApiHook
27
27
from airflow .providers .common .sql .sensors .sql import SqlSensor
@@ -117,17 +117,26 @@ def test_sql_sensor_postgres_poke(self, mock_hook):
117
117
mock_get_records .return_value = [["1" ]]
118
118
assert op .poke (None )
119
119
120
+ @pytest .mark .parametrize (
121
+ "soft_fail, expected_exception" , ((False , AirflowException ), (True , AirflowSkipException ))
122
+ )
120
123
@mock .patch ("airflow.providers.common.sql.sensors.sql.BaseHook" )
121
- def test_sql_sensor_postgres_poke_fail_on_empty (self , mock_hook ):
124
+ def test_sql_sensor_postgres_poke_fail_on_empty (
125
+ self , mock_hook , soft_fail : bool , expected_exception : AirflowException
126
+ ):
122
127
op = SqlSensor (
123
- task_id = "sql_sensor_check" , conn_id = "postgres_default" , sql = "SELECT 1" , fail_on_empty = True
128
+ task_id = "sql_sensor_check" ,
129
+ conn_id = "postgres_default" ,
130
+ sql = "SELECT 1" ,
131
+ fail_on_empty = True ,
132
+ soft_fail = soft_fail ,
124
133
)
125
134
126
135
mock_hook .get_connection .return_value .get_hook .return_value = mock .MagicMock (spec = DbApiHook )
127
136
mock_get_records = mock_hook .get_connection .return_value .get_hook .return_value .get_records
128
137
129
138
mock_get_records .return_value = []
130
- with pytest .raises (AirflowException ):
139
+ with pytest .raises (expected_exception ):
131
140
op .poke (None )
132
141
133
142
@mock .patch ("airflow.providers.common.sql.sensors.sql.BaseHook" )
@@ -148,10 +157,19 @@ def test_sql_sensor_postgres_poke_success(self, mock_hook):
148
157
mock_get_records .return_value = [["1" ]]
149
158
assert not op .poke (None )
150
159
160
+ @pytest .mark .parametrize (
161
+ "soft_fail, expected_exception" , ((False , AirflowException ), (True , AirflowSkipException ))
162
+ )
151
163
@mock .patch ("airflow.providers.common.sql.sensors.sql.BaseHook" )
152
- def test_sql_sensor_postgres_poke_failure (self , mock_hook ):
164
+ def test_sql_sensor_postgres_poke_failure (
165
+ self , mock_hook , soft_fail : bool , expected_exception : AirflowException
166
+ ):
153
167
op = SqlSensor (
154
- task_id = "sql_sensor_check" , conn_id = "postgres_default" , sql = "SELECT 1" , failure = lambda x : x in [1 ]
168
+ task_id = "sql_sensor_check" ,
169
+ conn_id = "postgres_default" ,
170
+ sql = "SELECT 1" ,
171
+ failure = lambda x : x in [1 ],
172
+ soft_fail = soft_fail ,
155
173
)
156
174
157
175
mock_hook .get_connection .return_value .get_hook .return_value = mock .MagicMock (spec = DbApiHook )
@@ -161,17 +179,23 @@ def test_sql_sensor_postgres_poke_failure(self, mock_hook):
161
179
assert not op .poke (None )
162
180
163
181
mock_get_records .return_value = [[1 ]]
164
- with pytest .raises (AirflowException ):
182
+ with pytest .raises (expected_exception ):
165
183
op .poke (None )
166
184
185
+ @pytest .mark .parametrize (
186
+ "soft_fail, expected_exception" , ((False , AirflowException ), (True , AirflowSkipException ))
187
+ )
167
188
@mock .patch ("airflow.providers.common.sql.sensors.sql.BaseHook" )
168
- def test_sql_sensor_postgres_poke_failure_success (self , mock_hook ):
189
+ def test_sql_sensor_postgres_poke_failure_success (
190
+ self , mock_hook , soft_fail : bool , expected_exception : AirflowException
191
+ ):
169
192
op = SqlSensor (
170
193
task_id = "sql_sensor_check" ,
171
194
conn_id = "postgres_default" ,
172
195
sql = "SELECT 1" ,
173
196
failure = lambda x : x in [1 ],
174
197
success = lambda x : x in [2 ],
198
+ soft_fail = soft_fail ,
175
199
)
176
200
177
201
mock_hook .get_connection .return_value .get_hook .return_value = mock .MagicMock (spec = DbApiHook )
@@ -181,20 +205,26 @@ def test_sql_sensor_postgres_poke_failure_success(self, mock_hook):
181
205
assert not op .poke (None )
182
206
183
207
mock_get_records .return_value = [[1 ]]
184
- with pytest .raises (AirflowException ):
208
+ with pytest .raises (expected_exception ):
185
209
op .poke (None )
186
210
187
211
mock_get_records .return_value = [[2 ]]
188
212
assert op .poke (None )
189
213
214
+ @pytest .mark .parametrize (
215
+ "soft_fail, expected_exception" , ((False , AirflowException ), (True , AirflowSkipException ))
216
+ )
190
217
@mock .patch ("airflow.providers.common.sql.sensors.sql.BaseHook" )
191
- def test_sql_sensor_postgres_poke_failure_success_same (self , mock_hook ):
218
+ def test_sql_sensor_postgres_poke_failure_success_same (
219
+ self , mock_hook , soft_fail : bool , expected_exception : AirflowException
220
+ ):
192
221
op = SqlSensor (
193
222
task_id = "sql_sensor_check" ,
194
223
conn_id = "postgres_default" ,
195
224
sql = "SELECT 1" ,
196
225
failure = lambda x : x in [1 ],
197
226
success = lambda x : x in [1 ],
227
+ soft_fail = soft_fail ,
198
228
)
199
229
200
230
mock_hook .get_connection .return_value .get_hook .return_value = mock .MagicMock (spec = DbApiHook )
@@ -204,40 +234,52 @@ def test_sql_sensor_postgres_poke_failure_success_same(self, mock_hook):
204
234
assert not op .poke (None )
205
235
206
236
mock_get_records .return_value = [[1 ]]
207
- with pytest .raises (AirflowException ):
237
+ with pytest .raises (expected_exception ):
208
238
op .poke (None )
209
239
240
+ @pytest .mark .parametrize (
241
+ "soft_fail, expected_exception" , ((False , AirflowException ), (True , AirflowSkipException ))
242
+ )
210
243
@mock .patch ("airflow.providers.common.sql.sensors.sql.BaseHook" )
211
- def test_sql_sensor_postgres_poke_invalid_failure (self , mock_hook ):
244
+ def test_sql_sensor_postgres_poke_invalid_failure (
245
+ self , mock_hook , soft_fail : bool , expected_exception : AirflowException
246
+ ):
212
247
op = SqlSensor (
213
248
task_id = "sql_sensor_check" ,
214
249
conn_id = "postgres_default" ,
215
250
sql = "SELECT 1" ,
216
251
failure = [1 ],
252
+ soft_fail = soft_fail ,
217
253
)
218
254
219
255
mock_hook .get_connection .return_value .get_hook .return_value = mock .MagicMock (spec = DbApiHook )
220
256
mock_get_records = mock_hook .get_connection .return_value .get_hook .return_value .get_records
221
257
222
258
mock_get_records .return_value = [[1 ]]
223
- with pytest .raises (AirflowException ) as ctx :
259
+ with pytest .raises (expected_exception ) as ctx :
224
260
op .poke (None )
225
261
assert "self.failure is present, but not callable -> [1]" == str (ctx .value )
226
262
263
+ @pytest .mark .parametrize (
264
+ "soft_fail, expected_exception" , ((False , AirflowException ), (True , AirflowSkipException ))
265
+ )
227
266
@mock .patch ("airflow.providers.common.sql.sensors.sql.BaseHook" )
228
- def test_sql_sensor_postgres_poke_invalid_success (self , mock_hook ):
267
+ def test_sql_sensor_postgres_poke_invalid_success (
268
+ self , mock_hook , soft_fail : bool , expected_exception : AirflowException
269
+ ):
229
270
op = SqlSensor (
230
271
task_id = "sql_sensor_check" ,
231
272
conn_id = "postgres_default" ,
232
273
sql = "SELECT 1" ,
233
274
success = [1 ],
275
+ soft_fail = soft_fail ,
234
276
)
235
277
236
278
mock_hook .get_connection .return_value .get_hook .return_value = mock .MagicMock (spec = DbApiHook )
237
279
mock_get_records = mock_hook .get_connection .return_value .get_hook .return_value .get_records
238
280
239
281
mock_get_records .return_value = [[1 ]]
240
- with pytest .raises (AirflowException ) as ctx :
282
+ with pytest .raises (expected_exception ) as ctx :
241
283
op .poke (None )
242
284
assert "self.success is present, but not callable -> [1]" == str (ctx .value )
243
285
0 commit comments