generated from dbt-labs/dbt-oss-template
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `silence` option to `WarnErrorOptions` * Add `silenced` def to `IncludeExclude` and use in `includes` * Begin using `silenced` of `WarnErrorOptions` in `warn_or_error` Technically since we modified `includes` in the previous commit, `warn_or_error` started including `silenced` in it's logic then. However we make it explicit in this commit, and begin testing it. * Refactor `get_all_subclasses` in `test_events` to ignore events classes defined in tests In the previous commit we defined a `Note` event class based on `WarnLevel`. The logic in `get_all_subclasses` was getting test defined event classes, which isn't what we want. Thus here we added logic to exclude these. * Changie doc for new `silence` of `WarnErrorOptions` * Refactor `silence` logic from `IncludeExclude` to `WarnErrorOptions` * Create `event_name` in `warn_or_error` to reduce regeneration of name
- Loading branch information
Showing
6 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Add ability to silence warnings via `WarnErrorOptions` | ||
time: 2024-04-19T23:20:30.014054-07:00 | ||
custom: | ||
Author: QMalcolm | ||
Issue: "111" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import pytest | ||
|
||
from dataclasses import dataclass, field | ||
from dbt_common.events import functions | ||
from dbt_common.events.base_types import EventLevel, EventMsg, WarnLevel | ||
from dbt_common.events.event_manager import EventManager | ||
from dbt_common.events.event_manager_client import ctx_set_event_manager | ||
from dbt_common.exceptions import EventCompilationError | ||
from dbt_common.helper_types import WarnErrorOptions | ||
from typing import List, Set | ||
|
||
|
||
# Re-implementing `Note` event as a warn event for | ||
# our testing purposes | ||
class Note(WarnLevel): | ||
def code(self) -> str: | ||
return "Z050" | ||
|
||
def message(self) -> str: | ||
return self.msg | ||
|
||
|
||
@dataclass | ||
class EventCatcher: | ||
caught_events: List[EventMsg] = field(default_factory=list) | ||
|
||
def catch(self, event: EventMsg) -> None: | ||
self.caught_events.append(event) | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def event_catcher() -> EventCatcher: | ||
return EventCatcher() | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def set_event_manager_with_catcher(event_catcher: EventCatcher) -> None: | ||
event_manager = EventManager() | ||
event_manager.callbacks.append(event_catcher.catch) | ||
ctx_set_event_manager(event_manager) | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def valid_error_names() -> Set[str]: | ||
return {Note.__name__} | ||
|
||
|
||
class TestWarnOrError: | ||
def test_fires_error(self, valid_error_names: Set[str]): | ||
functions.WARN_ERROR_OPTIONS = WarnErrorOptions( | ||
include="*", valid_error_names=valid_error_names | ||
) | ||
with pytest.raises(EventCompilationError): | ||
functions.warn_or_error(Note(msg="hi")) | ||
|
||
def test_fires_warning( | ||
self, | ||
valid_error_names: Set[str], | ||
event_catcher: EventCatcher, | ||
set_event_manager_with_catcher, | ||
): | ||
functions.WARN_ERROR_OPTIONS = WarnErrorOptions( | ||
include="*", exclude=list(valid_error_names), valid_error_names=valid_error_names | ||
) | ||
functions.warn_or_error(Note(msg="hi")) | ||
assert len(event_catcher.caught_events) == 1 | ||
assert event_catcher.caught_events[0].info.level == EventLevel.WARN.value | ||
|
||
def test_silenced( | ||
self, | ||
valid_error_names: Set[str], | ||
event_catcher: EventCatcher, | ||
set_event_manager_with_catcher, | ||
): | ||
functions.WARN_ERROR_OPTIONS = WarnErrorOptions( | ||
include="*", silence=list(valid_error_names), valid_error_names=valid_error_names | ||
) | ||
functions.warn_or_error(Note(msg="hi")) | ||
assert len(event_catcher.caught_events) == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters