From 066346faa25c8598730f58054df5d731e353017b Mon Sep 17 00:00:00 2001 From: Jeremy Cohen Date: Wed, 18 Jan 2023 22:37:50 +0100 Subject: [PATCH] convert 038_caching_tests (#6612) * convert 038_caching_tests * Adapt for dbt-snowflake * PR feedback * Reformat --- .../038_caching_tests/test_caching.py | 67 ------------ .../dbt/tests/adapter/caching/test_caching.py | 103 ++++++++++++++++++ 2 files changed, 103 insertions(+), 67 deletions(-) delete mode 100644 test/integration/038_caching_tests/test_caching.py create mode 100644 tests/adapter/dbt/tests/adapter/caching/test_caching.py diff --git a/test/integration/038_caching_tests/test_caching.py b/test/integration/038_caching_tests/test_caching.py deleted file mode 100644 index 1967e912628..00000000000 --- a/test/integration/038_caching_tests/test_caching.py +++ /dev/null @@ -1,67 +0,0 @@ -from test.integration.base import DBTIntegrationTest, use_profile -from dbt.adapters.factory import FACTORY - -class TestBaseCaching(DBTIntegrationTest): - @property - def schema(self): - return "caching_038" - - @property - def project_config(self): - return { - 'config-version': 2, - 'quoting': { - 'identifier': False, - 'schema': False, - } - } - - def run_and_get_adapter(self): - # we want to inspect the adapter that dbt used for the run, which is - # not self.adapter. You can't do this until after you've run dbt once. - self.run_dbt(['run']) - return FACTORY.adapters[self.adapter_type] - - def cache_run(self): - adapter = self.run_and_get_adapter() - self.assertEqual(len(adapter.cache.relations), 1) - relation = next(iter(adapter.cache.relations.values())) - self.assertEqual(relation.inner.schema, self.unique_schema()) - self.assertEqual(relation.schema, self.unique_schema().lower()) - - self.run_dbt(['run']) - self.assertEqual(len(adapter.cache.relations), 1) - second_relation = next(iter(adapter.cache.relations.values())) - self.assertEqual(relation, second_relation) - -class TestCachingLowercaseModel(TestBaseCaching): - @property - def models(self): - return "models" - - @use_profile('postgres') - def test_postgres_cache(self): - self.cache_run() - -class TestCachingUppercaseModel(TestBaseCaching): - @property - def models(self): - return "shouting_models" - - @use_profile('postgres') - def test_postgres_cache(self): - self.cache_run() - -class TestCachingSelectedSchemaOnly(TestBaseCaching): - @property - def models(self): - return "models_multi_schemas" - - def run_and_get_adapter(self): - # select only the 'model' in the default schema - self.run_dbt(['--cache-selected-only', 'run', '--select', 'model']) - return FACTORY.adapters[self.adapter_type] - - @use_profile('postgres') - def test_postgres_cache(self): - self.cache_run() diff --git a/tests/adapter/dbt/tests/adapter/caching/test_caching.py b/tests/adapter/dbt/tests/adapter/caching/test_caching.py new file mode 100644 index 00000000000..9cf02309c4c --- /dev/null +++ b/tests/adapter/dbt/tests/adapter/caching/test_caching.py @@ -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