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

add test for unit test that depends on ephemeral model #9929

Merged
merged 12 commits into from
Apr 17, 2024
84 changes: 84 additions & 0 deletions tests/functional/unit_testing/test_ut_ephemeral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import pytest
from dbt.tests.util import run_dbt, write_file
from dbt.contracts.results import RunStatus, TestStatus


ephemeral_model_sql = """
{{ config(materialized="ephemeral") }}
select 1 as id, 'Emily' as first_name
"""

nested_ephemeral_model_sql = """
{{ config(materialized="ephemeral") }}
select * from {{ ref('ephemeral_model') }}
"""

customers_sql = """
select * from {{ ref('nested_ephemeral_model') }}
"""

test_sql_format_yml = """
unit_tests:
- name: test_customers
model: customers
given:
- input: ref('nested_ephemeral_model')
format: sql
rows: |
select 1 as id, 'Emily' as first_name
expect:
rows:
- {id: 1, first_name: Emily}
"""

failing_test_sql_format_yml = """
- name: fail_test_customers
model: customers
given:
- input: ref('nested_ephemeral_model')
format: sql
rows: |
select 1 as id, 'Emily' as first_name
expect:
rows:
- {id: 1, first_name: Joan}
"""


class TestUnitTestEphemeralInput:
@pytest.fixture(scope="class")
def models(self):
return {
"customers.sql": customers_sql,
"ephemeral_model.sql": ephemeral_model_sql,
"nested_ephemeral_model.sql": nested_ephemeral_model_sql,
"tests.yml": test_sql_format_yml,
}

def test_ephemeral_input(self, project):
results = run_dbt(["run"])
len(results) == 1

results = run_dbt(["test", "--select", "test_type:unit"])
assert len(results) == 1

results = run_dbt(["build"])
assert len(results) == 2
result_unique_ids = [result.node.unique_id for result in results]
assert len(result_unique_ids) == 2
assert "unit_test.test.customers.test_customers" in result_unique_ids

# write failing unit test
write_file(
test_sql_format_yml + failing_test_sql_format_yml,
project.project_root,
"models",
"tests.yml",
)
results = run_dbt(["build"], expect_pass=False)
for result in results:
if result.node.unique_id == "model.test.customers":
assert result.status == RunStatus.Skipped
elif result.node.unique_id == "unit_test.test.customers.fail_test_customers":
assert result.status == TestStatus.Fail
assert len(results) == 3