Skip to content

Commit 1585fd1

Browse files
use mockerfixture instead
1 parent 9d89e95 commit 1585fd1

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

tests/dbt/cli/test_global_flags.py

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import typing as t
22
from pathlib import Path
33
import pytest
4+
from pytest_mock import MockerFixture
45
from click.testing import Result
5-
from unittest.mock import patch
66
from sqlmesh.utils.errors import SQLMeshError
77
from sqlglot.errors import SqlglotError
88

@@ -33,59 +33,63 @@ def test_profile_and_target(jaffle_shop_duckdb: Path, invoke_cli: t.Callable[...
3333
assert "No command specified" in result.output
3434

3535

36-
def test_run_error_handler(jaffle_shop_duckdb: Path, invoke_cli: t.Callable[..., Result]):
37-
with patch("sqlmesh_dbt.operations.DbtOperations.run") as mock_run:
38-
mock_run.side_effect = SQLMeshError("Test error message")
36+
def test_run_error_handler(
37+
jaffle_shop_duckdb: Path, invoke_cli: t.Callable[..., Result], mocker: MockerFixture
38+
) -> None:
39+
mock_run = mocker.patch("sqlmesh_dbt.operations.DbtOperations.run")
40+
mock_run.side_effect = SQLMeshError("Test error message")
3941

40-
result = invoke_cli(["run"])
41-
42-
# tesg that SQLMeshError are handled gracefully in run command
43-
assert result.exit_code == 1
44-
assert "Traceback" not in result.output
45-
46-
with patch("sqlmesh_dbt.operations.DbtOperations.run") as mock_run:
47-
mock_run.side_effect = SqlglotError("Invalid SQL syntax")
42+
result = invoke_cli(["run"])
43+
assert result.exit_code == 1
44+
assert "Error: Test error message" in result.output
45+
assert "Traceback" not in result.output
4846

49-
result = invoke_cli(["run"])
47+
# test SqlglotError in run command
48+
mock_run = mocker.patch("sqlmesh_dbt.operations.DbtOperations.run")
49+
mock_run.side_effect = SqlglotError("Invalid SQL syntax")
5050

51-
assert result.exit_code == 1
52-
assert "Error: Invalid SQL syntax" in result.output
53-
assert "Traceback" not in result.output
51+
result = invoke_cli(["run"])
5452

55-
with patch("sqlmesh_dbt.operations.DbtOperations.run") as mock_run:
56-
mock_run.side_effect = ValueError("Invalid configuration value")
53+
assert result.exit_code == 1
54+
assert "Error: Invalid SQL syntax" in result.output
55+
assert "Traceback" not in result.output
5756

58-
result = invoke_cli(["run"])
57+
# test ValueError in run command
58+
mock_run = mocker.patch("sqlmesh_dbt.operations.DbtOperations.run")
59+
mock_run.side_effect = ValueError("Invalid configuration value")
5960

60-
assert result.exit_code == 1
61-
assert "Error: Invalid configuration value" in result.output
62-
assert "Traceback" not in result.output
61+
result = invoke_cli(["run"])
6362

64-
with patch("sqlmesh_dbt.operations.DbtOperations.list_") as mock_list:
65-
mock_list.side_effect = SQLMeshError("List command error")
63+
assert result.exit_code == 1
64+
assert "Error: Invalid configuration value" in result.output
65+
assert "Traceback" not in result.output
6666

67-
result = invoke_cli(["list"])
67+
# test SQLMeshError in list command
68+
mock_list = mocker.patch("sqlmesh_dbt.operations.DbtOperations.list_")
69+
mock_list.side_effect = SQLMeshError("List command error")
6870

69-
assert result.exit_code == 1
70-
assert "Error: List command error" in result.output
71-
assert "Traceback" not in result.output
71+
result = invoke_cli(["list"])
7272

73-
with patch("sqlmesh_dbt.cli.create") as mock_create:
74-
mock_create.side_effect = SQLMeshError("Failed to load project")
73+
assert result.exit_code == 1
74+
assert "Error: List command error" in result.output
75+
assert "Traceback" not in result.output
7576

76-
# use without subcommand
77-
result = invoke_cli(["--profile", "jaffle_shop"])
77+
# test SQLMeshError in main command wuthout subcommand
78+
mock_create = mocker.patch("sqlmesh_dbt.cli.create")
79+
mock_create.side_effect = SQLMeshError("Failed to load project")
80+
result = invoke_cli(["--profile", "jaffle_shop"])
7881

79-
assert result.exit_code == 1
80-
assert "Error: Failed to load project" in result.output
81-
assert "Traceback" not in result.output
82+
assert result.exit_code == 1
83+
assert "Error: Failed to load project" in result.output
84+
assert "Traceback" not in result.output
85+
mocker.stopall()
8286

83-
with patch("sqlmesh_dbt.operations.DbtOperations.run") as mock_run:
84-
mock_run.side_effect = SQLMeshError("Error with selector")
87+
# test error with select option
88+
mock_run_select = mocker.patch("sqlmesh_dbt.operations.DbtOperations.run")
89+
mock_run_select.side_effect = SQLMeshError("Error with selector")
8590

86-
# with select option
87-
result = invoke_cli(["run", "--select", "model1"])
91+
result = invoke_cli(["run", "--select", "model1"])
8892

89-
assert result.exit_code == 1
90-
assert "Error: Error with selector" in result.output
91-
assert "Traceback" not in result.output
93+
assert result.exit_code == 1
94+
assert "Error: Error with selector" in result.output
95+
assert "Traceback" not in result.output

0 commit comments

Comments
 (0)