Unit tests failing with: "cannot access local variable 'connection' where it is not associated with a value"

I have some unit tests that I’ve written within a model’s configuration (i.e. YAML) file. Whenever I attempt to run them, they always fail with the following error: “cannot access local variable ‘connection’ where it is not associated with a value”.

I’ve already upgraded my dbt-core version to 1.10.15, and my Snowflake plugin to 1.10.3, which are both the latest versions as of the time of creating this post.

I created the most minimal model as possible, just to rule out as many factors as possible. This is my model’s SQL:
SELECT * from {{ ref('test_simple') }}

And this is my model’s YAML:

version: 2

models:
  - name: barebones
unit_tests:
  - name: test_barebones
    model: barebones
    given:
      - input: ref('test_simple')
        format: dict
        rows:
          - { id: 123, money_cents: 123, currency_code: USD }
    expect:
      format: dict
      rows:
        - { id: 123, money_cents: 123, currency_code: USD }

Here is a bit of the stack trace when I run the tests with debug mode:

00:01:58  Snowflake adapter: Error running SQL: macro drop_relation
00:01:58  Snowflake adapter: Rolling back transaction.
00:01:58  Runtime Error in unit_test test_barebones (models/intermediate/kafka/barebones.yml)
  An error occurred during execution of unit test 'test_barebones'. There may be an error in the unit test definition: check the data types.
   Runtime Error
    cannot access local variable 'connection' where it is not associated with a value

Sorry for the necrobump but I’m seeing the same phenomenon when using a multi-query macro in a post-hook. Seems like it could be a connection pooling issue. For now I’ve elected to hard code the queries from the macro into the post-hook. But it’s a bit of a bummer because I’d like to operationalize the post-hook and hard-coding an analog of the macro into every post-hook ain’t it.

@amack87 the issue of the less-than-helpful failure message (i.e. “cannot access local variable ‘connection’ where it is not associated with a value”) is known and it appears to be an issue in the Snowflake adapter: [Bug] Error in dbt-snowflake when executing empty pre-hook · Issue #997 · dbt-labs/dbt-adapters · GitHub.

As for the actual cause of my issue, the clue was in the stack trace of the failure: “Error running SQL: macro drop_relation”.

I created a set of macros that I used only for local development, and one of them was named “drop_relation”. Apparently dbt has an internal macro called “drop_relation” and my custom “drop_relation” macro was shadowing the built-in macro, so dbt was actually calling my macro instead of the built-in one.

See if you can deduce the underlying issue by checking some of the logs prior to the actual failure message.