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

fix(dbt): Load profiles with Jinja #280

Merged
merged 5 commits into from
Apr 22, 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: 6 additions & 3 deletions src/preset_cli/cli/superset/sync/dbt/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
from preset_cli.cli.superset.sync.dbt.databases import sync_database
from preset_cli.cli.superset.sync.dbt.datasets import sync_datasets
from preset_cli.cli.superset.sync.dbt.exposures import ModelKey, sync_exposures
from preset_cli.cli.superset.sync.dbt.lib import apply_select, list_failed_models
from preset_cli.cli.superset.sync.dbt.lib import (
apply_select,
list_failed_models,
load_profiles,
)
from preset_cli.cli.superset.sync.dbt.metrics import (
get_models_from_sql,
get_superset_metrics_per_model,
Expand Down Expand Up @@ -182,8 +186,7 @@ def dbt_core( # pylint: disable=too-many-arguments, too-many-branches, too-many
with open(manifest, encoding="utf-8") as input_:
configs = yaml.load(input_, Loader=yaml.SafeLoader)

with open(profiles, encoding="utf-8") as input_:
config = yaml.safe_load(input_)
config = load_profiles(Path(profiles), project, profile, target)
dialect = config[project]["outputs"][target]["type"]
mf_dialect = MFSQLEngine(dialect.upper())

Expand Down
91 changes: 91 additions & 0 deletions tests/cli/superset/sync/dbt/command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1904,6 +1904,97 @@ def test_dbt_core_merge_metadata(
)


def test_dbt_core_load_profile_jinja_variables(
mocker: MockerFixture,
fs: FakeFilesystem,
) -> None:
"""
Test the ``dbt-core`` command loading a profiles.yml containing Jinja
variables.
"""
jinja_profile_content = yaml.dump(
{
"default": {
"outputs": {
"dev": {
"type": "{{ env_var('DBT_TYPE') }}",
"dbname": "{{ env_var('DBT_DBNAME') }}",
"host": "{{ env_var('DBT_HOST') }}",
"user": "{{ env_var('DBT_USER') }}",
"pass": "{{ env_var('DBT_PASS') }}",
"port": "{{ env_var('DBT_PORT') | as_number }}",
"threads": "{{ env_var('DBT_THREADS') | as_number }}",
"schema": "{{ env_var('DBT_SCHEMA') }}",
},
},
},
},
)

mocker.patch.dict(
os.environ,
{
"DBT_DBNAME": "database",
"DBT_HOST": "hostname",
"DBT_USER": "username",
"DBT_PASS": "password",
"DBT_PORT": "5432",
"DBT_SCHEMA": "schema",
"DBT_THREADS": "1",
"DBT_TYPE": "postgres",
},
)

root = Path("/path/to/root")
fs.create_dir(root)
manifest = root / "default/target/manifest.json"
fs.create_file(manifest, contents=manifest_contents)
profiles = root / ".dbt/profiles.yml"
fs.create_file(profiles, contents=jinja_profile_content)
exposures = root / "models/exposures.yml"
fs.create_file(exposures)

SupersetClient = mocker.patch(
"preset_cli.cli.superset.sync.dbt.command.SupersetClient",
)
client = SupersetClient()
mocker.patch("preset_cli.cli.superset.main.UsernamePasswordAuth")
sync_database = mocker.patch(
"preset_cli.cli.superset.sync.dbt.command.sync_database",
)

runner = CliRunner()
result = runner.invoke(
superset_cli,
[
"https://superset.example.org/",
"sync",
"dbt-core",
str(manifest),
"--profiles",
str(profiles),
"--exposures",
str(exposures),
"--project",
"default",
"--target",
"dev",
],
catch_exceptions=False,
)
assert result.exit_code == 0
sync_database.assert_called_with(
client,
profiles,
"default",
"default",
"dev",
False,
False,
"",
)


def test_dbt_core_raise_failures_flag_no_failures(
mocker: MockerFixture,
fs: FakeFilesystem,
Expand Down
Loading