|
3 | 3 | import pytest
|
4 | 4 |
|
5 | 5 | from dbt.cli.main import dbtRunner, dbtRunnerResult
|
6 |
| -from dbt.events.types import DeprecatedModel |
| 6 | +from dbt.events.types import ( |
| 7 | + DeprecatedModel, |
| 8 | + MainEncounteredError, |
| 9 | + MicrobatchModelNoEventTimeInputs, |
| 10 | +) |
7 | 11 | from dbt.flags import get_flags
|
8 | 12 | from dbt.tests.util import run_dbt, update_config_file
|
9 | 13 | from dbt_common.events.base_types import EventLevel
|
@@ -229,3 +233,66 @@ def test_project_flags(self, project):
|
229 | 233 | # Note: WarnErrorOptions is not a dataclass, so you won't get "silence"
|
230 | 234 | # from to_dict or stringifying.
|
231 | 235 | assert flags.warn_error_options.silence == ["TestsConfigDeprecation"]
|
| 236 | + |
| 237 | + |
| 238 | +input_model_without_event_time_sql = """ |
| 239 | +{{ config(materialized='table') }} |
| 240 | +
|
| 241 | +select 1 as id, TIMESTAMP '2020-01-01 00:00:00-0' as event_time |
| 242 | +union all |
| 243 | +select 2 as id, TIMESTAMP '2020-01-02 00:00:00-0' as event_time |
| 244 | +union all |
| 245 | +select 3 as id, TIMESTAMP '2020-01-03 00:00:00-0' as event_time |
| 246 | +""" |
| 247 | + |
| 248 | +microbatch_model_sql = """ |
| 249 | +{{config(materialized='incremental', incremental_strategy='microbatch', unique_key='id', event_time='event_time', batch_size='day', begin=modules.datetime.datetime.now())}} |
| 250 | +SELECT id, event_time FROM {{ ref('input_model') }} |
| 251 | +""" |
| 252 | + |
| 253 | + |
| 254 | +class TestRequireAllWarningsHandledByWarnErrorBehaviorFlag: |
| 255 | + @pytest.fixture(scope="class") |
| 256 | + def models(self): |
| 257 | + return { |
| 258 | + "input_model.sql": input_model_without_event_time_sql, |
| 259 | + "microbatch_model.sql": microbatch_model_sql, |
| 260 | + } |
| 261 | + |
| 262 | + def test_require_all_warnings_handed_by_warn_error_behavior_flag(self, project): |
| 263 | + # Setup the event catchers |
| 264 | + microbatch_warning_catcher = EventCatcher(event_to_catch=MicrobatchModelNoEventTimeInputs) |
| 265 | + microbatch_error_catcher = EventCatcher(event_to_catch=MainEncounteredError) |
| 266 | + dbt_runner = dbtRunner( |
| 267 | + callbacks=[microbatch_warning_catcher.catch, microbatch_error_catcher.catch] |
| 268 | + ) |
| 269 | + |
| 270 | + # Run the command without the behavior flag off |
| 271 | + project_flags = { |
| 272 | + "flags": { |
| 273 | + "send_anonymous_usage_stats": False, |
| 274 | + "require_all_warnings_handled_by_warn_error": False, |
| 275 | + } |
| 276 | + } |
| 277 | + update_config_file(project_flags, project.project_root, "dbt_project.yml") |
| 278 | + dbt_runner.invoke(["run", "--warn-error"]) |
| 279 | + |
| 280 | + assert len(microbatch_warning_catcher.caught_events) == 1 |
| 281 | + assert len(microbatch_error_catcher.caught_events) == 0 |
| 282 | + |
| 283 | + # Reset the event catchers |
| 284 | + microbatch_warning_catcher.flush() |
| 285 | + microbatch_error_catcher.flush() |
| 286 | + |
| 287 | + # Run the command with the behavior flag on |
| 288 | + project_flags = { |
| 289 | + "flags": { |
| 290 | + "send_anonymous_usage_stats": False, |
| 291 | + "require_all_warnings_handled_by_warn_error": True, |
| 292 | + } |
| 293 | + } |
| 294 | + update_config_file(project_flags, project.project_root, "dbt_project.yml") |
| 295 | + dbt_runner.invoke(["run", "--warn-error", "--log-format", "json"]) |
| 296 | + |
| 297 | + assert len(microbatch_warning_catcher.caught_events) == 0 |
| 298 | + assert len(microbatch_error_catcher.caught_events) == 1 |
0 commit comments