diff --git a/elementary/clients/dbt/command_line_dbt_runner.py b/elementary/clients/dbt/command_line_dbt_runner.py index 97dba35fe..0f016516d 100644 --- a/elementary/clients/dbt/command_line_dbt_runner.py +++ b/elementary/clients/dbt/command_line_dbt_runner.py @@ -1,5 +1,6 @@ import json import os +import re from dataclasses import dataclass from typing import Any, Dict, List, Optional @@ -14,6 +15,10 @@ logger = get_logger(__name__) +MACRO_RESULT_PATTERN = re.compile( + "Elementary: --ELEMENTARY-MACRO-OUTPUT-START--(.*)--ELEMENTARY-MACRO-OUTPUT-END--" +) + @dataclass class DbtCommandResult: @@ -22,8 +27,6 @@ class DbtCommandResult: class CommandLineDbtRunner(BaseDbtRunner): - ELEMENTARY_LOG_PREFIX = "Elementary: " - def __init__( self, project_dir: str, @@ -185,10 +188,12 @@ def run_operation( if log_errors and log.level == "error": logger.error(log.msg) continue - if log.msg and log.msg.startswith(self.ELEMENTARY_LOG_PREFIX): - run_operation_results.append( - log.msg[len(self.ELEMENTARY_LOG_PREFIX) :] - ) + + if log.msg: + match = MACRO_RESULT_PATTERN.match(log.msg) + if match: + run_operation_results.append(match.group(1)) + return run_operation_results def run( diff --git a/tests/tests_with_db/dbt_project/macros/dummy_macro.sql b/tests/tests_with_db/dbt_project/macros/dummy_macro.sql new file mode 100644 index 000000000..5a60a020f --- /dev/null +++ b/tests/tests_with_db/dbt_project/macros/dummy_macro.sql @@ -0,0 +1,5 @@ +{% macro dummy_macro() %} + {# Validate that logs in the macro don't fail the dbt runner #} + {% do elementary.edr_log('Amazing macro, so happy to be here') %} + {% do return({'goodbye': 'toodleoo'}) %} +{% endmacro %} \ No newline at end of file diff --git a/tests/tests_with_db/pytest.ini b/tests/tests_with_db/pytest.ini index e7b9cd06f..de72fb22f 100644 --- a/tests/tests_with_db/pytest.ini +++ b/tests/tests_with_db/pytest.ini @@ -1,4 +1,5 @@ [pytest] +addopts = --ignore=dbt_project markers = skip_targets(targets): skip test for the given targets only_on_targets(targets): skip test for non given targets diff --git a/tests/tests_with_db/test_dbt_runner.py b/tests/tests_with_db/test_dbt_runner.py index 6e7960adc..b3ebbdd01 100644 --- a/tests/tests_with_db/test_dbt_runner.py +++ b/tests/tests_with_db/test_dbt_runner.py @@ -10,10 +10,10 @@ class BaseDbtRunnerTest: def test_run_operation(self, custom_dbt_runner: BaseDbtRunner): - result = self._run_query( - custom_dbt_runner, "select 1 as bla union all select 2 as bla" + result = json.loads( + custom_dbt_runner.run_operation("elementary_tests.dummy_macro")[0] ) - assert result == [{"bla": 1}, {"bla": 2}] + assert result == {"goodbye": "toodleoo"} def test_ls(self, custom_dbt_runner: BaseDbtRunner): result = custom_dbt_runner.ls()