22from pathlib import Path
33import pytest
44from click .testing import Result
5+ from unittest .mock import patch
6+ from sqlmesh .utils .errors import SQLMeshError
7+ from sqlglot .errors import SqlglotError
58
69pytestmark = pytest .mark .slow
710
@@ -28,3 +31,61 @@ def test_profile_and_target(jaffle_shop_duckdb: Path, invoke_cli: t.Callable[...
2831 result = invoke_cli (["--profile" , "jaffle_shop" , "--target" , "dev" ])
2932 assert result .exit_code == 0
3033 assert "No command specified" in result .output
34+
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" )
39+
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" )
48+
49+ result = invoke_cli (["run" ])
50+
51+ assert result .exit_code == 1
52+ assert "Error: Invalid SQL syntax" in result .output
53+ assert "Traceback" not in result .output
54+
55+ with patch ("sqlmesh_dbt.operations.DbtOperations.run" ) as mock_run :
56+ mock_run .side_effect = ValueError ("Invalid configuration value" )
57+
58+ result = invoke_cli (["run" ])
59+
60+ assert result .exit_code == 1
61+ assert "Error: Invalid configuration value" in result .output
62+ assert "Traceback" not in result .output
63+
64+ with patch ("sqlmesh_dbt.operations.DbtOperations.list_" ) as mock_list :
65+ mock_list .side_effect = SQLMeshError ("List command error" )
66+
67+ result = invoke_cli (["list" ])
68+
69+ assert result .exit_code == 1
70+ assert "Error: List command error" in result .output
71+ assert "Traceback" not in result .output
72+
73+ with patch ("sqlmesh_dbt.cli.create" ) as mock_create :
74+ mock_create .side_effect = SQLMeshError ("Failed to load project" )
75+
76+ # use without subcommand
77+ result = invoke_cli (["--profile" , "jaffle_shop" ])
78+
79+ assert result .exit_code == 1
80+ assert "Error: Failed to load project" in result .output
81+ assert "Traceback" not in result .output
82+
83+ with patch ("sqlmesh_dbt.operations.DbtOperations.run" ) as mock_run :
84+ mock_run .side_effect = SQLMeshError ("Error with selector" )
85+
86+ # with select option
87+ result = invoke_cli (["run" , "--select" , "model1" ])
88+
89+ assert result .exit_code == 1
90+ assert "Error: Error with selector" in result .output
91+ assert "Traceback" not in result .output
0 commit comments