Skip to content

Commit 4f33b2d

Browse files
committed
test: keep new test setup branch-free and constants first
1 parent b46eb7f commit 4f33b2d

4 files changed

Lines changed: 45 additions & 30 deletions

File tree

‎tests/e2e/src/sqlbuild/cli/commands/main/build/test_materialization_type_change.py‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@
2323
'name = "orders"\nadapter = "duckdb"\n[connection]\ndatabase = "orders.duckdb"\n'
2424
)
2525
_MODEL_PATH: str = "models/order_totals.sql"
26+
_SNAPSHOT_PROJECT_TOML: str = (
27+
'name = "customers"\nadapter = "duckdb"\n[connection]\ndatabase = "customers.duckdb"\n'
28+
'[snapshots]\ncurrent_state_full_refresh = "allow"\n'
29+
)
30+
_RAW_CUSTOMERS_YML: str = (
31+
"sources:\n - name: raw_customers\n description: Raw customers.\n"
32+
" schema: main\n table: raw_customers\n"
33+
)
34+
_CUSTOMER_SNAPSHOT_SQL: str = (
35+
'SELECT customer_id, plan, updated_at FROM __source("raw_customers")\n'
36+
)
2637

2738

2839
@pytest.mark.parametrize(
@@ -78,19 +89,6 @@ def test_given_built_model_when_materialization_changes_then_build_replaces_rela
7889
assert rows == [(1, 250)], test_case.description
7990

8091

81-
_SNAPSHOT_PROJECT_TOML: str = (
82-
'name = "customers"\nadapter = "duckdb"\n[connection]\ndatabase = "customers.duckdb"\n'
83-
'[snapshots]\ncurrent_state_full_refresh = "allow"\n'
84-
)
85-
_RAW_CUSTOMERS_YML: str = (
86-
"sources:\n - name: raw_customers\n description: Raw customers.\n"
87-
" schema: main\n table: raw_customers\n"
88-
)
89-
_CUSTOMER_SNAPSHOT_SQL: str = (
90-
'SELECT customer_id, plan, updated_at FROM __source("raw_customers")\n'
91-
)
92-
93-
9492
@pytest.mark.parametrize(
9593
"test_case",
9694
[

‎tests/unit/src/sqlbuild/adapters/motherduck/_test_types.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ class MotherDuckConnectErrorTestCase:
3131
description: str
3232
connect_error: str
3333
expected_error_type: str
34-
expected_help_fragment: str | None
34+
expected_help: str | None

‎tests/unit/src/sqlbuild/adapters/motherduck/helpers.py‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,44 @@ class FakeDuckDbModule:
2626
Error: type[Exception] = FakeDuckDbError
2727
__version__: str = "2.0.0"
2828

29-
def __init__(self, *, connect_error: str | None = None) -> None:
29+
def __init__(self) -> None:
3030
self.connected_databases: list[str] = []
3131
self.connection: FakeDuckDbConnection = FakeDuckDbConnection()
32-
self.connect_error: str | None = connect_error
3332

3433
def connect(self, *, database: str) -> FakeDuckDbConnection:
3534
self.connected_databases.append(database)
36-
if self.connect_error is not None:
37-
raise FakeDuckDbError(self.connect_error)
3835
return self.connection
3936

4037

41-
def install_fake_duckdb_module(
42-
monkeypatch: Any, *, connect_error: str | None = None
43-
) -> FakeDuckDbModule:
38+
class FailingFakeDuckDbModule(FakeDuckDbModule):
39+
"""Module double whose duckdb.connect raises a DuckDB error."""
40+
41+
def __init__(self, *, connect_error: str) -> None:
42+
super().__init__()
43+
self.connect_error: str = connect_error
44+
45+
def connect(self, *, database: str) -> FakeDuckDbConnection:
46+
self.connected_databases.append(database)
47+
raise FakeDuckDbError(self.connect_error)
48+
49+
50+
def install_failing_fake_duckdb_module(
51+
monkeypatch: Any, *, connect_error: str
52+
) -> FailingFakeDuckDbModule:
53+
"""Install a fake duckdb module whose connect fails with the given message."""
54+
55+
import sys
56+
57+
fake_module: FailingFakeDuckDbModule = FailingFakeDuckDbModule(connect_error=connect_error)
58+
monkeypatch.setitem(sys.modules, "duckdb", fake_module)
59+
return fake_module
60+
61+
62+
def install_fake_duckdb_module(monkeypatch: Any) -> FakeDuckDbModule:
4463
"""Install a fake duckdb module for adapter connection tests."""
4564

4665
import sys
4766

48-
fake_module: FakeDuckDbModule = FakeDuckDbModule(connect_error=connect_error)
67+
fake_module: FakeDuckDbModule = FakeDuckDbModule()
4968
monkeypatch.setitem(sys.modules, "duckdb", fake_module)
5069
return fake_module

‎tests/unit/src/sqlbuild/adapters/motherduck/test_client.py‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
from sqlbuild.adapters.motherduck.classes.motherduck_adapter import MotherDuckAdapter
6+
from sqlbuild.adapters.motherduck.constants import MOTHERDUCK_UNSUPPORTED_DUCKDB_HELP
67
from tests.unit.src.sqlbuild.adapters.motherduck._test_types import (
78
MotherDuckAdapterDefaultsTestCase,
89
MotherDuckConnectErrorTestCase,
@@ -11,6 +12,7 @@
1112
)
1213
from tests.unit.src.sqlbuild.adapters.motherduck.helpers import (
1314
FakeDuckDbModule,
15+
install_failing_fake_duckdb_module,
1416
install_fake_duckdb_module,
1517
)
1618

@@ -68,13 +70,13 @@ def test_given_connection_config_when_connecting_then_uses_motherduck_database(
6870
'"Your DuckDB version (v2.0.0) is not yet supported by MotherDuck."'
6971
),
7072
expected_error_type="AdapterUserError",
71-
expected_help_fragment="stay on the last SQLBuild release that supports DuckDB 1.5",
73+
expected_help=MOTHERDUCK_UNSUPPORTED_DUCKDB_HELP,
7274
),
7375
MotherDuckConnectErrorTestCase(
7476
description="any other connection error is raised unchanged",
7577
connect_error="IO Error: unauthorized",
7678
expected_error_type="FakeDuckDbError",
77-
expected_help_fragment=None,
79+
expected_help=None,
7880
),
7981
],
8082
ids=lambda case: case.description,
@@ -84,17 +86,13 @@ def test_given_failing_motherduck_connect_when_connecting_then_unsupported_versi
8486
monkeypatch: pytest.MonkeyPatch,
8587
) -> None:
8688
adapter: MotherDuckAdapter = MotherDuckAdapter()
87-
_ = install_fake_duckdb_module(monkeypatch, connect_error=test_case.connect_error)
89+
_ = install_failing_fake_duckdb_module(monkeypatch, connect_error=test_case.connect_error)
8890

8991
with pytest.raises(Exception) as raised:
9092
adapter.connect({"database": "md:analytics"})
9193

9294
assert type(raised.value).__name__ == test_case.expected_error_type
93-
help_text: object = getattr(raised.value, "help", None)
94-
if test_case.expected_help_fragment is None:
95-
assert help_text is None
96-
else:
97-
assert isinstance(help_text, str) and test_case.expected_help_fragment in help_text
95+
assert getattr(raised.value, "help", None) == test_case.expected_help
9896

9997

10098
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)