Skip to content

Commit

Permalink
Adjusted the render logic for the the input mapping.
Browse files Browse the repository at this point in the history
This has to be done for the values and the keys. This is necessary so that the unit tests can be executed as macros.
  • Loading branch information
Leopold Gabelmann committed Jun 5, 2023
1 parent caa2847 commit 5d3b4e3
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions macros/dmt_get_test_sql.sql
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{% macro get_unit_test_sql(model, input_mapping, depends_on) %}
{% set ns=namespace(
test_sql="(select 1) raw_code",
rendered_keys={},
rendered_mappings={},
graph_model=none
) %}

{% do dbt_datamocktool.__set_rendered_keys(ns, input_mapping.keys()) %}
{% do dbt_datamocktool.__set_rendered_mappings(ns, input_mapping) %}

{% if execute %}
{# inside an execute block because graph nodes aren't well-defined during parsing #}
{% set ns.graph_model = dbt_datamocktool.__get_graph_model(project_name, model.schema, model.name) %}
{% set ns.test_sql = ns.graph_model.raw_code %}

{% do dbt_datamocktool.__render_sql_and_replace_references(ns, input_mapping) %}

{% set mock_model_relation = dbt_datamocktool._get_model_to_mock(
model, suffix=('_dmt_' ~ modules.datetime.datetime.now().strftime("%S%f"))
) %}
{% do dbt_datamocktool.__render_sql_and_replace_references(ns, input_mapping) %}
{% set mock_model_relation = dbt_datamocktool._get_model_to_mock(
model, suffix=('_dmt_' ~ modules.datetime.datetime.now().strftime("%S%f"))
) %}

{% do dbt_datamocktool._create_mock_table_or_view(mock_model_relation, ns.test_sql) %}

Expand All @@ -25,36 +25,36 @@
{% for k in depends_on %}
-- depends_on: {{ k }}
{% endfor %}

{{ mock_model_relation }}
{% endmacro %}

{% macro get_unit_test_incremental_sql(model, input_mapping, depends_on) %}
{% set ns=namespace(
test_sql="(select 1) raw_code",
rendered_keys={},
rendered_mappings={},
graph_model=none
) %}

{# doing this outside the execute block allows dbt to infer the proper dependencies #}
{% do dbt_datamocktool.__set_rendered_keys(ns, input_mapping.keys()) %}

{% do dbt_datamocktool.__set_rendered_mappings(ns, input_mapping) %}
{% if execute %}
{# inside an execute block because graph nodes aren't well-defined during parsing #}
{% set ns.graph_model = dbt_datamocktool.__get_graph_model(project_name, model.schema, model.name) %}

{% set ns.test_sql = ns.graph_model.raw_code %}

{# replace is_incremental blocks to true to enable incremental code #}
{% set ns.test_sql = ns.test_sql|replace('is_incremental()','true') %}
{% set ns.test_sql = ns.test_sql|replace('is_incremental()','true') %}

{% do dbt_datamocktool.__render_sql_and_replace_references(ns, input_mapping) %}

{# after rendering - replace "this" with mock project and model #}
{# TODO: try catch -- if this not exists in input mapping #}
{% set ns.test_sql = ns.test_sql|replace(this.dataset, model.dataset) %}
{% set ns.test_sql = ns.test_sql|replace(this.table, input_mapping.this) %}
{% set ns.test_sql = ns.test_sql|replace(this.dataset, model.dataset) %}
{% set ns.test_sql = ns.test_sql|replace(this.table, input_mapping.this) %}

{# mock_model_relation is the mocked model name #}
{% set mock_model_relation = dbt_datamocktool._get_model_to_mock(
model, suffix=('_dmt_' ~ modules.datetime.datetime.now().strftime("%S%f"))
Expand Down Expand Up @@ -123,13 +123,13 @@
{% do run_query(get_merge_sql(model, test_sql, dest_columns=dest_columns)) %}
{% endmacro %}

{% macro __set_rendered_keys(ns, keys) %}
{% for k in keys %}
{% do ns.rendered_keys.update({k: render("{{ " + k + " }}")}) %}
{% macro __set_rendered_mappings(ns, input_mapping) %}
{% for k, v in input_mapping.items() %}
{% do ns.rendered_mappings.update({k: render(v)}) %}
{% endfor %}
{% endmacro %}

{% macro __get_graph_model(project_name, model_schema, model_name) %}
{% macro __get_graph_model(project_name, model_schema, model_name) %}
{% set graph_model = graph.nodes.get("model." + project_name + "." + model_name) %}
{# if the model uses an alias, the above call was unsuccessful, so loop through the graph to grab it by the alias instead #}
{% if graph_model is none %}
Expand All @@ -143,9 +143,20 @@
{{ return(graph_model) }}
{% endmacro %}

{% macro __render_sql_and_replace_references(ns, input_mapping) %}
{% for k,v in input_mapping.items() %}
{# render the original sql and replacement key before replacing because v is already rendered when it is passed to this test #}
{% set ns.test_sql = render(ns.test_sql)|replace(ns.rendered_keys[k], v) %}
{% macro __render_sql_and_replace_references(ns, input_mapping) %}
{#- Replace the keys first, before the sql code is rendered -#}
{% for k, v in ns.rendered_mappings.items() %}
{% set ns.test_sql = ns.test_sql|replace("{{ "~render(k)~" }}", v) %}
{% endfor %}

{#- Render the original sql after all reference values are set according to the provided input
mapping. -#}
{% set ns.test_sql = render(ns.test_sql) %}

{#- Replace left over rendered keys with their reference values. This is only necessary, if the
unit test is defined within a macro, since then the input mapping is already rendered within
the macro itself.-#}
{% for k, v in ns.rendered_mappings.items() %}
{% set ns.test_sql = ns.test_sql|replace(k, v) %}
{% endfor %}
{% endmacro %}

0 comments on commit 5d3b4e3

Please sign in to comment.