diff --git a/dbt/include/global_project/macros/materializations/tests/test.sql b/dbt/include/global_project/macros/materializations/tests/test.sql index ba205a9b..15f55748 100644 --- a/dbt/include/global_project/macros/materializations/tests/test.sql +++ b/dbt/include/global_project/macros/materializations/tests/test.sql @@ -1,6 +1,12 @@ {%- materialization test, default -%} {% set relations = [] %} + {% set limit = config.get('limit') %} + + {% set main_sql %} + {{ sql }} + {{ "limit " ~ limit if limit != none }} + {% endset %} {% if should_store_failures() %} @@ -26,11 +32,12 @@ {% endif %} {% call statement(auto_begin=True) %} - {{ get_create_sql(target_relation, sql) }} + {{ get_create_sql(target_relation, main_sql) }} {% endcall %} {% do relations.append(target_relation) %} + {# Since the test failures have already been saved to the database, reuse that result rather than querying again #} {% set main_sql %} select * from {{ target_relation }} @@ -38,20 +45,16 @@ {{ adapter.commit() }} - {% else %} - - {% set main_sql = sql %} - {% endif %} - {% set limit = config.get('limit') %} {% set fail_calc = config.get('fail_calc') %} {% set warn_if = config.get('warn_if') %} {% set error_if = config.get('error_if') %} {% call statement('main', fetch_result=True) -%} - {{ get_test_sql(main_sql, fail_calc, warn_if, error_if, limit)}} + {# Since the limit has already been applied above, no need to apply it again! #} + {{ get_test_sql(main_sql, fail_calc, warn_if, error_if, limit=none)}} {%- endcall %}