[Feature] Enable unit testing for models that use `adapter.get_relation`
- Dominant language
- Rust
- Stars
- 13.8k
- Forks
- 2.6k
- Avg merge
- 21h 31m
- Merged PRs (30d)
- 56
Description
### Is this a new bug in dbt-core?
- [X] I believe this is a new bug in dbt-core
- [X] I have searched the existing issues, and I could not find an existing issue for this bug
### Current Behavior
Currently we have an existing macro where we alter SQL behavior based on presence or absence of a model
```sql
{%- set relation = adapter.get_relation(this.database, this.schema, this.table) -%}
{%- set target_table = model.get('alias', model.get('name')) -%}
{%- set target_relation_exists, target_relation = get_or_create_relation(database=this.database,schema=this.schema,identifier=target_table,type='table') -%}
{%- if not target_relation_exists -%}
< do what needs to be done for first creation >
{%- else -%}
< do what needs to be done for subsequent creation >
```
**The code works fine without any issue**
When trying to unit test this model it fails with below error
_**'str object' has no attribute 'database'.**_
```yaml
unit_tests:
- name: test_dimension
model: target_dim
overrides:
macros:
is_incremental: true
given:
- input: ref('stage_table')
rows:
... rows here
- input: this
rows:
... rows here
expect:
rows:
... rows here
```
narrowed it down to usage of the below line
**`{%- set relation = adapter.get_relation(this.database, this.schema, this.table) -%}`**
### Expected Behavior
The "this" in adapter is treated as a string rather than a relation .
using `{{ this }}` in schema yml is not possible
the "this" inside adapter relation should be a relation
### Steps To Reproduce
create below model and unit test
```sql
{{
config(
materialized='incremental'
)
}}
{% if execute %}
{%- set relation = adapter.get_relation(this.database, this.schema, this.table) -%}
{% endif %}
select * from {{ ref('temp_table') }}
where event_time > (select max(event_time) from {{ this }})
```
### Relevant log output
```shell
13:33:27 Running with dbt=1.8.3
13:33:28 Registered adapter: snowflake=1.8.3
13:33:29 Found 27 models, 15 data tests, 13 sources, 601 macros, 2 unit tests
13:33:29
13:33:32 Concurrency: 5 threads (target='dev')
13:33:32
13:33:32 1 of 1 START unit_test temp::my_incremental_model_incremental_mode .......... [RUN]
13:33:33 1 of 1 ERROR temp::my_incremental_model_incremental_mode .................... [ERROR in 0.09s]
13:33:33
13:33:33 Finished running 1 unit test in 0 hours 0 minutes and 3.81 seconds (3.81s).
13:33:33
13:33:33 Completed with 1 error and 0 warnings:
13:33:33
13:33:33 Compilation Error in unit_test my_incremental_model_incremental_mode (models\schema.yml)
'str object' has no attribute 'database'. This can happen when calling a macro that does not exist. Check for typos and/or install package dependencies with "dbt deps".
13:33:33
13:33:33 Done. PASS=0 WARN=0 ERROR=1 SKIP=0 TOTAL=1
```
### Environment
```markdown
- OS: windows
- Python: 3.11
- dbt: 1.8.3
```
### Which database adapter are you using with dbt?
snowflake
### Additional Context
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.