Skip to content

Commit

Permalink
use test materialization for schema/generic tests, update integration…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
Kyle Wigley committed Apr 22, 2021
1 parent 33dc970 commit ac8cd78
Show file tree
Hide file tree
Showing 31 changed files with 70 additions and 418 deletions.
2 changes: 1 addition & 1 deletion core/dbt/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from dbt.context.providers import generate_runtime_model
from dbt.contracts.graph.manifest import Manifest
from dbt.contracts.graph.compiled import (
CompiledSchemaTestNode,
COMPILED_TYPES,
CompiledSchemaTestNode,
GraphMemberNode,
InjectedCTE,
ManifestNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ validation_errors as (
)
)

select count(*) as validation_errors
select *
from validation_errors

{% endmacro %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{% set column_name = kwargs.get('column_name', kwargs.get('arg')) %}

select count(*) as validation_errors
select *
from {{ model }}
where {{ column_name }} is null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% set column_name = kwargs.get('column_name', kwargs.get('from')) %}


select count(*) as validation_errors
select *
from (
select {{ column_name }} as id from {{ model }}
) as child
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{% set column_name = kwargs.get('column_name', kwargs.get('arg')) %}

select count(*) as validation_errors
select *
from (

select
Expand Down
45 changes: 6 additions & 39 deletions core/dbt/task/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import threading
from typing import Dict, Any, Set
from typing import Dict, Any, Set, Union

from .compile import CompileRunner
from .run import RunTask
Expand All @@ -11,15 +11,10 @@
CompiledTestNode,
)
from dbt.contracts.graph.manifest import Manifest
from dbt.contracts.graph.parsed import (
ParsedDataTestNode,
ParsedSchemaTestNode,
)
from dbt.contracts.results import RunResult, TestStatus
from dbt.context.providers import generate_runtime_model
from dbt.clients.jinja import MacroGenerator
from dbt.exceptions import (
raise_compiler_error,
InternalException,
missing_materialization
)
Expand Down Expand Up @@ -47,29 +42,12 @@ def print_start_line(self):
description = self.describe_node()
print_start_line(description, self.node_index, self.num_nodes)

def execute_schema_test(self, test: CompiledSchemaTestNode):
_, table = self.adapter.execute(
test.compiled_sql,
auto_begin=True,
fetch=True,
)

num_rows = len(table.rows)
if num_rows != 1:
num_cols = len(table.columns)
raise_compiler_error(
f"Bad test {test.test_metadata.name}: "
f"Returned {num_rows} rows and {num_cols} cols, but expected "
f"1 row and 1 column"
)
return table[0][0]

def before_execute(self):
self.print_start_line()

def execute_data_test(
def execute_test(
self,
test: CompiledDataTestNode,
test: Union[CompiledDataTestNode, CompiledSchemaTestNode],
manifest: Manifest
) -> int:
context = generate_runtime_model(
Expand All @@ -79,7 +57,8 @@ def execute_data_test(
materialization_macro = manifest.find_materialization_macro_by_name(
self.config.project_name,
test.get_materialization(),
self.adapter.type())
self.adapter.type()
)

if materialization_macro is None:
missing_materialization(test, self.adapter.type())
Expand Down Expand Up @@ -112,15 +91,7 @@ def execute_data_test(
return int(table[0][0])

def execute(self, test: CompiledTestNode, manifest: Manifest):
if isinstance(test, CompiledDataTestNode):
failed_rows = self.execute_data_test(test, manifest)
elif isinstance(test, CompiledSchemaTestNode):
failed_rows = self.execute_schema_test(test)
else:
raise InternalException(
f'Expected compiled schema test or compiled data test, got '
f'{type(test)}'
)
failed_rows = self.execute_test(test, manifest)

severity = test.config.severity.upper()
thread_id = threading.current_thread().name
Expand All @@ -146,10 +117,6 @@ def after_execute(self, result):
self.print_result_line(result)


DATA_TEST_TYPES = (CompiledDataTestNode, ParsedDataTestNode)
SCHEMA_TEST_TYPES = (CompiledSchemaTestNode, ParsedSchemaTestNode)


class TestSelector(ResourceTypeSelector):
def __init__(self, graph, manifest, previous_state):
super().__init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
mostly copy+pasted from dbt_utils, but I removed some parameters and added
a query that calls get_snapshot_unique_id
#}
{% macro test_mutually_exclusive_ranges(model) %}
{% test mutually_exclusive_ranges(model) %}

with base as (
select {{ get_snapshot_unique_id() }} as dbt_unique_id,
Expand Down Expand Up @@ -81,5 +81,5 @@ validation_errors as (
)
)

select count(*) from validation_errors
{% endmacro %}
select * from validation_errors
{% endtest %}
12 changes: 7 additions & 5 deletions test/integration/005_simple_seed_test/macros/schema_test.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

{% macro test_column_type(model, column_name, type) %}
{% test column_type(model, column_name, type) %}

{% set cols = adapter.get_columns_in_relation(model) %}

Expand All @@ -8,12 +8,14 @@
{% do col_types.update({col.name: col.data_type}) %}
{% endfor %}

{% set validation_message = 'Got a column type of ' ~ col_types.get(column_name) ~ ', expected ' ~ type %}

{% set val = 0 if col_types.get(column_name) == type else 1 %}
{% if val == 1 and execute %}
{# I'm so tired of guessing what's wrong, let's just log it #}
{{ log('Got a column type of ' ~ col_types.get(column_name) ~ ', expected ' ~ type, info=True) }}
{{ log(validation_message, info=True) }}
{% endif %}

select {{ val }} as pass_fail
select '{{ validation_message }}' as validation_error
where {{ val }} = 1

{% endmacro %}
{% endtest %}

This file was deleted.

This file was deleted.

41 changes: 17 additions & 24 deletions test/integration/008_schema_tests_test/macros-v2/macros/tests.sql
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
{% test every_value_is_blue(model, column_name) %}


{% macro test_every_value_is_blue(model, column_name) %}

select
count(*)

select *
from {{ model }}
where {{ column_name }} != 'blue'

{% endmacro %}
{% endtest %}


{% macro test_rejected_values(model, column_name, values) %}

select
count(*)
{% test rejected_values(model, column_name, values) %}

select *
from {{ model }}
where {{ column_name }} in (
{% for value in values %}
'{{ value }}' {% if not loop.last %} , {% endif %}
{% endfor %}
)

{% endmacro %}
{% endtest %}


{% macro test_equivalent(model, value) %}
{% test equivalent(model, value) %}
{% set expected = 'foo-bar' %}
select
{% if value == expected %}
0
{% else %}
{% set msg -%}
got "{{ value }}", expected "{{ expected }}"
{%- endset %}
{% do log(msg, info=True) %}
1
{% set eq = 1 if value == expected else 0 %}
{% set validation_message -%}
'got "{{ value }}", expected "{{ expected }}"'
{%- endset %}
{% if eq == 0 and execute %}
{{ log(validation_message, info=True) }}
{% endif %}
as id
{% endmacro %}

select {{ validation_message }} as validation_error
where {{ eq }} = 0
{% endtest %}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,3 @@ models:
tests:
- every_value_is_blue
- rejected_values: { values: ['orange', 'purple'] }
# passes
tests:
- local_dep.equality: { compare_model: ref('table_copy') }
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{% macro test_type_one(model) %}
{% test type_one(model) %}

select count(*) from (
select * from (

select * from {{ model }}
union all
select * from {{ ref('model_b') }}

) as Foo

{% endmacro %}
{% endtest %}

{% macro test_type_two(model) %}
{% test type_two(model) %}

{{ config(severity = "WARN") }}

select count(*) from {{ model }}
select * from {{ model }}

{% endmacro %}
{% endtest %}
Loading

0 comments on commit ac8cd78

Please sign in to comment.