Skip to content

DBT Test: Trase Regions Matches Indonesia Concessions 3 1

DBT test name: trase_regions_matches_indonesia_concessions_3_1

DBT details


Description

No description


Details

{{ test_reference_match(**_dbt_generic_test_kwargs) }}
    with unioned as (
        -- 2. Union distinct key combinations from both models
        -- Each row is tagged with its source ('model' or 'compare_to_model') to track its origin.

        -- Select distinct keys from the primary model
      select distinct

          trase_id as trase_id
        ,
        'model' as source_table
      from ST_READ('s3://resources.trase.earth/data/trase-regions/id/wood-pulp-concession-2020.geojson')


      union all

      -- Select distinct keys from the comparison model
      select distinct

          'ID-WOOD-CONCESSION-' || substr(ID, 3) as trase_id
        ,
        'compare_to_model' as source_table
      from "trase_production"."main"."indonesia_wood_pulp_logistics_out_concessions_ucsb_id_pulpwood_concessions_3_1"

    ),

    grouped as (
      -- 3. Group by the key columns to find unique occurrences
      -- Keys that appear only once (`occurrences = 1`) represent a mismatch between the models.
      select

          trase_id,

        count(*) as occurrences,
        min(source_table) as source_table -- If occurrences = 1, this reveals the single source table
      from unioned
      group by

          trase_id

    )

    -- 4. Select and classify the discrepancies based on the join_type
    -- The test fails if this query returns any rows.
    select

        trase_id,

      case
        when source_table = 'compare_to_model'
        then 'missing from model' -- The key was only in the reference model
        when source_table = 'model'
        then 'missing from reference' -- The key was only in the primary model
      end as error_type
    from grouped
    where occurrences = 1