Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Support Unit Testing on Materialized Views #111

Open
2 tasks done
bdewilde opened this issue May 31, 2024 · 3 comments
Open
2 tasks done

[Feature] Support Unit Testing on Materialized Views #111

bdewilde opened this issue May 31, 2024 · 3 comments
Labels
enhancement New feature or request materialized_views unit tests Issues related to built-in dbt unit testing functionality

Comments

@bdewilde
Copy link

bdewilde commented May 31, 2024

Is this a new bug?

  • I believe this is a new bug
  • I have searched the existing issues, and I could not find an existing issue for this bug

Current Behavior

As of dbt v1.8.1, if a model depends upon another model for which materialized="materialized_view", any unit tests on the former model fail with a compilation error: Not able to get columns for unit test X from relation Y because the relation doesn't exist. If the referenced model is materialized as a table, no error is raised; if the model being tested is a materialized view (and the reference model is a table), no error is raised.

Expected Behavior

I would expect unit tests to function regardless of referenced model's materialization. I don't see this listed as a limitation here.

Steps To Reproduce

file: model1.sql

{{ config(materialized="materialized_view") }}

SELECT 1 AS col1
UNION ALL
SELECT 2 AS col1

file: model2.sql

{{ config(materialized="table") }}

SELECT * FROM {{ ref("model1") }}

file: _unit_tests.yml

unit_tests:
  - name: test_matview
    model: model2
    given:
      - input: ref("model1")
        rows:
          - { col1: 1 }
          - { col1: 2 }
    expect:
      rows:
        - { col1: 1 }
        - { col1: 2 }

$ dbt-postgres test --select test_matview

Relevant log output

Extension executing `dbt test`...
15:52:37  Running with dbt=1.8.1
15:52:37  Registered adapter: postgres=1.8.1
15:52:37  Unable to do partial parsing because saved manifest not found. Starting full parse.
15:52:41  Found 2 models, ...
15:52:41
15:52:41  Concurrency: 2 threads (target='dev')
15:52:41
15:52:41  1 of 1 START unit_test model2::test_matview .................................... [RUN]
15:52:41  1 of 1 ERROR model2::test_matview .............................................. [ERROR in 0.03s]
15:52:41
15:52:41  Finished running 1 unit test in 0 hours 0 minutes and 0.14 seconds (0.14s).
15:52:41
15:52:41  Completed with 1 error and 0 warnings:
15:52:41
15:52:41    Compilation Error in model model1 (models/_unit_tests.yml)
  Not able to get columns for unit test 'model1' from relation "db"."schema"."model1" because the relation doesn't exist

  > in macro get_fixture_sql (macros/unit_test_sql/get_fixture_sql.sql)
  > called by model model1 (models/marts/_unit_tests.yml)

Environment

- OS: macOS 14
- Python: 3.9.13
- dbt-postgres: 1.8.1

Additional Context

No response

@bdewilde bdewilde added bug Something isn't working triage labels May 31, 2024
@amychen1776
Copy link

Thank you so much for providing such an easy, reproducible example! We will take a look at this

@amychen1776 amychen1776 changed the title [Bug] Unit tests don't work if referenced model is a materialized view [Feature] Support Unit Testing on Materialized Views Aug 28, 2024
@amychen1776 amychen1776 added enhancement New feature or request unit tests Issues related to built-in dbt unit testing functionality and removed bug Something isn't working labels Aug 28, 2024
matthewshaver pushed a commit to dbt-labs/docs.getdbt.com that referenced this issue Aug 29, 2024
…ized view` materialization (#5986)

[Preview](https://docs-getdbt-com-git-dbeatty10-patch-1-dbt-labs.vercel.app/docs/build/unit-tests#before-you-begin)

## What are you changing in this pull request and why?

We currently don't support unit testing models that use the
[`materialized
view`](https://docs.getdbt.com/docs/build/materializations#materialized-view)
materialization=

See dbt-labs/dbt-postgres#111

## Checklist
- [x] Review the [Content style
guide](https://github.com/dbt-labs/docs.getdbt.com/blob/current/contributing/content-style-guide.md)
so my content adheres to these guidelines.

---------

Co-authored-by: Mirna Wong <[email protected]>
@bdewilde
Copy link
Author

Hi @amychen1776 , just making sure I'm following: The decision was not to support this functionality, but to document that it isn't supported?

@amychen1776
Copy link

@bdewilde For the short term, we are going to document this limitation. But we do want to tackle this support and the core team is going to look into scoping this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request materialized_views unit tests Issues related to built-in dbt unit testing functionality
Projects
None yet
Development

No branches or pull requests

3 participants