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

Add dbt version check and adapt dbt test module for upcoming deprecation #80

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 22 additions & 10 deletions droughty/droughty/droughty_dbt/dbt_test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@
IdentifyConfigVariables,
ExploresVariables
)
from droughty.droughty_dbt.dbt_version_util import is_version_gte_1_8

import sys
import ruamel.yaml
import git


def get_test_definition_key():
"""
Returns the appropriate test definition key based on the dbt version.
"""
if is_version_gte_1_8():
return "data_tests"
else:
return "tests"

def get_all_values(nested_dictionary):

tests_config_key = get_test_definition_key()

test_overwrite = ExploresVariables.test_overwrite

Expand Down Expand Up @@ -63,22 +75,22 @@ def get_all_values(nested_dictionary):

if "pk" in key1 and "not_null" not in value1 and "unique" not in value1:

elem = {"name": key1, "description": "{{doc("+'"'+key1+'"'+")}}", "tests": ["not_null","unique"]}
elem = {"name": key1, "description": "{{doc("+'"'+key1+'"'+")}}", tests_config_key: ["not_null","unique"]}
seq.append(elem)

elif "fk" in key1:

elem = {"name": key1, "description": "{{doc("+'"'+key1+'"'+")}}", "tests": ["dbt_utils.at_least_one"]}
elem = {"name": key1, "description": "{{doc("+'"'+key1+'"'+")}}", tests_config_key: ["dbt_utils.at_least_one"]}
seq.append(elem)

elif "valid_to" in key1 or "valid_from" in key1:

elem = {"name": key1, "description": "{{doc("+'"'+key1+'"'+")}}", "tests": ["dbt_utils.expression_is_true"":""expression"":"" valid_from < valid_to","not_null","unique"]}
elem = {"name": key1, "description": "{{doc("+'"'+key1+'"'+")}}", tests_config_key: ["dbt_utils.expression_is_true"":""expression"":"" valid_from < valid_to","not_null","unique"]}
seq.append(elem)

elif "pk" not in key1 or "fk" not in key1:

elem = {"name": key1, "description": "{{doc("+'"'+key1+'"'+")}}", "tests": [""+"dbt_utils.at_least_one"]}
elem = {"name": key1, "description": "{{doc("+'"'+key1+'"'+")}}", tests_config_key: [""+"dbt_utils.at_least_one"]}
seq.append(elem)

elif "pk" not in key1 or "fk" not in key1:
Expand All @@ -90,35 +102,35 @@ def get_all_values(nested_dictionary):

if "pk" in key1:

elem = {"name": key1, "tests": ["not_null","unique"]}
elem = {"name": key1, tests_config_key: ["not_null","unique"]}
seq.append(elem)

elif "fk" in key1:

elem = {"name": key1, "tests": ["dbt_utils.at_least_one"]}
elem = {"name": key1, tests_config_key: ["dbt_utils.at_least_one"]}
seq.append(elem)

elif "valid_to" in key1 or "valid_from" in key1:

elem = {"name": key1, "tests": ["dbt_utils.expression_is_true"":""expression"":"" valid_from < valid_to","not_null","unique"]}
elem = {"name": key1, tests_config_key: ["dbt_utils.expression_is_true"":""expression"":"" valid_from < valid_to","not_null","unique"]}
seq.append(elem)

elif "pk" not in key1 or "fk" not in key1:

elem = {"name": key1, "tests": [""+"dbt_utils.at_least_one"]}
elem = {"name": key1, tests_config_key: [""+"dbt_utils.at_least_one"]}
seq.append(elem)

elif key + "-" + key1 in ignore_test_keys_and_values and key not in ExploresVariables.test_ignore:

if key1 in described_columns_list:


elem = {"name": key1, "description": "{{doc("+'"'+key1+'"'+")}}", "tests": value1}
elem = {"name": key1, "description": "{{doc("+'"'+key1+'"'+")}}", tests_config_key: value1}
seq.append(elem)

elif key1 not in described_columns_list:

elem = {"name": key1, "tests": value1}
elem = {"name": key1, tests_config_key: value1}
seq.append(elem)

res.append([{"name": key, "columns": seq}])
Expand Down
10 changes: 10 additions & 0 deletions droughty/droughty/droughty_dbt/dbt_version_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import dbt.version
import dbt_common.semver as semver

def is_version_gte_1_8():
"""
Checks if the installed dbt version is greater than or equal to 1.8.0.
"""
installed_version = dbt.version.get_installed_version()
target_version = semver.VersionSpecifier.from_version_string("1.8.0")
return installed_version.compare(target_version) >= 0