Skip to content

Commit

Permalink
dbt test works with Click (#5556)
Browse files Browse the repository at this point in the history
Co-authored-by: Github Build Bot <buildbot@fishtownanalytics.com>
resolves #5556
  • Loading branch information
aranke authored Jan 11, 2023
1 parent 5b31cc4 commit 91b20b7
Show file tree
Hide file tree
Showing 21 changed files with 270 additions and 13,310 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20230109-151417.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: '[CT-932] Implement `dbt test` in Click'
time: 2023-01-09T15:14:17.524221-08:00
custom:
Author: aranke
Issue: "5556"
21 changes: 14 additions & 7 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from dbt.task.clean import CleanTask
from dbt.task.deps import DepsTask
from dbt.task.run import RunTask
from dbt.task.test import TestTask


# CLI invocation
Expand Down Expand Up @@ -41,10 +42,11 @@ def __init__(
def invoke(self, args: List[str]) -> Tuple[Optional[List], bool]:
try:
dbt_ctx = cli.make_context(cli.name, args)
dbt_ctx.obj = {}
dbt_ctx.obj["project"] = self.project
dbt_ctx.obj["profile"] = self.profile
dbt_ctx.obj["manifest"] = self.manifest
dbt_ctx.obj = {
"project": self.project,
"profile": self.profile,
"manifest": self.manifest,
}
return cli.invoke(dbt_ctx)
except (click.NoSuchOption, click.UsageError) as e:
raise dbtUsageException(e.message)
Expand Down Expand Up @@ -206,7 +208,8 @@ def docs_serve(ctx, **kwargs):
@p.version_check
@requires.preflight
def compile(ctx, **kwargs):
"""Generates executable SQL from source, model, test, and analysis files. Compiled SQL files are written to the target/ directory."""
"""Generates executable SQL from source, model, test, and analysis files. Compiled SQL files are written to the
target/ directory."""
click.echo(f"`{inspect.stack()[0][3]}` called\n flags: {ctx.obj['flags']}")
return None, True

Expand Down Expand Up @@ -449,8 +452,12 @@ def freshness(ctx, **kwargs):
@requires.preflight
def test(ctx, **kwargs):
"""Runs tests on data in deployed models. Run this after `dbt run`"""
click.echo(f"`{inspect.stack()[0][3]}` called\n flags: {ctx.obj['flags']}")
return None, True
config = RuntimeConfig.from_parts(ctx.obj["project"], ctx.obj["profile"], ctx.obj["flags"])
task = TestTask(ctx.obj["flags"], config)

results = task.run()
success = task.interpret_results(results)
return results, success


# Support running as a module
Expand Down
6 changes: 2 additions & 4 deletions core/dbt/config/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,22 @@

from dbt import flags
from dbt.adapters.factory import get_include_paths, get_relation_class_by_name
from dbt.config.profile import read_user_config
from dbt.config.project import load_raw_project
from dbt.contracts.connection import AdapterRequiredConfig, Credentials, HasCredentials
from dbt.contracts.graph.manifest import ManifestMetadata
from dbt.contracts.project import Configuration, UserConfig
from dbt.contracts.relation import ComponentName
from dbt.dataclass_schema import ValidationError
from dbt.events.functions import warn_or_error
from dbt.events.types import UnusedResourceConfigPath
from dbt.exceptions import (
ConfigContractBroken,
DbtProjectError,
NonUniquePackageName,
RuntimeException,
UninstalledPackagesFound,
)
from dbt.events.functions import warn_or_error
from dbt.events.types import UnusedResourceConfigPath
from dbt.helper_types import DictDefaultEmptyStr, FQNPath, PathSet

from .profile import Profile
from .project import Project
from .renderer import DbtProjectYamlRenderer, ProfileRenderer
Expand Down
Binary file modified core/dbt/docs/build/doctrees/environment.pickle
Binary file not shown.
Binary file modified core/dbt/docs/build/doctrees/index.doctree
Binary file not shown.
2 changes: 1 addition & 1 deletion core/dbt/docs/build/html/.buildinfo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: 1ee31fc16e025fb98598189ba2cb5fcb
config: e27d6c1c419f2f0af393858cdf674109
tags: 645f666f9bcd5a90fca523b33c5a78b7
32 changes: 32 additions & 0 deletions core/dbt/docs/build/html/_sources/index.rst.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
dbt-core's API documentation
============================
How to invoke dbt commands in python runtime
--------------------------------------------

Right now the best way to invoke a command from python runtime is to use the `dbtRunner` we exposed

.. code-block:: python
from dbt.cli.main import dbtRunner
cli_args = ['run', '--project-dir', 'jaffle_shop']
# initialize the dbt runner
dbt = dbtRunner()
# run the command
res, success = dbt.invoke(args)
You can also pass in pre constructed object into dbtRunner, and we will use those objects instead of loading up from the disk.

.. code-block:: python
# preload profile and project
profile = load_profile(project_dir, {}, 'testing-postgres')
project = load_project(project_dir, False, profile, {})
# initialize the runner with pre-loaded profile and project
dbt = dbtRunner(profile=profile, project=project)
# run the command, this will use the pre-loaded profile and project instead of loading
res, success = dbt.invoke(cli_args)
For the full example code, you can refer to `core/dbt/cli/example.py`

API documentation
-----------------

.. dbt_click:: dbt.cli.main:cli

This file was deleted.

5 changes: 4 additions & 1 deletion core/dbt/docs/build/html/_static/basic.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* Sphinx stylesheet -- basic theme.
*
* :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
* :copyright: Copyright 2007-2023 by the Sphinx team, see AUTHORS.
* :license: BSD, see LICENSE for details.
*
*/
Expand Down Expand Up @@ -324,13 +324,15 @@ aside.sidebar {
p.sidebar-title {
font-weight: bold;
}

nav.contents,
aside.topic,
div.admonition, div.topic, blockquote {
clear: left;
}

/* -- topics ---------------------------------------------------------------- */

nav.contents,
aside.topic,
div.topic {
Expand Down Expand Up @@ -606,6 +608,7 @@ ol.simple p,
ul.simple p {
margin-bottom: 0;
}

aside.footnote > span,
div.citation > span {
float: left;
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/docs/build/html/_static/doctools.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* Base JavaScript utilities for all Sphinx HTML documentation.
*
* :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
* :copyright: Copyright 2007-2023 by the Sphinx team, see AUTHORS.
* :license: BSD, see LICENSE for details.
*
*/
Expand Down
Loading

0 comments on commit 91b20b7

Please sign in to comment.