Skip to content

Commit

Permalink
Merge pull request #1519 from fishtown-analytics/feature/render-sever…
Browse files Browse the repository at this point in the history
…ity-configs

render severity configurations (#1512) (#1511)
  • Loading branch information
beckjake authored Jun 13, 2019
2 parents 03aecc8 + a4be1e1 commit e57c7b6
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 21 deletions.
13 changes: 8 additions & 5 deletions core/dbt/clients/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,14 @@ def _fail_or_warn_with_undefined_error(self, *args, **kwargs):
raise
else:
msg = 'Compilation warning in {}:\n\t{}'
model_desc = "{} {} ({})".format(
self.node.get('resource_type'),
self.node.get('name', 'unknown'),
self.node.get('original_file_path')
)
if self.node is None:
model_desc = 'rendering'
else:
model_desc = "{} {} ({})".format(
self.node.get('resource_type'),
self.node.get('name', 'unknown'),
self.node.get('original_file_path')
)
logger.warning(msg.format(model_desc, str(exc)))

def __eq__(self, other):
Expand Down
3 changes: 2 additions & 1 deletion core/dbt/contracts/graph/parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
]
},
'severity': {
'enum': ['ERROR', 'WARN'],
'type': 'string',
'pattern': '([eE][rR][rR][oO][rR]|[wW][aA][rR][nN])',
},
},
'required': [
Expand Down
13 changes: 10 additions & 3 deletions core/dbt/parser/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import dbt.context.parser
import dbt.contracts.project

from dbt.context.common import generate_config_context
from dbt.clients.jinja import get_rendered
from dbt.node_types import NodeType
from dbt.compat import basestring, to_string
Expand Down Expand Up @@ -84,7 +85,7 @@ class TestBuilder(object):
# map magic keys to default values
MODIFIER_ARGS = {'severity': 'ERROR'}

def __init__(self, test, target, column_name, package_name):
def __init__(self, test, target, column_name, package_name, render_ctx):
test_name, test_args = self.extract_test_args(test, column_name)
self.args = test_args
self.package_name = package_name
Expand All @@ -102,7 +103,10 @@ def __init__(self, test, target, column_name, package_name):
self.namespace = groups['test_namespace']
self.modifiers = {}
for key, default in self.MODIFIER_ARGS.items():
self.modifiers[key] = self.args.pop(key, default)
value = self.args.pop(key, default)
if isinstance(value, basestring):
value = get_rendered(value, render_ctx)
self.modifiers[key] = value

if self.namespace is not None:
self.package_name = self.namespace
Expand Down Expand Up @@ -283,7 +287,10 @@ def build_test_node(self, test_target, package_name, test, root_dir, path,
if isinstance(test, basestring):
test = {test: {}}

test_info = self.Builder(test, test_target, column_name, package_name)
ctx = generate_config_context(self.root_project_config.cli_vars)

test_info = self.Builder(test, test_target, column_name, package_name,
ctx)

source_package = self.all_projects.get(test_info.package_name)
if source_package is None:
Expand Down
3 changes: 2 additions & 1 deletion core/dbt/ui/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ def print_test_result_line(result, schema_name, index, total):
color = red

elif result.status > 0:
if result.node.config['severity'] == 'ERROR' or dbt.flags.WARN_ERROR:
severity = result.node.config['severity'].upper()
if severity == 'ERROR' or dbt.flags.WARN_ERROR:
info = 'FAIL {}'.format(result.status)
color = red
result.fail = True
Expand Down
4 changes: 2 additions & 2 deletions test/integration/045_test_severity_tests/models/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ models:
- name: email
tests:
- not_null:
severity: WARN
severity: "{{ 'error' if var('strict', false) else 'warn' }}"
sources:
- name: source
schema: "{{ var('test_run_schema') }}"
Expand All @@ -16,4 +16,4 @@ sources:
- name: email
tests:
- not_null:
severity: WARN
severity: "{{ 'error' if var('strict', false) else 'warn' }}"
57 changes: 48 additions & 9 deletions test/integration/045_test_severity_tests/test_severity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,70 @@ def models(self):
def project_config(self):
return {
'data-paths': ['data'],
'test-paths': ['tests'],
}

def run_dbt_with_vars(self, cmd, *args, **kwargs):
def run_dbt_with_vars(self, cmd, strict_var, *args, **kwargs):
cmd.extend(['--vars',
'{{test_run_schema: {}}}'.format(self.unique_schema())])
'{{test_run_schema: {}, strict: {}}}'.format(self.unique_schema(), strict_var)])
return self.run_dbt(cmd, *args, **kwargs)

@use_profile('postgres')
def test_postgres_severity_warnings(self):
self.run_dbt_with_vars(['seed'], strict=False)
self.run_dbt_with_vars(['run'], strict=False)
results = self.run_dbt_with_vars(['test'], strict=False)
self.run_dbt_with_vars(['seed'], 'false', strict=False)
self.run_dbt_with_vars(['run'], 'false', strict=False)
results = self.run_dbt_with_vars(['test', '--schema'], 'false', strict=False)
self.assertEqual(len(results), 2)
self.assertFalse(results[0].fail)
self.assertEqual(results[0].status, 2)
self.assertFalse(results[1].fail)
self.assertEqual(results[1].status, 2)

@use_profile('postgres')
def test_postgres_severity_warnings_errors(self):
self.run_dbt_with_vars(['seed'], strict=False)
self.run_dbt_with_vars(['run'], strict=False)
results = self.run_dbt_with_vars(['test'], expect_pass=False)
def test_postgres_severity_rendered_errors(self):
self.run_dbt_with_vars(['seed'], 'false', strict=False)
self.run_dbt_with_vars(['run'], 'false', strict=False)
results = self.run_dbt_with_vars(['test', '--schema'], 'true', strict=False, expect_pass=False)
self.assertEqual(len(results), 2)
self.assertTrue(results[0].fail)
self.assertEqual(results[0].status, 2)
self.assertTrue(results[1].fail)
self.assertEqual(results[1].status, 2)

@use_profile('postgres')
def test_postgres_severity_warnings_strict(self):
self.run_dbt_with_vars(['seed'], 'false', strict=False)
self.run_dbt_with_vars(['run'], 'false', strict=False)
results = self.run_dbt_with_vars(['test', '--schema'], 'false', expect_pass=False)
self.assertEqual(len(results), 2)
self.assertTrue(results[0].fail)
self.assertEqual(results[0].status, 2)
self.assertTrue(results[1].fail)
self.assertEqual(results[1].status, 2)

@use_profile('postgres')
def test_postgres_data_severity_warnings(self):
self.run_dbt_with_vars(['seed'], 'false', strict=False)
self.run_dbt_with_vars(['run'], 'false', strict=False)
results = self.run_dbt_with_vars(['test', '--data'], 'false', strict=False)
self.assertEqual(len(results), 1)
self.assertFalse(results[0].fail)
self.assertEqual(results[0].status, 2)

@use_profile('postgres')
def test_postgres_data_severity_rendered_errors(self):
self.run_dbt_with_vars(['seed'], 'false', strict=False)
self.run_dbt_with_vars(['run'], 'false', strict=False)
results = self.run_dbt_with_vars(['test', '--data'], 'true', strict=False, expect_pass=False)
self.assertEqual(len(results), 1)
self.assertTrue(results[0].fail)
self.assertEqual(results[0].status, 2)

@use_profile('postgres')
def test_postgres_data_severity_warnings_strict(self):
self.run_dbt_with_vars(['seed'], 'false', strict=False)
self.run_dbt_with_vars(['run'], 'false', strict=False)
results = self.run_dbt_with_vars(['test', '--data'], 'false', expect_pass=False)
self.assertEqual(len(results), 1)
self.assertTrue(results[0].fail)
self.assertEqual(results[0].status, 2)
2 changes: 2 additions & 0 deletions test/integration/045_test_severity_tests/tests/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ config(severity='error' if var('strict', false) else 'warn') }}
select * from {{ ref('model') }} where email is null

0 comments on commit e57c7b6

Please sign in to comment.