From dd5e47f2e034f90e9458ed2845156afb5ed0fe23 Mon Sep 17 00:00:00 2001 From: Jacob Beck Date: Wed, 4 Dec 2019 14:10:17 -0700 Subject: [PATCH 1/2] add exceptions.emit_warning to the dbt context --- core/dbt/exceptions.py | 7 +++++++ .../emit-warning-models/warnings.sql | 2 ++ .../test_context_vars.py | 20 +++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 test/integration/013_context_var_tests/emit-warning-models/warnings.sql diff --git a/core/dbt/exceptions.py b/core/dbt/exceptions.py index 92b9d9f3fd0..3aa98ed63ee 100644 --- a/core/dbt/exceptions.py +++ b/core/dbt/exceptions.py @@ -756,12 +756,19 @@ def warn_or_raise(exc, log_fmt=None): logger.warning(msg) +def emit_warning(msg, node=None): + # there's no reason to expose log_fmt to macros - it's only useful for + # handling colors + return warn_or_error(msg, node=node) + + # Update this when a new function should be added to the # dbt context's `exceptions` key! CONTEXT_EXPORTS = { fn.__name__: fn for fn in [ + emit_warning, missing_config, missing_materialization, missing_relation, diff --git a/test/integration/013_context_var_tests/emit-warning-models/warnings.sql b/test/integration/013_context_var_tests/emit-warning-models/warnings.sql new file mode 100644 index 00000000000..c05d864a268 --- /dev/null +++ b/test/integration/013_context_var_tests/emit-warning-models/warnings.sql @@ -0,0 +1,2 @@ +{% do exceptions.emit_warning('warning: everything is terrible but not that terrible') %} +select 1 as id diff --git a/test/integration/013_context_var_tests/test_context_vars.py b/test/integration/013_context_var_tests/test_context_vars.py index e3c281b79d4..09ef5a1b3ba 100644 --- a/test/integration/013_context_var_tests/test_context_vars.py +++ b/test/integration/013_context_var_tests/test_context_vars.py @@ -2,6 +2,10 @@ import os +import pytest + +import dbt.exceptions + class TestContextVars(DBTIntegrationTest): @@ -133,3 +137,19 @@ def test_postgres_env_vars_prod(self): self.assertEqual(ctx['target.user'], 'root') self.assertEqual(ctx['target.pass'], '') self.assertEqual(ctx['env_var'], '1') + + +class TestEmitWarning(DBTIntegrationTest): + @property + def schema(self): + return "context_vars_013" + + @property + def models(self): + return "emit-warning-models" + + @use_profile('postgres') + def test_postgres_emit_warning(self): + with pytest.raises(dbt.exceptions.CompilationException): + self.run_dbt(['run'], strict=True) + self.run_dbt(['run'], strict=False, expect_pass=True) From 14c4bc7967a48de9166f41e2ba623d54d60ec80e Mon Sep 17 00:00:00 2001 From: Jacob Beck Date: Thu, 5 Dec 2019 14:26:27 -0700 Subject: [PATCH 2/2] rename emit_warning -> warn --- core/dbt/exceptions.py | 4 ++-- .../013_context_var_tests/emit-warning-models/warnings.sql | 2 +- test/integration/013_context_var_tests/test_context_vars.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/dbt/exceptions.py b/core/dbt/exceptions.py index 3aa98ed63ee..50eb7b4d438 100644 --- a/core/dbt/exceptions.py +++ b/core/dbt/exceptions.py @@ -756,7 +756,7 @@ def warn_or_raise(exc, log_fmt=None): logger.warning(msg) -def emit_warning(msg, node=None): +def warn(msg, node=None): # there's no reason to expose log_fmt to macros - it's only useful for # handling colors return warn_or_error(msg, node=node) @@ -768,7 +768,7 @@ def emit_warning(msg, node=None): fn.__name__: fn for fn in [ - emit_warning, + warn, missing_config, missing_materialization, missing_relation, diff --git a/test/integration/013_context_var_tests/emit-warning-models/warnings.sql b/test/integration/013_context_var_tests/emit-warning-models/warnings.sql index c05d864a268..1b5f33278a8 100644 --- a/test/integration/013_context_var_tests/emit-warning-models/warnings.sql +++ b/test/integration/013_context_var_tests/emit-warning-models/warnings.sql @@ -1,2 +1,2 @@ -{% do exceptions.emit_warning('warning: everything is terrible but not that terrible') %} +{% do exceptions.warn('warning: everything is terrible but not that terrible') %} select 1 as id diff --git a/test/integration/013_context_var_tests/test_context_vars.py b/test/integration/013_context_var_tests/test_context_vars.py index 09ef5a1b3ba..35c3a10c96d 100644 --- a/test/integration/013_context_var_tests/test_context_vars.py +++ b/test/integration/013_context_var_tests/test_context_vars.py @@ -149,7 +149,7 @@ def models(self): return "emit-warning-models" @use_profile('postgres') - def test_postgres_emit_warning(self): + def test_postgres_warn(self): with pytest.raises(dbt.exceptions.CompilationException): self.run_dbt(['run'], strict=True) self.run_dbt(['run'], strict=False, expect_pass=True)