Skip to content

Commit

Permalink
chore: decouple dbt-core as an ext deps
Browse files Browse the repository at this point in the history
  • Loading branch information
datnguye committed Mar 30, 2024
1 parent a144f05 commit 2d6f6fa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
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

0 comments on commit 2d6f6fa

Please sign in to comment.