EqualExperts / dbt-unit-testing

This dbt package contains macros to support unit testing that can be (re)used across dbt projects.
MIT License
421 stars 78 forks source link

dbt compile error 'dict object' has no attribute 'raw_code' #180

Closed AlejandroAlmaraz closed 6 months ago

AlejandroAlmaraz commented 1 year ago

Dataplatform: Snowflake Package unit-testing version: 0.3.3 DBT version: 1.5.1

I created a example test in my project and I am getting the following error when dbt compile run against all the project.

Runtime Error
  Compilation Error in test test_total_cost_by_costumer (tests/unit/test_total_cost_by_costumer.sql)
    'dict object' has no attribute 'raw_code'

    > in macro build_model_complete_sql (macros/sql_builders.sql)
    > called by macro build_test_queries (macros/tests.sql)
    > called by macro build_configuration_and_test_queries (macros/tests.sql)
    > called by macro test (macros/tests.sql)

I do not have any problem when I run dbt test, I saw after some debuging this raw_code is present when dbt test is executed but using dbt compile is not present in the model_node variable.

Code test:

-- depends_on: {{ ref('costumer_order_transaction') }}
{{
    config(
        tags=['unit-test']
    )
}}

{% call dbt_unit_testing.test ('total_cost_by_costumer','total_peding_cancelled_cost_by_costumer') %}
  {% call dbt_unit_testing.mock_ref ('costumer_order_transaction') %}
     SELECT 
    '123' as ID,
    'name1' as NAME,
    'LASTNAME1' as LAST_NAME,
    '120912' as ORDER_ID
    UNION
    SELECT 
    '124' as ID,
    'name2' as NAME,
    'LASTNAME2' as LAST_NAME,
    '130913' as ORDER_ID
    UNION
    SELECT 
    '125' as ID,
    'name3' as NAME,
    'LASTNAME3' as LAST_NAME,
    '140914' as ORDER_ID
    UNION
    SELECT 
    '126' as ID,
    'USERNAME25' as NAME,
    'LASTNAME25' as LAST_NAME,
    '150915' as ORDER_ID
  {% endcall %}

  {% call dbt_unit_testing.mock_ref('stg_order') %}
    SELECT
     '120912'    as order_id,
     'CANCELLED' as status,
     1000        as cost
     UNION ALL
     SELECT
     '120912'    as order_id,
     'CANCELLED' as status,
     1000        as cost
     UNION ALL
     SELECT
     '120912'    as order_id,
     'PENDING'   as status,
     1200        as cost
     UNION ALL
     SELECT
     '150915'    as order_id,
     'CONFIRMED' as status,
     1200        as cost
     UNION ALL 
     SELECT
     '150915'   as order_id,
     'PENDING'  as status,
     100        as cost
     UNION ALL
     SELECT
     '140914'    as order_id,
     'CONFIRMED' as status,
     50000       as cost
     UNION ALL 
     SELECT
     '140914'    as order_id,
     'PROCESSING'as status,
     100         as cost
  {% endcall %}

  {% call dbt_unit_testing.expect() %}
     SELECT 
    '123'       as ID,
    'name1'     as NAME,
    'LASTNAME1' as LAST_NAME,
    '120912'    as ORDER_ID,
    3200        as TOTAL_COST
    UNION
    SELECT 
    '126' as ID,
    'USERNAME25' as NAME,
    'LASTNAME25' as LAST_NAME,
    '150915'    as ORDER_ID,
    100        as TOTAL_COST
    UNION
    SELECT 
    '125'       as ID,
    'name3'     as NAME,
    'LASTNAME3' as LAST_NAME,
    '140914'    as ORDER_ID,
    0           as TOTAL_COST
  {% endcall %}

{% endcall %}
victormartingarcia commented 1 year ago

It happens the same to me with this unit test below:

{{ config(tags=['unit-test']) }}

{% set options = {} %}

{% call dbt_unit_testing.test(
  'funds', 'should contain only funds its company belong to a structure investment vehicle') %}

  {# *********************** Arrange *********************** #}

  {% call dbt_unit_testing.mock_ref('stg_backend__funds') %}
    uuid  | company_id
    'OK'  | 1
    'KO'  | 2
  {% endcall %}

  {% call dbt_unit_testing.mock_ref('stg_backend__companies') %}
    id
    1
    2
  {% endcall %}

  {% call dbt_unit_testing.mock_ref('stg_backend__structure_investment_vehicles') %}
    vehicle_id
    1
  {% endcall %}

  {% call dbt_unit_testing.expect(options) %}
    uuid
    'OK'
  {% endcall %}
{% endcall %}
whiteh commented 10 months ago

Hello! We had this issue and we think it was a conflict with dbt_artifacts. It looks like part of logging models deletes the raw_code attribute from the each model. During dbt compile it seems like the unit test module does something after the on-run-end hook. That was where we saw the error. Perhaps your issues are connected if you use that package?

https://github.com/brooklyn-data/dbt_artifacts/issues/414

psousa50 commented 10 months ago

Hi @AlejandroAlmaraz , @victormartingarcia could you please confirm that you are using dbt_artifacts package?

Thank you!

whiteh commented 9 months ago

Fix for this was just accepted, so should be in the next release of dbt_artifacts.

https://github.com/brooklyn-data/dbt_artifacts/pull/415

cdiniz commented 6 months ago

Closing due to inactivity. thank you!