Skip to content

Commit 2013286

Browse files
committed
Move the patch to an autouse fixture
1 parent 7aedc4e commit 2013286

File tree

3 files changed

+43
-40
lines changed

3 files changed

+43
-40
lines changed

tests/__init__.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
1-
import typing as t
2-
31
from sqlmesh.core import constants as c
42
from sqlmesh.core.analytics import disable_analytics
5-
from sqlmesh.core.config import Config, DuckDBConnectionConfig
6-
from sqlmesh.core.config.connection import ConnectionConfig
73

84
c.MAX_FORK_WORKERS = 1
95
disable_analytics()
10-
11-
12-
# Replace the implementation of Config._get_connection with a new one that always
13-
# returns a ConnectionConfig to simplify testing
14-
original_get_connection = Config.get_connection
15-
16-
17-
def _lax_get_connection(self, gateway_name: t.Optional[str] = None) -> ConnectionConfig:
18-
try:
19-
connection = original_get_connection(self, gateway_name)
20-
except:
21-
connection = DuckDBConnectionConfig()
22-
return connection
23-
24-
25-
setattr(Config, "original_get_connection", original_get_connection)
26-
setattr(Config, "get_connection", _lax_get_connection)

tests/conftest.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from __future__ import annotations
22

3+
34
import datetime
45
import logging
56
import typing as t
67
import uuid
8+
from contextlib import nullcontext
79
from pathlib import Path
810
from shutil import copytree, rmtree
911
from tempfile import TemporaryDirectory
@@ -21,7 +23,8 @@
2123
from sqlglot.helper import ensure_list
2224
from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
2325

24-
from sqlmesh.core.config import BaseDuckDBConnectionConfig
26+
from sqlmesh.core.config import Config, BaseDuckDBConnectionConfig, DuckDBConnectionConfig
27+
from sqlmesh.core.config.connection import ConnectionConfig
2528
from sqlmesh.core.context import Context
2629
from sqlmesh.core.engine_adapter import MSSQLEngineAdapter, SparkEngineAdapter
2730
from sqlmesh.core.engine_adapter.base import EngineAdapter
@@ -526,3 +529,26 @@ def _make_function(table_name: str, random_id: str) -> exp.Table:
526529
return temp_table
527530

528531
return _make_function
532+
533+
534+
@pytest.fixture(scope="function", autouse=True)
535+
def set_default_connection(request):
536+
request = request.node.get_closest_marker("set_default_connection")
537+
disable = request and request.kwargs.get("disable")
538+
539+
if disable:
540+
ctx = nullcontext()
541+
else:
542+
original_get_connection = Config.get_connection
543+
544+
def _lax_get_connection(self, gateway_name: t.Optional[str] = None) -> ConnectionConfig:
545+
try:
546+
connection = original_get_connection(self, gateway_name)
547+
except:
548+
connection = DuckDBConnectionConfig()
549+
return connection
550+
551+
ctx = mock.patch("sqlmesh.core.config.Config.get_connection", _lax_get_connection)
552+
553+
with ctx:
554+
yield

tests/core/test_integration.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6131,22 +6131,20 @@ def setup_senario(model_before: str, model_after: str):
61316131
)
61326132

61336133

6134-
@pytest.mark.isolated
6134+
@pytest.mark.set_default_connection(disable=True)
61356135
def test_missing_connection_config():
6136-
# This is the actual implementation of Config._get_connection which would be used in a real project
6137-
# To make testing easier, it's mocked at tests/__init__.py
6138-
6139-
with mock.patch.object(Config, "get_connection", Config.original_get_connection):
6140-
# Case 1: No default_connection or gateways specified should raise a ConfigError
6141-
with pytest.raises(ConfigError):
6142-
ctx = Context(config=Config())
6143-
6144-
# Case 2: No connection specified in the gateway should raise a ConfigError
6145-
with pytest.raises(ConfigError):
6146-
ctx = Context(config=Config(gateways={"default": GatewayConfig()}))
6147-
6148-
# Case 3: Specifying a default_connection or connection in the gateway should work
6149-
ctx = Context(config=Config(default_connection=DuckDBConnectionConfig()))
6150-
ctx = Context(
6151-
config=Config(gateways={"default": GatewayConfig(connection=DuckDBConnectionConfig())})
6152-
)
6136+
# This is testing the actual implementation of Config.get_connection
6137+
# To make writing tests easier, it's patched by the autouse fixture provide_sqlmesh_default_connection
6138+
# Case 1: No default_connection or gateways specified should raise a ConfigError
6139+
with pytest.raises(ConfigError):
6140+
ctx = Context(config=Config())
6141+
6142+
# Case 2: No connection specified in the gateway should raise a ConfigError
6143+
with pytest.raises(ConfigError):
6144+
ctx = Context(config=Config(gateways={"incorrect": GatewayConfig()}))
6145+
6146+
# Case 3: Specifying a default_connection or connection in the gateway should work
6147+
ctx = Context(config=Config(default_connection=DuckDBConnectionConfig()))
6148+
ctx = Context(
6149+
config=Config(gateways={"default": GatewayConfig(connection=DuckDBConnectionConfig())})
6150+
)

0 commit comments

Comments
 (0)