-
Notifications
You must be signed in to change notification settings - Fork 123
feat: add _with_context variants for generic tests #975
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
Open
joostboon
wants to merge
3
commits into
master
Choose a base branch
from
feat/add-with-context-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+269
−0
Open
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 hidden or 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,54 @@ | ||
| {% test accepted_range_with_context( | ||
| model, | ||
| column_name, | ||
| min_value=none, | ||
| max_value=none, | ||
| inclusive=true, | ||
| context_columns=none | ||
| ) %} | ||
| {%- if min_value is none and max_value is none %} | ||
| {{ | ||
| exceptions.raise_compiler_error( | ||
| "accepted_range_with_context: at least one of min_value or max_value must be provided." | ||
| ) | ||
| }} | ||
| {%- endif %} | ||
|
|
||
| {%- if context_columns is not none and context_columns is iterable and context_columns is not string %} | ||
| {%- set existing_column_names = ( | ||
| adapter.get_columns_in_relation(model) | ||
| | map(attribute="name") | ||
| | map("lower") | ||
| | list | ||
| ) %} | ||
| {%- set select_cols = [column_name] %} | ||
| {%- for col in context_columns %} | ||
| {%- if col | lower == column_name | lower %} | ||
| {# already included, skip #} | ||
| {%- elif col | lower not in existing_column_names %} | ||
| {%- do log( | ||
| "WARNING [accepted_range_with_context]: column '" | ||
| ~ col | ||
| ~ "' does not exist in model '" | ||
| ~ model.name | ||
| ~ "' and will be skipped.", | ||
| info=true, | ||
| ) %} | ||
| {%- else %} {%- do select_cols.append(col) %} | ||
| {%- endif %} | ||
| {%- endfor %} | ||
| {%- set select_clause = select_cols | join(", ") %} | ||
| {%- else %} {%- set select_clause = "*" %} | ||
| {%- endif %} | ||
|
|
||
| select {{ select_clause }} | ||
| from {{ model }} | ||
| where | ||
| 1 = 2 | ||
| {%- if min_value is not none %} | ||
| or not {{ column_name }} >{{- "=" if inclusive }} {{ min_value }} | ||
| {%- endif %} | ||
| {%- if max_value is not none %} | ||
| or not {{ column_name }} <{{- "=" if inclusive }} {{ max_value }} | ||
| {%- endif %} | ||
| {% endtest %} | ||
38 changes: 38 additions & 0 deletions
38
macros/edr/tests/test_expect_column_values_to_be_unique_with_context.sql
This file contains hidden or 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,38 @@ | ||
| {% test expect_column_values_to_be_unique_with_context( | ||
| model, column_name, row_condition=none, context_columns=none | ||
| ) %} | ||
| {%- set all_columns = ( | ||
| adapter.get_columns_in_relation(model) | map(attribute="name") | list | ||
| ) %} | ||
| {%- set all_columns_lower = all_columns | map("lower") | list %} | ||
|
|
||
| {%- if context_columns is not none and context_columns is iterable and context_columns is not string %} | ||
| {%- set select_cols = [column_name] %} | ||
| {%- for col in context_columns %} | ||
| {%- if col | lower == column_name | lower %} | ||
| {# already included, skip #} | ||
| {%- elif col | lower not in all_columns_lower %} | ||
| {%- do log( | ||
| "WARNING [expect_column_values_to_be_unique_with_context]: column '" | ||
| ~ col | ||
| ~ "' does not exist in model '" | ||
| ~ model.name | ||
| ~ "' and will be skipped.", | ||
| info=true, | ||
| ) %} | ||
| {%- else %} {%- do select_cols.append(col) %} | ||
| {%- endif %} | ||
| {%- endfor %} | ||
| {%- set select_clause = select_cols | join(", ") %} | ||
| {%- else %} {%- set select_clause = all_columns | join(", ") %} | ||
| {%- endif %} | ||
|
|
||
| select {{ select_clause }} | ||
| from | ||
| ( | ||
| select *, count(*) over (partition by {{ column_name }}) as n_records | ||
| from {{ model }} | ||
| {%- if row_condition %} where {{ row_condition }} {%- endif %} | ||
| ) validation | ||
| where n_records > 1 | ||
| {% endtest %} |
45 changes: 45 additions & 0 deletions
45
macros/edr/tests/test_expect_column_values_to_match_regex_with_context.sql
This file contains hidden or 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,45 @@ | ||
| {% test expect_column_values_to_match_regex_with_context( | ||
| model, | ||
| column_name, | ||
| regex, | ||
| row_condition=none, | ||
| is_raw=false, | ||
| flags="", | ||
| context_columns=none | ||
| ) %} | ||
| {%- if context_columns is not none and context_columns is iterable and context_columns is not string %} | ||
| {%- set existing_column_names = ( | ||
| adapter.get_columns_in_relation(model) | ||
| | map(attribute="name") | ||
| | map("lower") | ||
| | list | ||
| ) %} | ||
| {%- set select_cols = [column_name] %} | ||
| {%- for col in context_columns %} | ||
| {%- if col | lower == column_name | lower %} | ||
| {# already included, skip #} | ||
| {%- elif col | lower not in existing_column_names %} | ||
| {%- do log( | ||
| "WARNING [expect_column_values_to_match_regex_with_context]: column '" | ||
| ~ col | ||
| ~ "' does not exist in model '" | ||
| ~ model.name | ||
| ~ "' and will be skipped.", | ||
| info=true, | ||
| ) %} | ||
| {%- else %} {%- do select_cols.append(col) %} | ||
| {%- endif %} | ||
| {%- endfor %} | ||
| {%- set select_clause = select_cols | join(", ") %} | ||
| {%- else %} {%- set select_clause = "*" %} | ||
| {%- endif %} | ||
|
|
||
| select {{ select_clause }} | ||
| from {{ model }} | ||
| where | ||
| {{ | ||
| dbt_expectations.regexp_instr( | ||
| column_name, regex, is_raw=is_raw, flags=flags | ||
| ) | ||
| }} = 0 {%- if row_condition %} and {{ row_condition }} {%- endif %} | ||
| {% endtest %} |
36 changes: 36 additions & 0 deletions
36
macros/edr/tests/test_expect_column_values_to_not_be_null_with_context.sql
This file contains hidden or 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,36 @@ | ||
| {% test expect_column_values_to_not_be_null_with_context( | ||
| model, column_name, row_condition=none, context_columns=none | ||
| ) %} | ||
| {%- if context_columns is not none and context_columns is iterable and context_columns is not string %} | ||
| {%- set existing_column_names = ( | ||
| adapter.get_columns_in_relation(model) | ||
| | map(attribute="name") | ||
| | map("lower") | ||
| | list | ||
| ) %} | ||
| {%- set select_cols = [column_name] %} | ||
| {%- for col in context_columns %} | ||
| {%- if col | lower == column_name | lower %} | ||
| {# already included, skip #} | ||
| {%- elif col | lower not in existing_column_names %} | ||
| {%- do log( | ||
| "WARNING [expect_column_values_to_not_be_null_with_context]: column '" | ||
| ~ col | ||
| ~ "' does not exist in model '" | ||
| ~ model.name | ||
| ~ "' and will be skipped.", | ||
| info=true, | ||
| ) %} | ||
| {%- else %} {%- do select_cols.append(col) %} | ||
| {%- endif %} | ||
| {%- endfor %} | ||
| {%- set select_clause = select_cols | join(", ") %} | ||
| {%- else %} {%- set select_clause = "*" %} | ||
| {%- endif %} | ||
|
|
||
| select {{ select_clause }} | ||
| from {{ model }} | ||
| where | ||
| {{ column_name }} is null | ||
| {%- if row_condition %} and {{ row_condition }} {%- endif %} | ||
| {% endtest %} |
This file contains hidden or 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,31 @@ | ||
| {% test not_null_with_context(model, column_name, context_columns=none) %} | ||
| {%- if context_columns is not none and context_columns is iterable and context_columns is not string %} | ||
| {%- set existing_column_names = ( | ||
| adapter.get_columns_in_relation(model) | ||
| | map(attribute="name") | ||
| | map("lower") | ||
| | list | ||
| ) %} | ||
|
|
||
| {%- set select_cols = [column_name] %} | ||
| {%- for col in context_columns %} | ||
| {%- if col | lower == column_name | lower %} | ||
| {# already included, skip #} | ||
| {%- elif col | lower not in existing_column_names %} | ||
| {%- do log( | ||
| "WARNING [not_null_with_context]: column '" | ||
| ~ col | ||
| ~ "' does not exist in model '" | ||
| ~ model.name | ||
| ~ "' and will be skipped.", | ||
| info=true, | ||
| ) %} | ||
| {%- else %} {%- do select_cols.append(col) %} | ||
| {%- endif %} | ||
| {%- endfor %} | ||
| select {{ select_cols | join(", ") }} | ||
| from {{ model }} | ||
| where {{ column_name }} is null | ||
| {%- else %} select * from {{ model }} where {{ column_name }} is null | ||
| {%- endif %} | ||
| {% endtest %} |
This file contains hidden or 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,35 @@ | ||
| {% test relationships_with_context( | ||
| model, column_name, to, field, context_columns=none | ||
| ) %} | ||
| {%- if context_columns is not none and context_columns is iterable and context_columns is not string %} | ||
| {%- set existing_column_names = ( | ||
| adapter.get_columns_in_relation(model) | ||
| | map(attribute="name") | ||
| | map("lower") | ||
| | list | ||
| ) %} | ||
| {%- set select_cols = ["child." ~ column_name] %} | ||
| {%- for col in context_columns %} | ||
| {%- if col | lower == column_name | lower %} | ||
| {# already included, skip #} | ||
| {%- elif col | lower not in existing_column_names %} | ||
| {%- do log( | ||
| "WARNING [relationships_with_context]: column '" | ||
| ~ col | ||
| ~ "' does not exist in model '" | ||
| ~ model.name | ||
| ~ "' and will be skipped.", | ||
| info=true, | ||
| ) %} | ||
| {%- else %} {%- do select_cols.append("child." ~ col) %} | ||
| {%- endif %} | ||
| {%- endfor %} | ||
| {%- set select_clause = select_cols | join(", ") %} | ||
| {%- else %} {%- set select_clause = "child.*" %} | ||
| {%- endif %} | ||
|
|
||
| select {{ select_clause }} | ||
| from {{ model }} as child | ||
| left join {{ to }} as parent on child.{{ column_name }} = parent.{{ field }} | ||
| where child.{{ column_name }} is not null and parent.{{ field }} is null | ||
| {% endtest %} |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.