Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit acad4e4

Browse files
committed
custom exceptions and tests
1 parent 00bc80a commit acad4e4

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

data_diff/dbt.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import os
22
import re
33
import time
4-
import webbrowser
54
from typing import List, Optional, Dict, Tuple, Union
65
import keyring
76
import pydantic
87
import rich
9-
from rich.prompt import Confirm, Prompt
8+
from rich.prompt import Prompt
109

11-
from data_diff.errors import DataDiffCustomSchemaNoConfigError, DataDiffDbtProjectVarsNotFoundError
10+
from data_diff.errors import DataDiffCustomSchemaNoConfigError, DataDiffDbtProjectVarsNotFoundError, DataDiffNoAPIKeyError, DataDiffNoDatasourceIdError
1211

1312
from . import connect_to_table, diff_tables, Algorithm
1413
from .cloud import DatafoldAPI, TCloudApiDataDiff, TCloudApiOrgMeta, get_or_create_data_source
15-
from .dbt_parser import DbtParser, PROJECT_FILE, TDatadiffConfig
14+
from .dbt_parser import DbtParser, TDatadiffConfig
1615
from .tracking import (
1716
bool_ask_for_email,
1817
create_email_signup_event_json,
@@ -38,6 +37,7 @@
3837
)
3938

4039
logger = getLogger(__name__)
40+
CLOUD_DOC_URL = "https://docs.datafold.com/development_testing/cloud"
4141

4242

4343
class TDiffVars(pydantic.BaseModel):
@@ -81,11 +81,9 @@ def dbt_diff(
8181
org_meta = api.get_org_meta()
8282

8383
if config.datasource_id is None:
84-
cloud_doc_url = "https://docs.datafold.com/development_testing/cloud"
8584
rich.print("[red]Data source ID not found in dbt_project.yml")
86-
raise ValueError(
87-
"Datasource ID not found. Please include it as a dbt variable in the dbt_project.yml. Instructions: {cloud_doc_url}"
88-
"\nvars:\n data_diff:\n datasource_id: 1234"
85+
raise DataDiffNoDatasourceIdError(
86+
f"Datasource ID not found. Please include it as a dbt variable in the dbt_project.yml. \nInstructions: {CLOUD_DOC_URL}\n\nvars:\n data_diff:\n datasource_id: 1234"
8987
)
9088

9189
data_source = api.get_data_source(config.datasource_id)
@@ -283,8 +281,9 @@ def _initialize_api() -> Optional[DatafoldAPI]:
283281
rich.print("[red]API key not found. Getting from the keyring service")
284282
api_key = keyring.get_password("data-diff", "DATAFOLD_API_KEY")
285283
if not api_key:
286-
cloud_doc_url = "https://docs.datafold.com/development_testing/cloud"
287-
rich.print("[red]API key not found. Please follow the steps at {cloud_doc_url} to use the --cloud flag.")
284+
raise DataDiffNoAPIKeyError(
285+
f"API key not found. Please follow the steps at {CLOUD_DOC_URL} to use the --cloud flag."
286+
)
288287
rich.print("Saving the API key to the system keyring service")
289288
try:
290289
keyring.set_password("data-diff", "DATAFOLD_API_KEY", api_key)

data_diff/errors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,9 @@ class DataDiffDbtSelectVersionTooLowError(Exception):
4848

4949
class DataDiffCustomSchemaNoConfigError(Exception):
5050
"Raised when a model has a custom schema, but there is no prod_custom_schema config. (And not using --state)."
51+
52+
class DataDiffNoAPIKeyError(Exception):
53+
"Raised when using --cloud but no API key is present in the DATAFOLD_API_KEY env var or keyring"
54+
55+
class DataDiffNoDatasourceIdError(Exception):
56+
"Raised when using --cloud but no datasource_id was found in dbt_project.yml"

tests/test_dbt.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
DataDiffDbtRunResultsVersionError,
1717
DataDiffDbtSelectVersionTooLowError,
1818
DataDiffDbtSnowflakeSetConnectionError,
19+
DataDiffNoAPIKeyError,
20+
DataDiffNoDatasourceIdError,
1921
)
2022
from .test_cli import run_datadiff_cli
2123

@@ -771,7 +773,7 @@ def test_diff_is_cloud_no_ds_id(
771773
)
772774
mock_get_diff_vars.return_value = diff_vars
773775

774-
with self.assertRaises(ValueError):
776+
with self.assertRaises(DataDiffNoDatasourceIdError):
775777
dbt_diff(is_cloud=True)
776778
mock_dbt_parser_inst.get_models.assert_called_once()
777779
mock_dbt_parser_inst.set_connection.assert_not_called()
@@ -781,6 +783,31 @@ def test_diff_is_cloud_no_ds_id(
781783
mock_local_diff.assert_not_called()
782784
mock_print.assert_called_once()
783785

786+
@patch("data_diff.dbt._get_diff_vars")
787+
@patch("data_diff.dbt._local_diff")
788+
@patch("data_diff.dbt._cloud_diff")
789+
@patch("data_diff.dbt_parser.DbtParser.__new__")
790+
@patch("builtins.input", return_value="n")
791+
def test_diff_is_cloud_no_api_key(
792+
self, _, mock_dbt_parser, mock_cloud_diff, mock_local_diff, mock_get_diff_vars
793+
):
794+
mock_dbt_parser_inst = Mock()
795+
mock_model = Mock()
796+
config = TDatadiffConfig(prod_database="prod_db", prod_schema="prod_schema")
797+
798+
mock_dbt_parser.return_value = mock_dbt_parser_inst
799+
mock_dbt_parser_inst.get_models.return_value = [mock_model]
800+
mock_dbt_parser_inst.get_datadiff_config.return_value = config
801+
802+
with self.assertRaises(DataDiffNoAPIKeyError):
803+
dbt_diff(is_cloud=True)
804+
mock_dbt_parser_inst.get_models.assert_called_once()
805+
mock_dbt_parser_inst.set_connection.assert_not_called()
806+
807+
mock_get_diff_vars.assert_not_called()
808+
mock_cloud_diff.assert_not_called()
809+
mock_local_diff.assert_not_called()
810+
784811
@patch("data_diff.dbt._get_diff_vars")
785812
@patch("data_diff.dbt._local_diff")
786813
@patch("data_diff.dbt._cloud_diff")

0 commit comments

Comments
 (0)