Skip to content

Commit

Permalink
refactor sources tests + pass unrendered properties to SourceDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed Sep 26, 2024
1 parent e924bdc commit 1213867
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 37 deletions.
2 changes: 2 additions & 0 deletions core/dbt/parser/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ def parse_source(self, target: UnpatchedSourceDefinition) -> SourceDefinition:
parsed_source = SourceDefinition(
package_name=target.package_name,
database=(source.database or default_database),
unrendered_database=source.unrendered_database,
schema=(source.schema or source.name),
unrendered_schema=source.unrendered_schema,
identifier=(table.identifier or table.name),
path=target.path,
original_file_path=target.original_file_path,
Expand Down
68 changes: 51 additions & 17 deletions tests/functional/defer_state/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,23 +560,6 @@
"""

schema_source_with_env_var_as_database_property_yml = """
sources:
- name: jaffle_shop
database: "{{ env_var('DBT_TEST_STATE_MODIFIED') }}"
tables:
- name: customers
"""

schema_source_with_env_var_as_schema_property_yml = """
sources:
- name: jaffle_shop
database: "test"
schema: "{{ env_var('DBT_TEST_STATE_MODIFIED') }}"
tables:
- name: customers
"""


model_with_var_in_config_sql = """
{{ config(materialized=var('DBT_TEST_STATE_MODIFIED')) }}
Expand Down Expand Up @@ -612,3 +595,54 @@
config:
materialized: "{{ ('view' if execute else 'table') }}"
"""

schema_source_with_env_var_as_database_property_yml = """
sources:
- name: jaffle_shop
database: "{{ env_var('DBT_TEST_STATE_MODIFIED') }}"
tables:
- name: customers
"""

schema_source_with_env_var_as_schema_property_yml = """
sources:
- name: jaffle_shop
database: "test"
schema: "{{ env_var('DBT_TEST_STATE_MODIFIED') }}"
tables:
- name: customers
"""

schema_source_with_jinja_as_database_property_yml = """
sources:
- name: jaffle_shop
database: "{{ ('foo' if execute else 'bar') }}"
tables:
- name: customers
"""

schema_source_with_updated_jinja_as_database_property_yml = """
sources:
- name: jaffle_shop
database: "{{ ('bar' if execute else 'foo') }}"
tables:
- name: customers
"""

schema_source_with_jinja_as_schema_property_yml = """
sources:
- name: jaffle_shop
database: "test"
schema: "{{ ('foo' if execute else 'bar') }}"
tables:
- name: customers
"""

schema_source_with_updated_jinja_as_schema_property_yml = """
sources:
- name: jaffle_shop
database: "test"
schema: "{{ ('bar' if execute else 'foo') }}"
tables:
- name: customers
"""
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
model_with_env_var_in_config_sql,
model_with_no_in_config_sql,
schema_model_with_env_var_in_config_yml,
schema_source_with_env_var_as_database_property_yml,
schema_source_with_env_var_as_schema_property_yml,
)
from tests.functional.defer_state.test_modified_state import BaseModifiedState

Expand Down Expand Up @@ -108,19 +106,3 @@ def models(self):
"model.sql": model_with_env_var_in_config_sql,
"schema.yml": schema_model_with_env_var_in_config_yml,
}


class TestSourceNodeWithEnvVarConfigInDatabase(BaseTestStateSelectionEnvVarConfig):
@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": schema_source_with_env_var_as_database_property_yml,
}


class TestSourceNodeWithEnvVarConfigInSchema(BaseTestStateSelectionEnvVarConfig):
@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": schema_source_with_env_var_as_schema_property_yml,
}
3 changes: 1 addition & 2 deletions tests/functional/defer_state/test_modified_state_jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def project_config_update(self):
def update_jinja_expression_in_config(self, project):
pass

def test_change_target_jinja_if(self, project, dbt_profile_data, profiles_root):
# Generate ./state without changing target.name
def test_change_jinja_if(self, project):
run_dbt(["run"])
self.copy_state()
# Model is table when execute = True
Expand Down
113 changes: 113 additions & 0 deletions tests/functional/defer_state/test_modified_state_sources_unrendered.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import pytest

from dbt.tests.util import get_artifact, run_dbt, write_file
from tests.functional.defer_state.fixtures import (
schema_source_with_env_var_as_database_property_yml,
schema_source_with_env_var_as_schema_property_yml,
schema_source_with_jinja_as_database_property_yml,
schema_source_with_jinja_as_schema_property_yml,
schema_source_with_updated_jinja_as_database_property_yml,
schema_source_with_updated_jinja_as_schema_property_yml,
)
from tests.functional.defer_state.test_modified_state import BaseModifiedState
from tests.functional.defer_state.test_modified_state_environment_vars import (
BaseTestStateSelectionEnvVarConfig,
)


class TestSourceNodeWithEnvVarConfigInDatabase(BaseTestStateSelectionEnvVarConfig):
@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": schema_source_with_env_var_as_database_property_yml,
}


class TestSourceNodeWithEnvVarConfigInSchema(BaseTestStateSelectionEnvVarConfig):
@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": schema_source_with_env_var_as_schema_property_yml,
}


class TestSourceNodeWithJinjaInDatabase(BaseModifiedState):
@pytest.fixture(scope="class")
def project_config_update(self):
return {
"flags": {
"state_modified_compare_more_unrendered_values": True,
}
}

def update_jinja_expression_in_config(self, project):
write_file(
schema_source_with_updated_jinja_as_database_property_yml,
project.project_root,
"models",
"schema.yml",
)

@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": schema_source_with_jinja_as_database_property_yml,
}

def test_change_jinja_if(self, project):
run_dbt(["run"])
self.copy_state()
# source database is 'bar' when execute = False
manifest_json = get_artifact(project.project_root, "target", "manifest.json")
assert manifest_json["sources"]["source.test.jaffle_shop.customers"]["database"] == "bar"
assert (
manifest_json["sources"]["source.test.jaffle_shop.customers"]["unrendered_database"]
== "{{ ('foo' if execute else 'bar') }}"
)

# Assert no false positive (execute = False)
results = run_dbt(["list", "-s", "state:modified", "--state", "./state"])
assert len(results) == 0

# Update unrendered config (change jinja expression)
self.update_jinja_expression_in_config(project)
# Assert no false negatives (jinja expression has changed)
results = run_dbt(["list", "-s", "state:modified", "--state", "./state"])
assert len(results) == 1


class TestSourceNodeWithJinjaInSchema(BaseModifiedState):
def update_jinja_expression_in_config(self, project):
write_file(
schema_source_with_updated_jinja_as_schema_property_yml,
project.project_root,
"models",
"schema.yml",
)

@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": schema_source_with_jinja_as_schema_property_yml,
}

def test_change_jinja_if(self, project):
run_dbt(["run"])
self.copy_state()
# source database is 'bar' when execute = False
manifest_json = get_artifact(project.project_root, "target", "manifest.json")
assert manifest_json["sources"]["source.test.jaffle_shop.customers"]["schema"] == "bar"
assert (
manifest_json["sources"]["source.test.jaffle_shop.customers"]["unrendered_schema"]
== "{{ ('foo' if execute else 'bar') }}"
)

# Assert no false positive (execute = False)
results = run_dbt(["list", "-s", "state:modified", "--state", "./state"])
assert len(results) == 0

# Update unrendered config (change jinja expression)
self.update_jinja_expression_in_config(project)
# Assert no false negatives (jinja expression has changed)
results = run_dbt(["list", "-s", "state:modified", "--state", "./state"])
assert len(results) == 1

0 comments on commit 1213867

Please sign in to comment.