Skip to content

Commit a5fd075

Browse files
authored
chore: fix dbt test flakiness (#5530)
1 parent 4b210f2 commit a5fd075

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

tests/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,12 @@ def rescope_lineage_cache(request):
261261

262262
@pytest.fixture(autouse=True)
263263
def reset_console():
264-
from sqlmesh.core.console import set_console, NoopConsole
264+
from sqlmesh.core.console import set_console, NoopConsole, get_console
265265

266+
orig_console = get_console()
266267
set_console(NoopConsole())
268+
yield
269+
set_console(orig_console)
267270

268271

269272
@pytest.fixture

tests/dbt/conftest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,26 @@ def dbt_dummy_postgres_config() -> PostgresConfig:
127127
port=5432,
128128
schema="schema",
129129
)
130+
131+
132+
@pytest.fixture(scope="function", autouse=True)
133+
def reset_dbt_globals():
134+
# This fixture is used to clear the memoized cache for _get_package_with_retries
135+
# in dbt.clients.registry. This is necessary because the cache is shared across
136+
# tests and can cause unexpected behavior if not cleared as some tests depend on
137+
# the deprecation warning that _get_package_with_retries fires
138+
yield
139+
# https://github.com/dbt-labs/dbt-core/blob/main/tests/functional/conftest.py#L9
140+
try:
141+
from dbt.clients.registry import _get_cached
142+
143+
_get_cached.cache = {}
144+
except Exception:
145+
pass
146+
# https://github.com/dbt-labs/dbt-core/blob/main/core/dbt/tests/util.py#L82
147+
try:
148+
from dbt_common.events.functions import reset_metadata_vars
149+
150+
reset_metadata_vars()
151+
except Exception:
152+
pass

tests/dbt/test_model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from sqlglot import exp
77
from sqlglot.errors import SchemaError
88
from sqlmesh import Context
9+
from sqlmesh.core.console import NoopConsole, get_console
910
from sqlmesh.core.model import TimeColumn, IncrementalByTimeRangeKind
1011
from sqlmesh.core.model.kind import OnDestructiveChange, OnAdditiveChange
1112
from sqlmesh.core.state_sync.db.snapshot import _snapshot_to_json
@@ -537,6 +538,7 @@ def test_load_deprecated_incremental_time_column(
537538
f.write(incremental_time_range_contents)
538539

539540
snapshot_fqn = '"local"."main"."incremental_time_range"'
541+
assert isinstance(get_console(), NoopConsole)
540542
context = Context(paths=project_dir)
541543
model = context.snapshots[snapshot_fqn].model
542544
# Validate model-level attributes

0 commit comments

Comments
 (0)