dbt-datamocktool icon indicating copy to clipboard operation
dbt-datamocktool copied to clipboard

Ability to mock {{ this }} even when accessed inside macros

Open ran-silberman-gong opened this issue 1 year ago • 2 comments

Describe the feature

Sometimes, in Incremental testing, we use call macros from a model to access {{ this }} table. In this case, the test fails to replace this table with the mock file

Describe alternatives you've considered

The alternative is completely to abandon the macro and have {{ this }} only in the model itself.

Additional context

Here is an example of the situation: inside a block of {% if is_incremental %} there might be a line like: where id > (select max(column_name) from {{ this }}) but in other cases, we may just call a macro that does the same: for example, we might implement it like this: in the is_incremental block we write:

{% if is_incremental %}
{% set max_value = get_nax_value(column_name) %}

and then call:

where id > ('{{ max_value }}')
{% endif %}

the macro function get_max_value() then implements select max(column_name) from {{ this }}

Problem is, that in compilation time this fails, because it tries to run the SELECT statement in compile time in order to render the model, but the variable this was not yet replaced by the mock table, because this happens only in runtime after rendering.

Who will this benefit?

Anyone who delegate functions from the module that uses {{ this }} into a macro

Are you interested in contributing this feature?

I would love to. Even though I do not have a suggestion on how to resolve it.

ran-silberman-gong avatar May 16 '23 11:05 ran-silberman-gong

This would be very useful I think! I too have had issues / wanting this functionality.

pettersoderlund avatar May 26 '23 18:05 pettersoderlund

Thanks @ran-silberman-gong and @pettersoderlund. I'm thinking about how to implement this. Let me know if you have some ideas, if not I'll update here soon!

mjirv avatar Jun 04 '23 19:06 mjirv