Skip to content

Commit 8c3645d

Browse files
use console instead of print; adapt tests
1 parent bf10ab9 commit 8c3645d

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
@@ -847,37 +847,47 @@ def test_run_query(sushi_test_project: Project, runtime_renderer: t.Callable):
847847

848848

849849
@pytest.mark.xdist_group("dbt_manifest")
850-
def test_logging(sushi_test_project: Project, runtime_renderer: t.Callable, capsys):
850+
def test_logging(sushi_test_project: Project, runtime_renderer: t.Callable):
851851
context = sushi_test_project.context
852852
assert context.target
853853
engine_adapter = context.target.to_sqlmesh().create_engine_adapter()
854854
renderer = runtime_renderer(context, engine_adapter=engine_adapter)
855855

856856
logger = logging.getLogger("sqlmesh.dbt.builtin")
857857

858-
# Test log with info=False (default) which should only log with debug
859-
with patch.object(logger, "debug") as mock_debug, patch.object(logger, "info") as mock_info:
858+
# Test log with info=False (default), should only log to file with debug and not to console
859+
with (
860+
patch.object(logger, "debug") as mock_debug,
861+
patch.object(logger, "info") as mock_info,
862+
patch.object(get_console(), "log_status_update") as mock_console,
863+
):
860864
assert renderer('{{ log("foo") }}') == ""
861865
mock_debug.assert_called_once()
862866
assert "foo" in mock_debug.call_args[0][0]
863867
mock_info.assert_not_called()
864-
865-
captured = capsys.readouterr()
866-
assert "foo" not in captured.out
867-
868-
# Test log with info=True, now should log with info and also print to stdout
869-
with patch.object(logger, "debug") as mock_debug, patch.object(logger, "info") as mock_info:
868+
mock_console.assert_not_called()
869+
870+
# Test log with info=True, should log to info and also call log_status_update
871+
with (
872+
patch.object(logger, "debug") as mock_debug,
873+
patch.object(logger, "info") as mock_info,
874+
patch.object(get_console(), "log_status_update") as mock_console,
875+
):
870876
assert renderer('{{ log("output to be logged with info", info=true) }}') == ""
871877
mock_info.assert_called_once()
872878
assert "output to be logged with info" in mock_info.call_args[0][0]
873879
mock_debug.assert_not_called()
874-
875-
captured = capsys.readouterr()
876-
assert "output to be logged with info" in captured.out
877-
878-
with patch.object(logger, "debug") as mock_logger:
880+
mock_console.assert_called_once()
881+
assert "output to be logged with info" in mock_console.call_args[0][0]
882+
883+
# Test print function as well, should use debug
884+
with (
885+
patch.object(logger, "debug") as mock_logger,
886+
patch.object(get_console(), "log_status_update") as mock_console,
887+
):
879888
assert renderer('{{ print("bar") }}') == ""
880-
assert "bar" in mock_logger.call_args[0][0]
889+
assert "bar" in mock_logger.call_args[0][0]
890+
mock_console.assert_not_called()
881891

882892

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

0 commit comments

Comments
 (0)