Skip to content

Commit

Permalink
add error for empty test result set
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Larsen committed Nov 8, 2019
1 parent 2d0f938 commit a703924
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
5 changes: 5 additions & 0 deletions core/dbt/node_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,11 @@ def execute_test(self, test):
raise RuntimeError(
"Bad test {name}: Returned {rows} rows and {cols} cols"
.format(name=test.name, rows=num_rows, cols=num_cols))
elif num_rows == 0:
raise RuntimeError(
"Bad test {name}: Empty result set cannot be evaluated"
.format(name=test.name)
)

return table[0][0]

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("Empty result set cannot be evaluated", 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

0 comments on commit a703924

Please sign in to comment.