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

Fix tuple index out of range in custom schema tests #1903

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
11 changes: 6 additions & 5 deletions core/dbt/node_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,13 @@ def execute_test(self, test):
fetch=True)

num_rows = len(table.rows)
if num_rows > 1:
if num_rows != 1:
num_cols = len(table.columns)
raise RuntimeError(
"Bad test {name}: Returned {rows} rows and {cols} cols"
.format(name=test.name, rows=num_rows, cols=num_cols))

dbt.exceptions.raise_compiler_error(
f"Bad test {test.test_metadata.name}: "
f"Returned {num_rows} rows "
f"and {num_cols} cols but expected 1 row and 1 column"
)
return table[0][0]

def before_execute(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- tries to build a macro that always returns an empty set
{% macro test_empty_aggregation(model, column_name) %}

SELECT * from {{ model }} WHERE 1=0

{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2

models:
- name: table_copy
description: "A copy of the table"
columns:
- name: email
tests:
- not_null
# should throw a runtime error
tests:
- empty_aggregation
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

{{
config(
materialized='table'
)
}}

select * from {{ this.schema }}.seed
47 changes: 46 additions & 1 deletion test/integration/008_schema_tests_test/test_schema_v2_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,51 @@ def test_postgres_malformed_schema_strict_will_break_run(self):
self.run_dbt(strict=False)


class TestMalformedMacroTests(DBTIntegrationTest):

def setUp(self):
DBTIntegrationTest.setUp(self)
self.run_sql_file("seed.sql")

@property
def schema(self):
return "schema_tests_008"

@property
def models(self):
return "models-v2/custom-bad-test-macro"

@property
def project_config(self):
return {
"macro-paths": ["macros-v2/malformed"],
}

def run_schema_validations(self):
args = FakeArgs()
test_task = TestTask(args, self.config)
return test_task.run()

@use_profile('postgres')
def test_postgres_malformed_macro_reports_error(self):
self.run_dbt(["deps"])
self.run_dbt()
expected_failure = 'not_null'

test_results = self.run_schema_validations()

self.assertEqual(len(test_results), 2)

for result in test_results:
self.assertTrue(result.error is not None or result.fail)
# Assert that error is thrown for empty schema test
if result.error is not None:
self.assertIn("Returned 0 rows", result.error)
# Assert that failure occurs for normal schema test
elif result.fail:
self.assertIn(expected_failure, result.node.name)


class TestHooksInTests(DBTIntegrationTest):

@property
Expand Down Expand Up @@ -148,7 +193,7 @@ def project_config(self):
# dbt-integration-project contains a schema.yml file
# both should work!
return {
"macro-paths": ["macros-v2"],
"macro-paths": ["macros-v2/macros"],
}

@property
Expand Down