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.
A method to add callbacks to the global event manager (#132)
* Refactor `EventCatcher` into a test utilities file. We did this refactor because we plan to use the `EventCatcher` in more unit test files. We have a similar class in dbt-core currently. It might be worthwhile at a future date to distribute this class via the `dbt_common` package itself. Also the empty `__init__.py` files are unfortunately necessary it seems. This is because without them mypy will complain about "Source file found twice under different module names". * Fix event tests to ignore event types created in testing We had done this work previously by ignoring event classes that came from modules beginning with `test_`. This worked because if we created or re-implemented events in test files, their module was the the file that they existed in. However, in the previous commit 4d34929, we added `__init__.py` files to the directories under `tests`. This made it so that the events that we were previously successfully ignoring had their module updated to their full path, i.e. `tests.unit.<file_name>`, and thus their module no longer began with `test_`, but `tests.` instead. Because of this, we had a regression that caused the previously corrected tests to begin failing again. This commit fixes that. * Add `add_callback` method to `IEventManager` protocol and `EventManager` class * Add `add_callback_to_manager` for adding callbacks to global event manager * Refactor typing of callbacks to use a central `TCallback` type definition
- Loading branch information
Showing
12 changed files
with
69 additions
and
17 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: Support adding callbacks to the event manager | ||
time: 2024-05-14T16:20:52.120336-07:00 | ||
custom: | ||
Author: QMalcolm | ||
Issue: "131" |
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
Empty file.
Empty file.
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,11 @@ | ||
from dbt_common.events.event_manager import EventManager | ||
from tests.unit.utils import EventCatcher | ||
|
||
|
||
class TestEventManager: | ||
def test_add_callback(self) -> None: | ||
event_manager = EventManager() | ||
assert len(event_manager.callbacks) == 0 | ||
|
||
event_manager.add_callback(EventCatcher().catch) | ||
assert len(event_manager.callbacks) == 1 |
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,15 @@ | ||
from pytest_mock import MockerFixture | ||
|
||
from dbt_common.events.event_manager import EventManager | ||
from dbt_common.events.event_manager_client import add_callback_to_manager, get_event_manager | ||
from tests.unit.utils import EventCatcher | ||
|
||
|
||
def test_add_callback_to_manager(mocker: MockerFixture) -> None: | ||
# mock out the global event manager so the callback doesn't get added to all other tests | ||
mocker.patch("dbt_common.events.event_manager_client._EVENT_MANAGER", EventManager()) | ||
manager = get_event_manager() | ||
assert len(manager.callbacks) == 0 | ||
|
||
add_callback_to_manager(EventCatcher().catch) | ||
assert len(manager.callbacks) == 1 |
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,12 @@ | ||
from dataclasses import dataclass, field | ||
from typing import List | ||
|
||
from dbt_common.events.base_types import EventMsg | ||
|
||
|
||
@dataclass | ||
class EventCatcher: | ||
caught_events: List[EventMsg] = field(default_factory=list) | ||
|
||
def catch(self, event: EventMsg) -> None: | ||
self.caught_events.append(event) |