Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use test materialization for schema/generic tests #3286

Merged
merged 3 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- Add a new materialization for tests, update data tests to use test materialization when executing. ([#3154](https:/fishtown-analytics/dbt/issues/3154), [#3181](https:/fishtown-analytics/dbt/pull/3181))
- Switch from externally storing parsing state in ParseResult object to using Manifest ([#3163](http:/fishtown-analytics/dbt/issues/3163), [#3219](https:/fishtown-analytics/dbt/pull/3219))
- Switch from loading project files in separate parsers to loading in one place([#3244](http:/fishtown-analytics/dbt/issues/3244), [#3248](https:/fishtown-analytics/dbt/pull/3248))
- Update schema/generic tests to use test materialization when executing. ([#3192](https:/fishtown-analytics/dbt/issues/3192), [#3286](https:/fishtown-analytics/dbt/pull/3286))

Contributors:
- [@yu-iskw](https:/yu-iskw) ([#2928](https:/fishtown-analytics/dbt/pull/2928))
Expand Down
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 %}
13 changes: 8 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,15 @@
{% 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
from (select true) as nothing
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') }
Loading