Skip to content

Commit

Permalink
convert 038_caching_tests (#6612)
Browse files Browse the repository at this point in the history
* convert 038_caching_tests

* Adapt for dbt-snowflake

* PR feedback

* Reformat
  • Loading branch information
jtcohen6 authored Jan 18, 2023
1 parent 0a03355 commit 066346f
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 67 deletions.
67 changes: 0 additions & 67 deletions test/integration/038_caching_tests/test_caching.py

This file was deleted.

103 changes: 103 additions & 0 deletions tests/adapter/dbt/tests/adapter/caching/test_caching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import pytest

from dbt.tests.util import run_dbt

model_sql = """
{{
config(
materialized='table'
)
}}
select 1 as id
"""

another_schema_model_sql = """
{{
config(
materialized='table',
schema='another_schema'
)
}}
select 1 as id
"""


class BaseCachingTest:
@pytest.fixture(scope="class")
def project_config_update(self):
return {
"config-version": 2,
"quoting": {
"identifier": False,
"schema": False,
},
}

def run_and_inspect_cache(self, project, run_args=None):
run_dbt(run_args)

# the cache was empty at the start of the run.
# the model materialization returned an unquoted relation and added to the cache.
adapter = project.adapter
assert len(adapter.cache.relations) == 1
relation = list(adapter.cache.relations).pop()
assert relation.schema == project.test_schema
assert relation.schema == project.test_schema.lower()

# on the second run, dbt will find a relation in the database during cache population.
# this relation will be quoted, because list_relations_without_caching (by default) uses
# quote_policy = {"database": True, "schema": True, "identifier": True}
# when adding relations to the cache.
run_dbt(run_args)
adapter = project.adapter
assert len(adapter.cache.relations) == 1
second_relation = list(adapter.cache.relations).pop()

# perform a case-insensitive + quote-insensitive comparison
for key in ["database", "schema", "identifier"]:
assert getattr(relation, key).lower() == getattr(second_relation, key).lower()

def test_cache(self, project):
self.run_and_inspect_cache(project, run_args=["run"])


class BaseCachingLowercaseModel(BaseCachingTest):
@pytest.fixture(scope="class")
def models(self):
return {
"model.sql": model_sql,
}


class BaseCachingUppercaseModel(BaseCachingTest):
@pytest.fixture(scope="class")
def models(self):
return {
"MODEL.sql": model_sql,
}


class BaseCachingSelectedSchemaOnly(BaseCachingTest):
@pytest.fixture(scope="class")
def models(self):
return {
"model.sql": model_sql,
"another_schema_model.sql": another_schema_model_sql,
}

def test_cache(self, project):
# this should only cache the schema containing the selected model
run_args = ["--cache-selected-only", "run", "--select", "model"]
self.run_and_inspect_cache(project, run_args)


class TestCachingLowerCaseModel(BaseCachingLowercaseModel):
pass


class TestCachingUppercaseModel(BaseCachingUppercaseModel):
pass


class TestCachingSelectedSchemaOnly(BaseCachingSelectedSchemaOnly):
pass

0 comments on commit 066346f

Please sign in to comment.