Skip to content

Commit e84312b

Browse files
use console instead of print; adapt tests
1 parent 364b888 commit e84312b

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

sqlmesh/dbt/builtin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from ruamel.yaml import YAMLError
1515
from sqlglot import Dialect
1616

17+
from sqlmesh.core.console import get_console
1718
from sqlmesh.core.engine_adapter import EngineAdapter
1819
from sqlmesh.core.snapshot.definition import DeployabilityIndex
1920
from sqlmesh.dbt.adapter import BaseAdapter, ParsetimeAdapter, RuntimeAdapter
@@ -173,7 +174,7 @@ def log(msg: str, info: bool = False) -> str:
173174
if info:
174175
# Write to both log file and stdout
175176
logger.info(msg)
176-
print(msg)
177+
get_console().log_status_update(msg)
177178
else:
178179
logger.debug(msg)
179180

tests/dbt/test_transformation.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -856,37 +856,47 @@ def test_run_query(sushi_test_project: Project, runtime_renderer: t.Callable):
856856

857857

858858
@pytest.mark.xdist_group("dbt_manifest")
859-
def test_logging(sushi_test_project: Project, runtime_renderer: t.Callable, capsys):
859+
def test_logging(sushi_test_project: Project, runtime_renderer: t.Callable):
860860
context = sushi_test_project.context
861861
assert context.target
862862
engine_adapter = context.target.to_sqlmesh().create_engine_adapter()
863863
renderer = runtime_renderer(context, engine_adapter=engine_adapter)
864864

865865
logger = logging.getLogger("sqlmesh.dbt.builtin")
866866

867-
# Test log with info=False (default) which should only log with debug
868-
with patch.object(logger, "debug") as mock_debug, patch.object(logger, "info") as mock_info:
867+
# Test log with info=False (default), should only log to file with debug and not to console
868+
with (
869+
patch.object(logger, "debug") as mock_debug,
870+
patch.object(logger, "info") as mock_info,
871+
patch.object(get_console(), "log_status_update") as mock_console,
872+
):
869873
assert renderer('{{ log("foo") }}') == ""
870874
mock_debug.assert_called_once()
871875
assert "foo" in mock_debug.call_args[0][0]
872876
mock_info.assert_not_called()
873-
874-
captured = capsys.readouterr()
875-
assert "foo" not in captured.out
876-
877-
# Test log with info=True, now should log with info and also print to stdout
878-
with patch.object(logger, "debug") as mock_debug, patch.object(logger, "info") as mock_info:
877+
mock_console.assert_not_called()
878+
879+
# Test log with info=True, should log to info and also call log_status_update
880+
with (
881+
patch.object(logger, "debug") as mock_debug,
882+
patch.object(logger, "info") as mock_info,
883+
patch.object(get_console(), "log_status_update") as mock_console,
884+
):
879885
assert renderer('{{ log("output to be logged with info", info=true) }}') == ""
880886
mock_info.assert_called_once()
881887
assert "output to be logged with info" in mock_info.call_args[0][0]
882888
mock_debug.assert_not_called()
883-
884-
captured = capsys.readouterr()
885-
assert "output to be logged with info" in captured.out
886-
887-
with patch.object(logger, "debug") as mock_logger:
889+
mock_console.assert_called_once()
890+
assert "output to be logged with info" in mock_console.call_args[0][0]
891+
892+
# Test print function as well, should use debug
893+
with (
894+
patch.object(logger, "debug") as mock_logger,
895+
patch.object(get_console(), "log_status_update") as mock_console,
896+
):
888897
assert renderer('{{ print("bar") }}') == ""
889-
assert "bar" in mock_logger.call_args[0][0]
898+
assert "bar" in mock_logger.call_args[0][0]
899+
mock_console.assert_not_called()
890900

891901

892902
@pytest.mark.xdist_group("dbt_manifest")

0 commit comments

Comments
 (0)