|
| 1 | +from urllib.parse import urlparse |
| 2 | +import pytest |
| 3 | +from dbt.tests.util import ( |
| 4 | + run_dbt, |
| 5 | +) |
| 6 | +from dbt.artifacts.schemas.results import RunStatus |
| 7 | + |
| 8 | +from dbt.adapters.duckdb.environments.motherduck import MOTHERDUCK_SAAS_MODE_QUERY |
| 9 | + |
| 10 | +random_logs_sql = """ |
| 11 | +{{ config(materialized='table', meta=dict(temp_schema_name='dbt_temp_test')) }} |
| 12 | +
|
| 13 | +select |
| 14 | + uuid()::varchar as log_id, |
| 15 | + '2023-10-01'::timestamp + interval 1 minute * (random() * 20000)::int as dt , |
| 16 | +(random() * 4)::int64 as user_id |
| 17 | +from generate_series(1, 10000) g(x) |
| 18 | +""" |
| 19 | + |
| 20 | +summary_of_logs_sql = """ |
| 21 | +{{ |
| 22 | + config( |
| 23 | + materialized='incremental', |
| 24 | + meta=dict(temp_schema_name='dbt_temp_test'), |
| 25 | + ) |
| 26 | +}} |
| 27 | +
|
| 28 | +select dt::date as dt, user_id, count(1) as c |
| 29 | +from {{ ref('random_logs_test') }} |
| 30 | +
|
| 31 | +
|
| 32 | +{% if is_incremental() %} |
| 33 | +
|
| 34 | + -- this filter will only be applied on an incremental run |
| 35 | + -- (uses > to include records whose timestamp occurred since the last run of this model) |
| 36 | + where dt > '2023-10-08'::timestamp |
| 37 | +
|
| 38 | +{% endif %} |
| 39 | +group by all |
| 40 | +""" |
| 41 | + |
| 42 | +python_pyarrow_table_model = """ |
| 43 | +import pyarrow as pa |
| 44 | +
|
| 45 | +def model(dbt, con): |
| 46 | + return pa.Table.from_pydict({"a": [1,2,3]}) |
| 47 | +""" |
| 48 | + |
| 49 | +@pytest.mark.skip_profile("buenavista", "file", "memory") |
| 50 | +class TestMDPluginSaaSMode: |
| 51 | + @pytest.fixture(scope="class") |
| 52 | + def profiles_config_update(self, dbt_profile_target): |
| 53 | + md_config = {"motherduck_token": dbt_profile_target.get("token"), "motherduck_saas_mode": True} |
| 54 | + return { |
| 55 | + "test": { |
| 56 | + "outputs": { |
| 57 | + "dev": { |
| 58 | + "type": "duckdb", |
| 59 | + "path": dbt_profile_target.get("path", ":memory:") + "?user=1", |
| 60 | + "config_options": md_config, |
| 61 | + } |
| 62 | + }, |
| 63 | + "target": "dev", |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @pytest.fixture(scope="class") |
| 68 | + def models(self, md_sql): |
| 69 | + return { |
| 70 | + "md_table.sql": md_sql, |
| 71 | + "random_logs_test.sql": random_logs_sql, |
| 72 | + "summary_of_logs_test.sql": summary_of_logs_sql, |
| 73 | + "python_pyarrow_table_model.py": python_pyarrow_table_model, |
| 74 | + } |
| 75 | + |
| 76 | + @pytest.fixture(scope="class") |
| 77 | + def database_name(self, dbt_profile_target): |
| 78 | + return urlparse(dbt_profile_target["path"]).path |
| 79 | + |
| 80 | + @pytest.fixture(scope="class") |
| 81 | + def md_sql(self, database_name): |
| 82 | + # Reads from a MD database in my test account in the cloud |
| 83 | + return f""" |
| 84 | + select * FROM {database_name}.main.plugin_table |
| 85 | + """ |
| 86 | + |
| 87 | + @pytest.fixture(autouse=True) |
| 88 | + def run_dbt_scope(self, project, database_name): |
| 89 | + # CREATE DATABASE does not work with SaaS mode on duckdb 1.0.0 |
| 90 | + # This will be fixed in duckdb 1.1.1 |
| 91 | + # project.run_sql(f"CREATE DATABASE IF NOT EXISTS {database_name}") |
| 92 | + project.run_sql(f"CREATE OR REPLACE TABLE {database_name}.plugin_table (i integer, j string)") |
| 93 | + project.run_sql(f"INSERT INTO {database_name}.plugin_table (i, j) VALUES (1, 'foo')") |
| 94 | + yield |
| 95 | + project.run_sql("DROP VIEW md_table") |
| 96 | + project.run_sql("DROP TABLE random_logs_test") |
| 97 | + project.run_sql("DROP TABLE summary_of_logs_test") |
| 98 | + project.run_sql(f"DROP TABLE {database_name}.plugin_table") |
| 99 | + |
| 100 | + def test_motherduck(self, project): |
| 101 | + (motherduck_saas_mode,) = project.run_sql(MOTHERDUCK_SAAS_MODE_QUERY, fetch="one") |
| 102 | + if str(motherduck_saas_mode).lower() not in ["1", "true"]: |
| 103 | + raise ValueError("SaaS mode was not set") |
| 104 | + result = run_dbt(expect_pass=False) |
| 105 | + expected_msg = "Python models are disabled when MotherDuck SaaS Mode is on." |
| 106 | + assert [_res for _res in result.results if _res.status != RunStatus.Success][0].message == expected_msg |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.skip_profile("buenavista", "file", "memory") |
| 110 | +class TestMDPluginSaaSModeViaAttach(TestMDPluginSaaSMode): |
| 111 | + @pytest.fixture(scope="class") |
| 112 | + def profiles_config_update(self, dbt_profile_target): |
| 113 | + md_config = { |
| 114 | + "token": dbt_profile_target.get("token"), |
| 115 | + "saas_mode": 1 |
| 116 | + } |
| 117 | + plugins = [{"module": "motherduck", "config": md_config}] |
| 118 | + return { |
| 119 | + "test": { |
| 120 | + "outputs": { |
| 121 | + "dev": { |
| 122 | + "type": "duckdb", |
| 123 | + "path": ":memory:", |
| 124 | + "plugins": plugins, |
| 125 | + "attach": [ |
| 126 | + { |
| 127 | + "path": dbt_profile_target.get("path", ":memory:") + "?user=2", |
| 128 | + "type": "motherduck" |
| 129 | + } |
| 130 | + ] |
| 131 | + } |
| 132 | + }, |
| 133 | + "target": "dev", |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | +@pytest.mark.skip_profile("buenavista", "file", "memory") |
| 139 | +class TestMDPluginSaaSModeViaAttachWithSettings(TestMDPluginSaaSMode): |
| 140 | + @pytest.fixture(scope="class") |
| 141 | + def profiles_config_update(self, dbt_profile_target): |
| 142 | + md_setting = { |
| 143 | + "motherduck_token": dbt_profile_target.get("token"), |
| 144 | + "motherduck_saas_mode": True |
| 145 | + } |
| 146 | + return { |
| 147 | + "test": { |
| 148 | + "outputs": { |
| 149 | + "dev": { |
| 150 | + "type": "duckdb", |
| 151 | + "path": ":memory:", |
| 152 | + "attach": [ |
| 153 | + { |
| 154 | + "path": dbt_profile_target.get("path", ":memory:") + "?user=3", |
| 155 | + "type": "motherduck" |
| 156 | + } |
| 157 | + ], |
| 158 | + "settings": md_setting |
| 159 | + } |
| 160 | + }, |
| 161 | + "target": "dev", |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | +@pytest.mark.skip_profile("buenavista", "file", "memory") |
| 167 | +class TestMDPluginSaaSModeViaAttachWithTokenInPath(TestMDPluginSaaSMode): |
| 168 | + @pytest.fixture(scope="class") |
| 169 | + def profiles_config_update(self, dbt_profile_target): |
| 170 | + token = dbt_profile_target.get("token") |
| 171 | + qs = f"?motherduck_token={token}&saas_mode=true&user=4" |
| 172 | + return { |
| 173 | + "test": { |
| 174 | + "outputs": { |
| 175 | + "dev": { |
| 176 | + "type": "duckdb", |
| 177 | + "path": ":memory:", |
| 178 | + "attach": [ |
| 179 | + { |
| 180 | + "path": dbt_profile_target.get("path", ":memory:") + qs, |
| 181 | + "type": "motherduck" |
| 182 | + } |
| 183 | + ] |
| 184 | + } |
| 185 | + }, |
| 186 | + "target": "dev", |
| 187 | + } |
| 188 | + } |
0 commit comments