|
1 | 1 | import typing as t |
2 | 2 | from pathlib import Path |
3 | 3 | import pytest |
| 4 | +from pytest_mock import MockerFixture |
4 | 5 | from click.testing import Result |
5 | | -from unittest.mock import patch |
6 | 6 | from sqlmesh.utils.errors import SQLMeshError |
7 | 7 | from sqlglot.errors import SqlglotError |
8 | 8 |
|
@@ -33,59 +33,63 @@ def test_profile_and_target(jaffle_shop_duckdb: Path, invoke_cli: t.Callable[... |
33 | 33 | assert "No command specified" in result.output |
34 | 34 |
|
35 | 35 |
|
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") |
39 | 41 |
|
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 |
48 | 46 |
|
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") |
50 | 50 |
|
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"]) |
54 | 52 |
|
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 |
57 | 56 |
|
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") |
59 | 60 |
|
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"]) |
63 | 62 |
|
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 |
66 | 66 |
|
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") |
68 | 70 |
|
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"]) |
72 | 72 |
|
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 |
75 | 76 |
|
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"]) |
78 | 81 |
|
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() |
82 | 86 |
|
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") |
85 | 90 |
|
86 | | - # with select option |
87 | | - result = invoke_cli(["run", "--select", "model1"]) |
| 91 | + result = invoke_cli(["run", "--select", "model1"]) |
88 | 92 |
|
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