|
| 1 | +""" |
| 2 | +Integration tests for the upload_only_current_project_artifacts config var. |
| 3 | +
|
| 4 | +When enabled, artifact uploads should only include resources from the current |
| 5 | +project (by package_name), excluding artifacts from dependency packages. |
| 6 | +When disabled (default), all artifacts including dependencies should be uploaded. |
| 7 | +""" |
| 8 | + |
| 9 | +import uuid |
| 10 | + |
| 11 | +from dbt_project import DbtProject |
| 12 | + |
| 13 | +TEST_MODEL = "one" |
| 14 | + |
| 15 | + |
| 16 | +def test_default_includes_dependency_artifacts(dbt_project: DbtProject): |
| 17 | + """ |
| 18 | + By default (upload_only_current_project_artifacts=false), artifacts from |
| 19 | + dependency packages (like 'elementary') should be present in dbt_models. |
| 20 | + """ |
| 21 | + dbt_project.dbt_runner.vars["disable_dbt_artifacts_autoupload"] = False |
| 22 | + dbt_project.dbt_runner.vars["cache_artifacts"] = False |
| 23 | + |
| 24 | + dbt_project.dbt_runner.run(select=TEST_MODEL) |
| 25 | + |
| 26 | + all_models = dbt_project.read_table("dbt_models", raise_if_empty=True) |
| 27 | + package_names = {row["package_name"] for row in all_models} |
| 28 | + |
| 29 | + assert "elementary" in package_names, ( |
| 30 | + "Expected 'elementary' package artifacts to be present by default, " |
| 31 | + f"but only found packages: {package_names}" |
| 32 | + ) |
| 33 | + assert "elementary_tests" in package_names, ( |
| 34 | + "Expected 'elementary_tests' package artifacts to be present, " |
| 35 | + f"but only found packages: {package_names}" |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def test_filtering_excludes_dependency_artifacts(dbt_project: DbtProject): |
| 40 | + """ |
| 41 | + When upload_only_current_project_artifacts=true, only artifacts from the |
| 42 | + current project should be uploaded — dependency packages like 'elementary' |
| 43 | + should be excluded. |
| 44 | +
|
| 45 | + We first clear the dbt_models table, then run with filtering enabled via |
| 46 | + the on-run-end hook. The diff upload sees an empty table and inserts all |
| 47 | + (filtered) artifacts, so only current-project rows end up in the table. |
| 48 | + """ |
| 49 | + # Clear existing rows so the diff upload will insert fresh filtered data. |
| 50 | + dbt_project.run_query("DELETE FROM {{ ref('dbt_models') }} WHERE 1=1") |
| 51 | + |
| 52 | + dbt_project.dbt_runner.vars["disable_dbt_artifacts_autoupload"] = False |
| 53 | + dbt_project.dbt_runner.vars["upload_only_current_project_artifacts"] = True |
| 54 | + |
| 55 | + dbt_project.dbt_runner.run(select=TEST_MODEL) |
| 56 | + |
| 57 | + all_models = dbt_project.read_table("dbt_models", raise_if_empty=True) |
| 58 | + package_names = {row["package_name"] for row in all_models} |
| 59 | + |
| 60 | + assert package_names == {"elementary_tests"}, ( |
| 61 | + "Expected only 'elementary_tests' artifacts when filtering is enabled, " |
| 62 | + f"but found packages: {package_names}" |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def test_filtering_applies_to_tests(dbt_project: DbtProject): |
| 67 | + """ |
| 68 | + When upload_only_current_project_artifacts=true, tests from the current |
| 69 | + project should still be uploaded. |
| 70 | + """ |
| 71 | + unique_id = str(uuid.uuid4()).replace("-", "_") |
| 72 | + model_name = f"filter_test_model_{unique_id}" |
| 73 | + model_sql = "select 1 as col" |
| 74 | + schema_yaml = { |
| 75 | + "version": 2, |
| 76 | + "models": [ |
| 77 | + { |
| 78 | + "name": model_name, |
| 79 | + "columns": [{"name": "col", "tests": ["unique"]}], |
| 80 | + } |
| 81 | + ], |
| 82 | + } |
| 83 | + |
| 84 | + with dbt_project.write_yaml( |
| 85 | + schema_yaml, name=f"schema_filter_test_{unique_id}.yml" |
| 86 | + ): |
| 87 | + dbt_model_path = dbt_project.models_dir_path / "tmp" / f"{model_name}.sql" |
| 88 | + dbt_model_path.parent.mkdir(parents=True, exist_ok=True) |
| 89 | + dbt_model_path.write_text(model_sql) |
| 90 | + try: |
| 91 | + # Clear existing rows so the diff upload inserts fresh filtered data. |
| 92 | + dbt_project.run_query("DELETE FROM {{ ref('dbt_tests') }} WHERE 1=1") |
| 93 | + |
| 94 | + dbt_project.dbt_runner.vars["disable_dbt_artifacts_autoupload"] = False |
| 95 | + dbt_project.dbt_runner.vars["upload_only_current_project_artifacts"] = True |
| 96 | + |
| 97 | + dbt_project.dbt_runner.run(select=model_name) |
| 98 | + |
| 99 | + tests = dbt_project.read_table("dbt_tests", raise_if_empty=True) |
| 100 | + test_packages = {row["package_name"] for row in tests} |
| 101 | + assert test_packages == {"elementary_tests"}, ( |
| 102 | + "Expected only 'elementary_tests' tests when filtering is enabled, " |
| 103 | + f"but found packages: {test_packages}" |
| 104 | + ) |
| 105 | + finally: |
| 106 | + if dbt_model_path.exists(): |
| 107 | + dbt_model_path.unlink() |
0 commit comments