-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(fix) generate_database_name macro should be case insensitive (#453)
* add macro test * force lowercase for quoted identifiers * extend behavior to generate_schema_name
- Loading branch information
Showing
3 changed files
with
157 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# | ||
# Models | ||
# | ||
|
||
models__gen_data_macro = """ | ||
select * from {{ ref("seed") }} | ||
""" | ||
|
||
# | ||
# Macros | ||
# | ||
|
||
macros__generate_database_name = """ | ||
{% macro generate_database_name(custom_database_name=none, node=none) -%} | ||
{{ target.database | trim }}_{{ var("build_env") | trim }}_{{ var("org_prefix") | trim }} | ||
{%- endmacro %} | ||
""" | ||
|
||
|
||
macros__generate_schema_name = """ | ||
{% macro generate_schema_name(custom_schema_name=none, node=none) -%} | ||
{{ target.schema | trim }}_{{ var("build_env") | trim }}_{{ var("org_prefix") | trim }} | ||
{%- endmacro %} | ||
""" | ||
|
||
# | ||
# Seeds | ||
# | ||
|
||
seeds__example_seed_csv = """a,b,c | ||
1,2,3 | ||
4,5,6 | ||
7,8,9 | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
""" | ||
Test that the generate database name macro is case insensitive | ||
See DuckDB docs: https://duckdb.org/docs/sql/dialect/keywords_and_identifiers.html | ||
"Identifiers in DuckDB are always case-insensitive, similarly to PostgreSQL. | ||
However, unlike PostgreSQL (and some other major SQL implementations), DuckDB also | ||
treats quoted identifiers as case-insensitive." | ||
""" | ||
from urllib.parse import urlparse | ||
import pytest | ||
|
||
from dbt.tests.util import ( | ||
run_dbt, | ||
check_result_nodes_by_name | ||
) | ||
from tests.functional.plugins.motherduck.fixtures import ( | ||
models__gen_data_macro, | ||
macros__generate_database_name, | ||
macros__generate_schema_name, | ||
seeds__example_seed_csv, | ||
) | ||
|
||
|
||
@pytest.mark.skip_profile("buenavista", "file", "memory") | ||
class TestMacrosGenerateDatabaseName: | ||
@pytest.fixture(scope="class") | ||
def database_name(self, dbt_profile_target, request): | ||
return urlparse(dbt_profile_target["path"]).path + "_ducky_ducky" | ||
|
||
@pytest.fixture(autouse=True) | ||
def run_dbt_scope(self, project, database_name): | ||
project.run_sql(f"CREATE DATABASE IF NOT EXISTS {database_name}") | ||
yield | ||
project.run_sql(f"DROP DATABASE {database_name}") | ||
|
||
@pytest.fixture(scope="class") | ||
def seeds(self): | ||
return { | ||
"seed.csv": seeds__example_seed_csv, | ||
} | ||
|
||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"model.sql": models__gen_data_macro | ||
} | ||
|
||
@pytest.fixture(scope="class") | ||
def macros(self): | ||
return { | ||
"db_name.sql": macros__generate_database_name, | ||
"schema_name.sql": macros__generate_schema_name | ||
} | ||
|
||
@staticmethod | ||
def gen_project_config_update(build_env, org_prefix): | ||
return { | ||
"config-version": 2, | ||
"vars": { | ||
"test": { | ||
"build_env": build_env, | ||
"org_prefix": org_prefix | ||
}, | ||
}, | ||
"macro-paths": ["macros"], | ||
} | ||
|
||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return self.gen_project_config_update("ducky", "ducky") | ||
|
||
def test_dbname_macro(self, project): | ||
# seed command | ||
results = run_dbt(["seed"]) | ||
assert len(results) == 1 | ||
check_result_nodes_by_name(results, ["seed"]) | ||
|
||
for _ in range(3): | ||
results = run_dbt(["run"]) | ||
assert len(results) == 1 | ||
check_result_nodes_by_name(results, ["model"]) | ||
|
||
|
||
@pytest.mark.skip_profile("buenavista", "file", "memory") | ||
class TestMacrosGenerateDatabaseNameUpperCase(TestMacrosGenerateDatabaseName): | ||
@pytest.fixture(scope="class") | ||
def database_name(self, dbt_profile_target, request): | ||
return urlparse(dbt_profile_target["path"]).path + "_ducky_ducky" | ||
|
||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return self.gen_project_config_update("DUCKY", "DUCKY") | ||
|
||
|
||
@pytest.mark.skip_profile("buenavista", "file", "memory") | ||
class TestMacrosGenerateDatabaseNameLowerCase(TestMacrosGenerateDatabaseName): | ||
@pytest.fixture(scope="class") | ||
def database_name(self, dbt_profile_target, request): | ||
return urlparse(dbt_profile_target["path"]).path + "_DUCKY_DUCKY" | ||
|
||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return self.gen_project_config_update("ducky", "ducky") | ||
|
||
|
||
@pytest.mark.skip_profile("buenavista", "file", "memory") | ||
class TestMacrosGenerateDatabaseNameAllMixedCase(TestMacrosGenerateDatabaseName): | ||
@pytest.fixture(scope="class") | ||
def database_name(self, dbt_profile_target, request): | ||
return urlparse(dbt_profile_target["path"]).path + "_dUcKy_DUckY" | ||
|
||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return self.gen_project_config_update("DuCkY", "dUcKy") |