Skip to content

Commit

Permalink
Add support for disabling schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jtcohen6 committed Apr 13, 2021
1 parent 749f873 commit 2fa54be
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
34 changes: 24 additions & 10 deletions core/dbt/parser/schema_test_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ class TestBuilder(Generic[Testable]):
r'((?P<test_namespace>([a-zA-Z_][0-9a-zA-Z_]*))\.)?'
r'(?P<test_name>([a-zA-Z_][0-9a-zA-Z_]*))'
)
# map magic keys to default values
MODIFIER_ARGS = {'severity': 'ERROR', 'tags': []}
# kwargs representing test configs
MODIFIER_ARGS = ('severity', 'tags', 'enabled')

def __init__(
self,
Expand Down Expand Up @@ -217,11 +217,12 @@ def __init__(
self.name: str = groups['test_name']
self.namespace: str = groups['test_namespace']
self.modifiers: Dict[str, Any] = {}
for key, default in self.MODIFIER_ARGS.items():
value = self.args.pop(key, default)
for key in self.MODIFIER_ARGS:
value = self.args.pop(key, None)
if isinstance(value, str):
value = get_rendered(value, render_ctx)
self.modifiers[key] = value
if value is not None:
self.modifiers[key] = value

if self.namespace is not None:
self.package_name = self.namespace
Expand Down Expand Up @@ -267,8 +268,15 @@ def extract_test_args(test, name=None) -> Tuple[str, Dict[str, Any]]:
test_args['column_name'] = name
return test_name, test_args

def severity(self) -> str:
return self.modifiers.get('severity', 'ERROR').upper()
def enabled(self) -> [bool, None]:
return self.modifiers.get('enabled')

def severity(self) -> [str, None]:
sev = self.modifiers.get('severity')
if sev:
return sev.upper()
else:
return None

def tags(self) -> List[str]:
tags = self.modifiers.get('tags', [])
Expand Down Expand Up @@ -303,15 +311,21 @@ def get_test_name(self) -> Tuple[str, str]:
name = '{}_{}'.format(self.namespace, name)
return get_nice_schema_test_name(name, self.target.name, self.args)

def construct_config(self) -> str:
configs = ",".join([f"{key}={value}" for key, value in self.modifiers.items()])
if configs:
return f"{{{{ config({configs}) }}}}"
else:
return ""

# this is the 'raw_sql' that's used in 'render_update' and execution
# of the test macro
def build_raw_sql(self) -> str:
return (
"{{{{ config(severity='{severity}') }}}}"
"{{{{ {macro}(**{kwargs_name}) }}}}"
"{config}{{{{ {macro}(**{kwargs_name}) }}}}"
).format(
macro=self.macro_name(),
severity=self.severity(),
config=self.construct_config(),
kwargs_name=SCHEMA_TEST_KWARGS_NAME,
)

Expand Down
7 changes: 5 additions & 2 deletions core/dbt/parser/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,11 @@ def render_test_update(self, node, config, builder):
if (macro_unique_id in
['macro.dbt.test_not_null', 'macro.dbt.test_unique']):
self.update_parsed_node(node, config)
node.unrendered_config['severity'] = builder.severity()
node.config['severity'] = builder.severity()
if builder.severity():
node.unrendered_config['severity'] = builder.severity()
node.config['severity'] = builder.severity()
if builder.enabled():
node.config['enabled'] = builder.enabled()
# source node tests are processed at patch_source time
if isinstance(builder.target, UnpatchedSourceDefinition):
sources = [builder.target.fqn[-2], builder.target.fqn[-1]]
Expand Down

0 comments on commit 2fa54be

Please sign in to comment.