Skip to content

Commit

Permalink
Merge pull request #1346 from fishtown-analytics/fix/on-run-hooks-in-…
Browse files Browse the repository at this point in the history
…tests

[Stephen Girard] fix for on-run- hooks running in tests
  • Loading branch information
drewbanin authored Mar 12, 2019
2 parents 7415256 + a08c075 commit 4771452
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
16 changes: 9 additions & 7 deletions core/dbt/task/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
class TestTask(RunTask):
"""
Testing:
1) Create tmp views w/ 0 rows to ensure all tables, schemas, and SQL
statements are valid
2) Read schema files and validate that constraints are satisfied
a) not null
b) uniquenss
c) referential integrity
d) accepted value
Read schema files + custom data tests and validate that
constraints are satisfied.
"""
def raise_on_first_error(self):
return False

def before_run(self, adapter, selected_uids):
# Don't execute on-run-* hooks for tests
self.populate_adapter_cache(adapter)

def after_run(self, adapter, results):
pass

def build_query(self):
query = {
"include": self.args.models,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

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

select 1 as id
8 changes: 8 additions & 0 deletions test/integration/008_schema_tests_test/ephemeral/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

version: 2
models:
- name: ephemeral
columns:
- name: id
tests:
- unique
32 changes: 32 additions & 0 deletions test/integration/008_schema_tests_test/test_schema_v2_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,38 @@ def test_malformed_schema_strict_will_break_run(self):
self.run_dbt(strict=True)


class TestHooksInTests(DBTIntegrationTest):

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

@property
def models(self):
# test ephemeral models so we don't need to do a run (which would fail)
return "test/integration/008_schema_tests_test/ephemeral"

@property
def project_config(self):
return {
"on-run-start": ["{{ exceptions.raise_compiler_error('hooks called in tests -- error') if execute }}"],
"on-run-end": ["{{ exceptions.raise_compiler_error('hooks called in tests -- error') if execute }}"],
}

@attr(type='postgres')
def test_hooks_dont_run_for_tests(self):
# This would fail if the hooks ran
results = self.run_dbt(['test', '--model', 'ephemeral'])
self.assertEqual(len(results), 1)
for result in results:
self.assertIsNone(result.error)
self.assertFalse(result.skipped)
# status = # of failing rows
self.assertEqual(
result.status, 0,
'test {} failed'.format(result.node.get('name'))
)

class TestCustomSchemaTests(DBTIntegrationTest):

def setUp(self):
Expand Down

0 comments on commit 4771452

Please sign in to comment.