Skip to content

Commit

Permalink
Enable test project hooks, flags.WHICH (dbt-labs#4004)
Browse files Browse the repository at this point in the history
* Turn on project hooks test task

* Add flags.WHICH

* Rm unused env vars (RPC)

* Add changelog entry
  • Loading branch information
jtcohen6 authored Oct 18, 2021
1 parent 80a5d27 commit 4bda8c8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
## dbt-core 1.0.0 (Release TBD)

### Breaking changes
- Enable `on-run-start` and `on-run-end` hooks for `dbt test`. Add `flags.WHICH` to execution context, representing current task ([#3463](https://github.com/dbt-labs/dbt-core/issues/3463), [#4004](https://github.com/dbt-labs/dbt-core/pull/4004))

### Under the hood
- Fix intermittent errors in partial parsing tests ([#4060](https://github.com/dbt-labs/dbt-core/issues/4060), [#4068](https://github.com/dbt-labs/dbt-core/pull/4068))
- Make finding disabled nodes more consistent ([#4069](https://github.com/dbt-labs/dbt-core/issues/4069), [#4073](https://github.com/dbt-labas/dbt-core/pull/4073))

## dbt-core 1.0.0b1 (October 11, 2021)

## dbt 1.0.0b1 (October 11, 2021)

### Breaking changes

- The two type of test definitions are now "singular" and "generic" (instead of "data" and "schema", respectively). The `test_type:` selection method accepts `test_type:singular` and `test_type:generic`. (It will also accept `test_type:schema` and `test_type:data` for backwards compatibility) ([#3234](https://github.com/dbt-labs/dbt-core/issues/3234), [#3880](https://github.com/dbt-labs/dbt-core/pull/3880)). **Not backwards compatible:** The `--data` and `--schema` flags to `dbt test` are no longer supported, and tests no longer have the tags `'data'` and `'schema'` automatically applied.
Expand Down
8 changes: 2 additions & 6 deletions core/dbt/context/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,8 @@ def flags(self) -> Any:
-- no-op
{% endif %}
The list of valid flags are:
- `flags.FULL_REFRESH`: True if `--full-refresh` was provided on the
command line
- `flags.NON_DESTRUCTIVE`: True if `--non-destructive` was provided on
the command line
This supports all flags defined in flags submodule (core/dbt/flags.py)
TODO: Replace with object that provides read-only access to flag values
"""
return flags

Expand Down
7 changes: 4 additions & 3 deletions core/dbt/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
FAIL_FAST = None
SEND_ANONYMOUS_USAGE_STATS = None
PRINTER_WIDTH = 80
WHICH = None

# Global CLI defaults. These flags are set from three places:
# CLI args, environment variables, and user_config (profiles.yml).
Expand Down Expand Up @@ -77,8 +78,6 @@ def env_set_path(key: str) -> Optional[Path]:
return Path(value)


SINGLE_THREADED_WEBSERVER = env_set_truthy('DBT_SINGLE_THREADED_WEBSERVER')
SINGLE_THREADED_HANDLER = env_set_truthy('DBT_SINGLE_THREADED_HANDLER')
MACRO_DEBUGGING = env_set_truthy('DBT_MACRO_DEBUGGING')
DEFER_MODE = env_set_truthy('DBT_DEFER_TO_STATE')
ARTIFACT_STATE_PATH = env_set_path('DBT_ARTIFACT_STATE_PATH')
Expand All @@ -97,13 +96,15 @@ def set_from_args(args, user_config):
global STRICT_MODE, FULL_REFRESH, WARN_ERROR, \
USE_EXPERIMENTAL_PARSER, STATIC_PARSER, WRITE_JSON, PARTIAL_PARSE, \
USE_COLORS, STORE_FAILURES, PROFILES_DIR, DEBUG, LOG_FORMAT, GREEDY, \
VERSION_CHECK, FAIL_FAST, SEND_ANONYMOUS_USAGE_STATS, PRINTER_WIDTH
VERSION_CHECK, FAIL_FAST, SEND_ANONYMOUS_USAGE_STATS, PRINTER_WIDTH, \
WHICH

STRICT_MODE = False # backwards compatibility
# cli args without user_config or env var option
FULL_REFRESH = getattr(args, 'full_refresh', FULL_REFRESH)
STORE_FAILURES = getattr(args, 'store_failures', STORE_FAILURES)
GREEDY = getattr(args, 'greedy', GREEDY)
WHICH = getattr(args, 'which', WHICH)

# global cli flags with env var and user_config alternatives
USE_EXPERIMENTAL_PARSER = get_flag_value('USE_EXPERIMENTAL_PARSER', args, user_config)
Expand Down
10 changes: 2 additions & 8 deletions core/dbt/task/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dbt import utils
from dbt.dataclass_schema import dbtClassMixin
import threading
from typing import Dict, Any, Union
from typing import Union

from .compile import CompileRunner
from .run import RunTask
Expand All @@ -24,7 +24,7 @@
from dbt.graph import (
ResourceTypeSelector,
)
from dbt.node_types import NodeType, RunHookType
from dbt.node_types import NodeType
from dbt import flags


Expand Down Expand Up @@ -165,12 +165,6 @@ class TestTask(RunTask):
def raise_on_first_error(self):
return False

def safe_run_hooks(
self, adapter, hook_type: RunHookType, extra_context: Dict[str, Any]
) -> None:
# Don't execute on-run-* hooks for tests
pass

def get_node_selector(self) -> TestSelector:
if self.manifest is None or self.graph is None:
raise InternalException(
Expand Down
38 changes: 35 additions & 3 deletions test/integration/008_schema_tests_test/test_schema_v2_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,38 @@ def test_postgres_config(self):

class TestHooksInTests(DBTIntegrationTest):

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

@property
def models(self):
# this makes things easier
return "ephemeral"

@property
def project_config(self):
return {
'config-version': 2,
"on-run-start": ["{{ log('hooks called in tests -- good!') if execute }}"],
"on-run-end": ["{{ log('hooks called in tests -- good!') if execute }}"],
}

@use_profile('postgres')
def test_postgres_hooks_do_run_for_tests(self):
# This passes now that hooks run, a behavior we changed in v1.0
results = self.run_dbt(['test', '--model', 'ephemeral'])
self.assertEqual(len(results), 1)
for result in results:
self.assertEqual(result.status, "pass")
self.assertFalse(result.skipped)
self.assertEqual(
result.failures, 0,
'test {} failed'.format(result.node.name)
)

class TestHooksForWhich(DBTIntegrationTest):

@property
def schema(self):
return "schema_tests_008"
Expand All @@ -231,12 +263,12 @@ def models(self):
def project_config(self):
return {
'config-version': 2,
"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 }}"],
"on-run-start": ["{{exceptions.raise_compiler_error('hooks called in tests -- error') if (execute and flags.WHICH != 'test') }}"],
"on-run-end": ["{{exceptions.raise_compiler_error('hooks called in tests -- error') if (execute and flags.WHICH != 'test') }}"],
}

@use_profile('postgres')
def test_postgres_hooks_dont_run_for_tests(self):
def test_postgres_these_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)
Expand Down

0 comments on commit 4bda8c8

Please sign in to comment.