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

Quoted column identifiers #153

Closed
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ venv/
integration-tests/ci/.user.yml
tmp
.idea
.devcontainer
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ Note that in this case, we are also mocking the model being tested (`incremental
| option | description | default | scope* |
|-----------------------------|---------------------------------|--------------------|--------------------|
| **include_missing_columns** | Use the definition of the model to grab the columns not specified in a mock. The columns will be added automatically with *null* values (this option will increase the number of roundtrips to the database when running the test). | false | project/test/mock |
| **quoted_columns** | Adds double quotes to the column identifiers generated by include_missing_columns | false | project/test/mock |
| **use_database_models** | Use the models in the database instead of the model SQL. <br> This option is used to simplify the final test query if needed | false | project/test/mock |
| **input_format** | **sql**: use *SELECT* statements to define the mock values. <br> <br> *SELECT 10::int as c1, 20 as c2 <br> UNION ALL <br> SELECT 30::int as c1, 40 as c2* <br> <br> **csv**: Use tabular form to specify mock values. <br> <br> c1::int \| c2 <br> 10 \| 20 <br> 30 \| 40 | sql | project/test |
| **column_separator** | Defines the column separator for csv format | , | project/test |
Expand Down
22 changes: 14 additions & 8 deletions macros/utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
{% endmacro %}

{% macro extract_columns_difference(cl1, cl2) %}
!! WRONG !!
{% set columns = cl1 | reject('in', cl2) | list %}
{{ return(columns) }}
{% set columns = [] %}
{% for column in cl1 %}
{% if column.lower() not in cl2 | map('lower') | list %}
{% set _ = columns.append(column) %}
{% endif %}
{% endfor %}
{{ return (columns) }}
{% endmacro %}

{% macro quote_and_join_columns(columns) %}
Expand Down Expand Up @@ -133,29 +137,31 @@
{{ return (dbt_unit_testing.merge_jsons([unit_tests_config] + configs)) }}
{% endmacro %}

{% macro quote_identifier(identifier) %}
{{ return(adapter.dispatch('quote_identifier','dbt_unit_testing')(identifier)) }}
{% macro quote_identifier(identifier, preserve_case=false) %}
{{ return(adapter.dispatch('quote_identifier','dbt_unit_testing')(identifier, preserve_case)) }}
{% endmacro %}

{% macro default__quote_identifier(identifier) -%}
{% macro default__quote_identifier(identifier, preserve_case) -%}
{% if identifier.startswith('"') %}
{{ return(identifier) }}
{% else %}
{{ return('"' ~ identifier ~ '"') }}
{% endif %}
{%- endmacro %}

{% macro bigquery__quote_identifier(identifier) %}
{% macro bigquery__quote_identifier(identifier, preserve_case) %}
{% if identifier.startswith('`') %}
{{ return(identifier) }}
{% else %}
{{ return('`' ~ identifier ~ '`') }}
{% endif %}
{% endmacro %}

{% macro snowflake__quote_identifier(identifier) %}
{% macro snowflake__quote_identifier(identifier, preserve_case) %}
{% if identifier.startswith('"') %}
{{ return(identifier) }}
{% elif preserve_case %}
{{ return('"' ~ identifier ~ '"') }}
{% else %}
{{ return('"' ~ identifier | upper ~ '"') }}
{% endif %}
Expand Down