From bcb2efcf0f0f4bfb1a0c0e94ae7cdd329d5d4b68 Mon Sep 17 00:00:00 2001 From: Quigley Malcolm Date: Wed, 15 May 2024 15:54:56 -0700 Subject: [PATCH] Move `EventCatcher` to `tests.utils` for use in unit and functional tests --- .../configs/test_warn_error_options.py | 2 +- .../test_check_for_spaces_in_model_names.py | 2 +- tests/unit/events/test_logging.py | 2 +- tests/utils.py | 17 +++++++++++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 tests/utils.py diff --git a/tests/functional/configs/test_warn_error_options.py b/tests/functional/configs/test_warn_error_options.py index 4b6552b0143..d8e68fcf11d 100644 --- a/tests/functional/configs/test_warn_error_options.py +++ b/tests/functional/configs/test_warn_error_options.py @@ -6,7 +6,7 @@ from dbt.events.types import DeprecatedModel from dbt.tests.util import update_config_file from dbt_common.events.base_types import EventLevel -from tests.functional.utils import EventCatcher +from tests.utils import EventCatcher ModelsDictSpec = Dict[str, Union[str, "ModelsDictSpec"]] diff --git a/tests/functional/manifest_validations/test_check_for_spaces_in_model_names.py b/tests/functional/manifest_validations/test_check_for_spaces_in_model_names.py index 4ba25a530f9..543a4557eea 100644 --- a/tests/functional/manifest_validations/test_check_for_spaces_in_model_names.py +++ b/tests/functional/manifest_validations/test_check_for_spaces_in_model_names.py @@ -10,7 +10,7 @@ ) from dbt.tests.util import update_config_file from dbt_common.events.base_types import EventLevel -from tests.functional.utils import EventCatcher +from tests.utils import EventCatcher class TestSpacesInModelNamesHappyPath: diff --git a/tests/unit/events/test_logging.py b/tests/unit/events/test_logging.py index a49c9066ed9..532f55e428d 100644 --- a/tests/unit/events/test_logging.py +++ b/tests/unit/events/test_logging.py @@ -8,7 +8,7 @@ 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.functional.utils import EventCatcher +from tests.utils import EventCatcher class TestSetupEventLogger: diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 00000000000..3f71cac38ef --- /dev/null +++ b/tests/utils.py @@ -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 = []