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

chore: decouple dbt-core as an ext deps #101

Merged
merged 1 commit into from
Mar 30, 2024
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
9 changes: 5 additions & 4 deletions dbterd/adapters/dbt_core/dbt_invocation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import os
import importlib.util
from importlib import util
from importlib.metadata import version
from pathlib import Path
from typing import List

import click
from dbt.cli.main import dbtRunner, dbtRunnerResult

from dbterd.helpers.log import logger

Expand All @@ -21,6 +20,8 @@ def __init__(self, dbt_project_dir: str = None, dbt_target: str = None) -> None:
dbt_target (str, optional): Custom dbt target name. Defaults to None - using default target
"""
self.__ensure_dbt_installed()
from dbt.cli.main import dbtRunner

self.dbt = dbtRunner()
self.project_dir = (
dbt_project_dir or os.environ.get("DBT_PROJECT_DIR") or str(Path.cwd())
Expand All @@ -42,7 +43,7 @@ def __invoke(self, runner_args: List[str] = []):
"""
args = self.__construct_arguments(*runner_args)
logger.debug(f"Invoking: `dbt {' '.join(args)}` at {self.project_dir}")
r: dbtRunnerResult = self.dbt.invoke(args)
r = self.dbt.invoke(args)

if not r.success:
logger.error(str(r))
Expand Down Expand Up @@ -72,7 +73,7 @@ def __ensure_dbt_installed(self):
Raises:
click.UsageError: dbt is not installed
"""
dbt_spec = importlib.util.find_spec("dbt")
dbt_spec = util.find_spec("dbt")
if dbt_spec and dbt_spec.loader:
installed_path = dbt_spec.submodule_search_locations[0]
logger.debug(
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/adapters/test_dbt_invocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test__ensure_dbt_installed__no_dbt_installed(self, mock_find_spec):
),
],
)
@mock.patch("dbterd.adapters.dbt_core.dbt_invocation.dbtRunner.invoke")
@mock.patch("dbt.cli.main.dbtRunner.invoke")
def test_get_selection(
self,
mock_dbtRunner_invoke,
Expand Down Expand Up @@ -62,15 +62,15 @@ def test_get_selection(
]
)

@mock.patch("dbterd.adapters.dbt_core.dbt_invocation.dbtRunner.invoke")
@mock.patch("dbt.cli.main.dbtRunner.invoke")
def test_get_selection__failed(self, mock_dbtRunner_invoke):
mock_dbtRunner_invoke.return_value = dbtRunnerResult(success=False)
with pytest.raises(click.UsageError):
DbtInvocation(dbt_target="dummy").get_selection(
select_rules=[], exclude_rules=[]
)

@mock.patch("dbterd.adapters.dbt_core.dbt_invocation.dbtRunner.invoke")
@mock.patch("dbt.cli.main.dbtRunner.invoke")
def test_get_artifacts_for_erd(self, mock_dbtRunner_invoke):
invoker = DbtInvocation()
_ = invoker.get_artifacts_for_erd()
Expand Down
Loading