-
Notifications
You must be signed in to change notification settings - Fork 20
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
Feature support multiple test cases #54
Closed
marcellovictorino
wants to merge
3
commits into
mjirv:main
from
marcellovictorino:feature-support-multiple-test-cases
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,9 +1,50 @@ | ||
{% test unit_test(model, input_mapping, expected_output, name, description, compare_columns, depends_on) %} | ||
{% set test_sql = dbt_datamocktool.get_unit_test_sql(model, input_mapping, depends_on)|trim %} | ||
{% do return(dbt_utils.test_equality(expected_output, compare_model=test_sql, compare_columns=compare_columns)) %} | ||
{% test unit_test(model, input_mapping, expected_output, name, description, compare_columns, depends_on, test_case_list = []) %} | ||
{# Support iterating through list of test cases #} | ||
{% if test_case_list %} | ||
marcellovictorino marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{% set error_count = namespace(value=0) %} | ||
{% for test_case in test_case_list %} | ||
{# String substitution for inputs #} | ||
{% set individual_input_mapping = dict() %} | ||
{% for k, v in input_mapping.items() %} | ||
{# String substitution on the templated value #} | ||
{% set templated_value = v|replace('@', test_case)|replace('\\', '') %} | ||
{# Update copy of dictionary #} | ||
{% do individual_input_mapping.update({k: render('{{' ~ templated_value ~ '}}')}) %} | ||
{% endfor %} | ||
|
||
{# String substitution for expected output #} | ||
{# Equality test expects a Relation #} | ||
{% set full_path = render('{{' ~ expected_output|replace('@', test_case)|replace('\\', '') ~ '}}') %} | ||
{% set full_path_list = full_path.split('.') %} | ||
{% set individual_expected_output = adapter.get_relation(*full_path_list) %} | ||
|
||
{# Retrieve the SQL code with the input mapping applied, using mocked input #} | ||
{% set individual_test_sql = dbt_datamocktool.get_unit_test_sql(model, individual_input_mapping, depends_on, test_case) %} | ||
{# Retrieve the SQL code that compares the results between model and expected result #} | ||
{% set comparison_sql = dbt_utils.test_equality(individual_expected_output, compare_model=individual_test_sql, compare_columns=compare_columns) %} | ||
|
||
{% if execute %} | ||
{% set test_difference_count = run_query(comparison_sql).columns[0].values()[0] %} | ||
{% else %} | ||
{% set test_difference_count = 0 %} | ||
{% endif %} | ||
|
||
{% if test_difference_count > 0 %} | ||
{# log errors with red font #} | ||
{{ log('\033[31m [ERROR] >> TEST CASE FAILED: ' ~ test_case ~ ' | Number of incorrect records = ' ~ test_difference_count ~ '\033[m', info=True) }} | ||
{% set error_count.value = error_count.value + 1 %} | ||
{% endif %} | ||
{% endfor %} | ||
|
||
{% do return('select '~ error_count.value) %} | ||
|
||
{% else %} | ||
{# Backwards compatible when not using multiple test_case list #} | ||
{% set test_sql = dbt_datamocktool.get_unit_test_sql(model, input_mapping, depends_on)|trim %} | ||
{% do return(dbt_utils.test_equality(expected_output, compare_model=test_sql, compare_columns=compare_columns)) %} | ||
{% endif %} | ||
{% endtest %} | ||
|
||
{% test assert_mock_eq(model, input_mapping, expected_output) %} | ||
{% do return(test_unit_test(model, input_mapping, expected_output)) %} | ||
{% endtest %} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
model.database/schema
will retrieve the value from the model where the test is being applied.Alternatively, I wanted to use the default value as defined in the
dbt_project.yaml
, for the seeds. Any idea how to do this?In my use case, I was able to hard-code the value, but would be nice to have a more elegant solution