-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Standardize event catching during repository unit tests #10153
Changes from 5 commits
7e91db2
1d0f5f4
38bcde5
e08c448
bcb2efc
67c87fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from argparse import Namespace | ||
from copy import deepcopy | ||
|
||
from pytest_mock import MockerFixture | ||
|
||
from dbt.events.logging import setup_event_logger | ||
from dbt.flags import get_flags, set_from_args | ||
from dbt_common.events.base_types import BaseEvent | ||
from dbt_common.events.event_manager_client import get_event_manager | ||
from dbt_common.events.logger import LoggerConfig | ||
from tests.utils import EventCatcher | ||
|
||
|
||
class TestSetupEventLogger: | ||
def test_clears_preexisting_event_manager_state(self, mock_global_event_manager) -> None: | ||
manager = get_event_manager() | ||
manager.add_logger(LoggerConfig(name="test_logger")) | ||
manager.callbacks.append(EventCatcher(BaseEvent).catch) | ||
assert len(manager.loggers) == 1 | ||
assert len(manager.callbacks) == 1 | ||
|
||
flags = deepcopy(get_flags()) | ||
# setting both of these to none guarantees that no logger will be added | ||
object.__setattr__(flags, "LOG_LEVEL", "none") | ||
object.__setattr__(flags, "LOG_LEVEL_FILE", "none") | ||
|
||
setup_event_logger(flags=flags) | ||
assert len(manager.loggers) == 0 | ||
assert len(manager.callbacks) == 0 | ||
|
||
def test_specify_max_bytes( | ||
self, | ||
mocker: MockerFixture, | ||
mock_global_event_manager, | ||
) -> None: | ||
patched_file_handler = mocker.patch("dbt_common.events.logger.RotatingFileHandler") | ||
args = Namespace(log_file_max_bytes=1234567) | ||
set_from_args(args, {}) | ||
setup_event_logger(get_flags()) | ||
Comment on lines
+37
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One theory I had was that although this commit primarily just moves the test, I also made one small change which is highlighted here. Specifically, in this test previously, these methods were called via the module instead of directly. That is these two lines were flags.set_from_args(args, {})
setup_event_logger(flags.get_flags()) However changing these lines back to what they were and the the associated import did not solve the broken test issue we're seeing 🤔 |
||
patched_file_handler.assert_called_once_with( | ||
filename="logs/dbt.log", encoding="utf8", maxBytes=1234567, backupCount=5 | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import pytest | ||
from pytest_mock import MockerFixture | ||
|
||
from dbt_common.events.event_manager import EventManager | ||
|
||
|
||
@pytest.fixture | ||
def mock_global_event_manager(mocker: MockerFixture) -> None: | ||
"""Mocks the global _EVENT_MANAGER so that unit tests can safely modify it without worry about other tests.""" | ||
mocker.patch("dbt_common.events.event_manager_client._EVENT_MANAGER", EventManager()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from dataclasses import dataclass, field | ||
from typing import List | ||
|
||
from dbt_common.events.base_types import BaseEvent, EventMsg | ||
|
||
|
||
@dataclass | ||
class EventCatcher: | ||
event_to_catch: BaseEvent | ||
caught_events: List[EventMsg] = field(default_factory=list) | ||
|
||
def catch(self, event: EventMsg): | ||
if event.info.name == self.event_to_catch.__name__: | ||
self.caught_events.append(event) | ||
|
||
def flush(self) -> None: | ||
self.caught_events = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverting this commit fixes our broken
tests/unit/graph/test_selector.py::test_run_specs[a,b--]
test problem. I don't understand why.