|
37 | 37 | ) |
38 | 38 | from sqlmesh.core.state_sync.db.snapshot import _snapshot_to_json |
39 | 39 | from sqlmesh.dbt.builtin import _relation_info_to_relation |
| 40 | +from sqlmesh.dbt.common import Dependencies |
40 | 41 | from sqlmesh.dbt.column import ( |
41 | 42 | ColumnConfig, |
42 | 43 | column_descriptions_to_sqlmesh, |
|
50 | 51 | from sqlmesh.dbt.target import BigQueryConfig, DuckDbConfig, SnowflakeConfig, ClickhouseConfig |
51 | 52 | from sqlmesh.dbt.test import TestConfig |
52 | 53 | from sqlmesh.utils.errors import ConfigError, MacroEvalError, SQLMeshError |
| 54 | +from sqlmesh.utils.jinja import MacroReference |
53 | 55 |
|
54 | 56 | pytestmark = [pytest.mark.dbt, pytest.mark.slow] |
55 | 57 |
|
@@ -1530,6 +1532,9 @@ def test_dbt_package_macros(sushi_test_project: Project): |
1530 | 1532 | @pytest.mark.xdist_group("dbt_manifest") |
1531 | 1533 | def test_dbt_vars(sushi_test_project: Project): |
1532 | 1534 | context = sushi_test_project.context |
| 1535 | + context.set_and_render_variables( |
| 1536 | + sushi_test_project.packages["customers"].variables, "customers" |
| 1537 | + ) |
1533 | 1538 |
|
1534 | 1539 | assert context.render("{{ var('some_other_var') }}") == "5" |
1535 | 1540 | assert context.render("{{ var('some_other_var', 0) }}") == "5" |
@@ -1854,3 +1859,65 @@ def test_on_run_start_end(): |
1854 | 1859 | "CREATE OR REPLACE TABLE schema_table_sushi__dev_nested_package AS SELECT 'sushi__dev' AS schema", |
1855 | 1860 | ] |
1856 | 1861 | ) |
| 1862 | + |
| 1863 | + |
| 1864 | +@pytest.mark.xdist_group("dbt_manifest") |
| 1865 | +def test_dynamic_var_names(sushi_test_project: Project, sushi_test_dbt_context: Context): |
| 1866 | + context = sushi_test_project.context |
| 1867 | + context.set_and_render_variables(sushi_test_project.packages["sushi"].variables, "sushi") |
| 1868 | + context.target = BigQueryConfig(name="production", database="main", schema="sushi") |
| 1869 | + model_config = ModelConfig( |
| 1870 | + name="model", |
| 1871 | + alias="model", |
| 1872 | + schema="test", |
| 1873 | + package_name="package", |
| 1874 | + materialized="table", |
| 1875 | + unique_key="ds", |
| 1876 | + partition_by={"field": "ds", "granularity": "month"}, |
| 1877 | + sql=""" |
| 1878 | + {% set var_name = "yet_" + "another_" + "var" %} |
| 1879 | + {% set results = run_query('select 1 as one') %} |
| 1880 | + {% if results %} |
| 1881 | + SELECT {{ results.columns[0].values()[0] }} AS one {{ var(var_name) }} AS var FROM {{ this.identifier }} |
| 1882 | + {% else %} |
| 1883 | + SELECT NULL AS one {{ var(var_name) }} AS var FROM {{ this.identifier }} |
| 1884 | + {% endif %} |
| 1885 | + """, |
| 1886 | + dependencies=Dependencies(has_dynamic_var_names=True), |
| 1887 | + ) |
| 1888 | + converted_model = model_config.to_sqlmesh(context) |
| 1889 | + assert "yet_another_var" in converted_model.jinja_macros.global_objs["vars"] # type: ignore |
| 1890 | + |
| 1891 | + # Test the existing model in the sushi project |
| 1892 | + assert ( |
| 1893 | + "dynamic_test_var" # type: ignore |
| 1894 | + in sushi_test_dbt_context.get_model( |
| 1895 | + "sushi.waiter_revenue_by_day_v2" |
| 1896 | + ).jinja_macros.global_objs["vars"] |
| 1897 | + ) |
| 1898 | + |
| 1899 | + |
| 1900 | +@pytest.mark.xdist_group("dbt_manifest") |
| 1901 | +def test_dynamic_var_names_in_macro(sushi_test_project: Project): |
| 1902 | + context = sushi_test_project.context |
| 1903 | + context.set_and_render_variables(sushi_test_project.packages["sushi"].variables, "sushi") |
| 1904 | + context.target = BigQueryConfig(name="production", database="main", schema="sushi") |
| 1905 | + model_config = ModelConfig( |
| 1906 | + name="model", |
| 1907 | + alias="model", |
| 1908 | + schema="test", |
| 1909 | + package_name="package", |
| 1910 | + materialized="table", |
| 1911 | + unique_key="ds", |
| 1912 | + partition_by={"field": "ds", "granularity": "month"}, |
| 1913 | + sql=""" |
| 1914 | + {% set var_name = "dynamic_" + "test_" + "var" %} |
| 1915 | + SELECT {{ sushi.dynamic_var_name_dependency(var_name) }} AS var |
| 1916 | + """, |
| 1917 | + dependencies=Dependencies( |
| 1918 | + macros=[MacroReference(package="sushi", name="dynamic_var_name_dependency")], |
| 1919 | + has_dynamic_var_names=True, |
| 1920 | + ), |
| 1921 | + ) |
| 1922 | + converted_model = model_config.to_sqlmesh(context) |
| 1923 | + assert "dynamic_test_var" in converted_model.jinja_macros.global_objs["vars"] # type: ignore |
0 commit comments