-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
beb9980
commit 4a8826a
Showing
4 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
Utility functions for prefect-dbt | ||
""" | ||
import os | ||
from typing import Any, Dict, Optional | ||
|
||
import yaml | ||
|
||
|
||
def get_profiles_dir() -> str: | ||
"""Get the dbt profiles directory from environment or default location.""" | ||
profiles_dir = os.getenv("DBT_PROFILES_DIR") | ||
if not profiles_dir: | ||
profiles_dir = os.path.expanduser("~/.dbt") | ||
return profiles_dir | ||
|
||
|
||
def load_profiles_yml(profiles_dir: Optional[str]) -> Dict[str, Any]: | ||
""" | ||
Load and parse the profiles.yml file. | ||
Args: | ||
profiles_dir: Path to the directory containing profiles.yml. | ||
If None, uses the default profiles directory. | ||
Returns: | ||
Dict containing the parsed profiles.yml contents | ||
Raises: | ||
ValueError: If profiles.yml is not found | ||
""" | ||
if profiles_dir is None: | ||
profiles_dir = get_profiles_dir() | ||
|
||
profiles_path = os.path.join(profiles_dir, "profiles.yml") | ||
if not os.path.exists(profiles_path): | ||
raise ValueError(f"No profiles.yml found at {profiles_path}") | ||
|
||
with open(profiles_path, "r") as f: | ||
return yaml.safe_load(f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
import pytest | ||
import yaml | ||
from prefect_dbt.utilities import get_profiles_dir, load_profiles_yml | ||
|
||
SAMPLE_PROFILES = { | ||
"jaffle_shop": { | ||
"outputs": { | ||
"dev": { | ||
"type": "duckdb", | ||
"path": "jaffle_shop.duckdb", | ||
"schema": "main", | ||
"threads": 4, | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def temp_profiles_dir(tmp_path): | ||
profiles_dir = tmp_path / ".dbt" | ||
profiles_dir.mkdir() | ||
|
||
profiles_path = profiles_dir / "profiles.yml" | ||
with open(profiles_path, "w") as f: | ||
yaml.dump(SAMPLE_PROFILES, f) | ||
|
||
return str(profiles_dir) | ||
|
||
|
||
def test_get_profiles_dir_default(): | ||
if "DBT_PROFILES_DIR" in os.environ: | ||
del os.environ["DBT_PROFILES_DIR"] | ||
|
||
expected = os.path.expanduser("~/.dbt") | ||
assert get_profiles_dir() == expected | ||
|
||
|
||
def test_get_profiles_dir_from_env(): | ||
test_path = "/custom/path" | ||
os.environ["DBT_PROFILES_DIR"] = test_path | ||
try: | ||
assert get_profiles_dir() == test_path | ||
finally: | ||
del os.environ["DBT_PROFILES_DIR"] | ||
|
||
|
||
def test_load_profiles_yml_success(temp_profiles_dir): | ||
profiles = load_profiles_yml(temp_profiles_dir) | ||
assert profiles == SAMPLE_PROFILES | ||
|
||
|
||
def test_load_profiles_yml_default_dir(monkeypatch, temp_profiles_dir): | ||
monkeypatch.setenv("DBT_PROFILES_DIR", temp_profiles_dir) | ||
profiles = load_profiles_yml(None) | ||
assert profiles == SAMPLE_PROFILES | ||
|
||
|
||
def test_load_profiles_yml_file_not_found(): | ||
nonexistent_dir = "/path/that/does/not/exist" | ||
with pytest.raises( | ||
ValueError, | ||
match=f"No profiles.yml found at {os.path.join(nonexistent_dir, 'profiles.yml')}", | ||
): | ||
load_profiles_yml(nonexistent_dir) | ||
|
||
|
||
def test_load_profiles_yml_invalid_yaml(temp_profiles_dir): | ||
profiles_path = Path(temp_profiles_dir) / "profiles.yml" | ||
with open(profiles_path, "w") as f: | ||
f.write("invalid: yaml: content:\nindentation error") | ||
|
||
with pytest.raises(yaml.YAMLError): | ||
load_profiles_yml(temp_profiles_dir) |