Upgrade to v2: Generic Data test behaving weirdly

The problem I’m having: I am upgrading out dbt installation from 1.8 to the newest version. Our project takes quite long to parse so the v2 parser seems like a good idea. When running test --use-v2-parser, I get this error on my generic data tests:

14:14:51    Database Error in test date_format_validation (tests/dbt/generic/date_format_validation.sql)
  
  [PARSE_SYNTAX_ERROR] Syntax error at or near ')'. SQLSTATE: 42601 (line 15, pos 4)
  
  == SQL ==
  /* {"app": "dbt", "dbt_version": "1.12.6", "dbt_databricks_version": "1.13.0", "databricks_sql_connector_version": "4.4.0", "profile_name": "MYCOMPANY", "target_name": "t", "invocation_id": "[REDACTED]", "node_id": "test.MYCOMPANY.date_format_validation"} */
  
      
      select
        count(*) as failures,
        count(*) != 0 as should_warn,
        count(*) != 0 as should_error
      from (
        
      
    
    
    
        
      ) dbt_internal_test

This is one my my data tests uncompiled:

{% test date_format_validation(model, column_name) %}

    {{ config(severity='warn') }}


    with validation as (
        select
            {{ column_name }} as correct_date_format
        from {{ model }}
    ),

    validation_errors as (
        select
            correct_date_format
        from validation
        where correct_date_format NOT LIKE '__-__-____'
    )
    
    select *
    from validation_errors

{% endtest %}

However the compiled version simply becomes only the code in the error. I can add some code below the endtest block and then that gets added to the run code. So this test passes with the where clause and fails without:

    select
      count(*) as failures,
      count(*) != 0 as should_warn,
      count(*) != 0 as should_error
    from (
      
    
  

select "hallo" from myschema.mytable where 1=0
  
  
      
    ) dbt_internal_test

Simply removing the datatest files leads to models crashing because they cannot invoke the test.
When using the v1 parser I don’t get this issue, but I would not be surprised if it turns out that is because the data tests were not enforced at all.

I am wondering if anyone encountered something similar and whether I am doing something wrong. Maybe from the beginning (but v1 just fails silently due to ignoring unknown keywords), maybe due to a change in v2.

The empty from ( ) means dbt generated the generic-test wrapper but the macro body rendered to nothing; that isn’t a syntax error in your date-format logic. Since code after {% endtest %} ends up in the compiled test, v2 looks like it’s rendering the file as ordinary SQL instead of extracting the {% test %} macro. Check that the file is in a configured test path (typically tests/generic), contains only the test macro, and isn’t shadowed by a duplicate macro.

Someone on github figured out the problem. There is an ongoing PR to fix it too. The issue is that custom test paths (such as tests/dbt instead of tests only) don’t actually work correctly and got silently dropped in v1 and loudly failed in v2

See the issue here: [1.x Bug] v2-parser breaks generic tests · Issue #17089 · dbt-labs/dbt