|
| 1 | +import pytest |
| 2 | +from dbt.tests.util import run_dbt, check_relations_equal, check_relations_equal_with_relations |
| 3 | +from tests.functional.test_override_database.fixtures import ( |
| 4 | + models, |
| 5 | + seeds, |
| 6 | + project_files |
| 7 | +) |
| 8 | +import os |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +class BaseOverrideDatabase: |
| 14 | + @pytest.fixture(scope="class") |
| 15 | + def model_path(self): |
| 16 | + return "models" |
| 17 | + |
| 18 | + @pytest.fixture(scope="class") |
| 19 | + def project_config_update(self): |
| 20 | + return { |
| 21 | + "config-version": 2, |
| 22 | + "seed-paths": ["seeds"], |
| 23 | + "vars": { |
| 24 | + "alternate_db": os.getenv("BIGQUERY_TEST_ALT_DATABASE"), |
| 25 | + }, |
| 26 | + "quoting": { |
| 27 | + "database": True, |
| 28 | + }, |
| 29 | + "seeds": { |
| 30 | + "quote_columns": False, |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | +class TestModelOverrideBigQuery(BaseOverrideDatabase): |
| 36 | + def run_database_override(self, project): |
| 37 | + run_dbt(["seed"]) |
| 38 | + assert len(run_dbt(["run"])) == 4 |
| 39 | + check_relations_equal_with_relations(project.adapter, [ |
| 40 | + project.adapter.Relation.create(schema=project.test_schema, identifier="seed"), |
| 41 | + project.adapter.Relation.create(database=os.getenv("BIGQUERY_TEST_ALT_DATABASE"), schema=project.test_schema, identifier="view_2"), |
| 42 | + project.adapter.Relation.create(schema=project.test_schema, identifier="view_1"), |
| 43 | + project.adapter.Relation.create(schema=project.test_schema, identifier="view_3"), |
| 44 | + project.adapter.Relation.create(database=os.getenv("BIGQUERY_TEST_ALT_DATABASE"), schema=project.test_schema, identifier="view_4") |
| 45 | + ]) |
| 46 | + |
| 47 | + |
| 48 | + def test_bigquery_database_override(self, project): |
| 49 | + self.run_database_override(project) |
| 50 | + |
| 51 | + |
| 52 | +class BaseTestProjectModelOverrideBigQuery(BaseOverrideDatabase): |
| 53 | + |
| 54 | + def run_database_override(self, project): |
| 55 | + run_dbt(["seed"]) |
| 56 | + assert len(run_dbt(["run"])) == 4 |
| 57 | + self.assertExpectedRelations(project) |
| 58 | + |
| 59 | + def assertExpectedRelations(self, project): |
| 60 | + check_relations_equal_with_relations(project.adapter, [ |
| 61 | + project.adapter.Relation.create(schema=project.test_schema, identifier="seed"), |
| 62 | + project.adapter.Relation.create(database=os.getenv("BIGQUERY_TEST_ALT_DATABASE"), schema=project.test_schema, identifier="view_2"), |
| 63 | + project.adapter.Relation.create(database=os.getenv("BIGQUERY_TEST_ALT_DATABASE"), schema=project.test_schema, identifier="view_1"), |
| 64 | + project.adapter.Relation.create(schema=project.test_schema, identifier="view_3"), |
| 65 | + project.adapter.Relation.create(database=os.getenv("BIGQUERY_TEST_ALT_DATABASE"), schema=project.test_schema, identifier="view_4") |
| 66 | + ]) |
| 67 | + |
| 68 | + |
| 69 | +class TestProjectModelOverrideBigQuery(BaseTestProjectModelOverrideBigQuery): |
| 70 | + @pytest.fixture(scope="class") |
| 71 | + def project_config_update(self): |
| 72 | + return { |
| 73 | + "config-version": 2, |
| 74 | + "vars": { |
| 75 | + "alternate_db": os.getenv("BIGQUERY_TEST_ALT_DATABASE"), |
| 76 | + }, |
| 77 | + "models": { |
| 78 | + "database": os.getenv("BIGQUERY_TEST_ALT_DATABASE"), |
| 79 | + "test": { |
| 80 | + "subfolder": { |
| 81 | + "database": "{{ target.database }}" |
| 82 | + } |
| 83 | + } |
| 84 | + }, |
| 85 | + "seed-paths": ["seeds"], |
| 86 | + "vars": { |
| 87 | + "alternate_db": os.getenv("BIGQUERY_TEST_ALT_DATABASE"), |
| 88 | + }, |
| 89 | + "quoting": { |
| 90 | + "database": True, |
| 91 | + }, |
| 92 | + "seeds": { |
| 93 | + "quote_columns": False, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + def test_bigquery_database_override(self, project): |
| 98 | + self.run_database_override(project) |
| 99 | + |
| 100 | + |
| 101 | +class TestProjectModelAliasOverrideBigQuery(BaseTestProjectModelOverrideBigQuery): |
| 102 | + @pytest.fixture(scope="class") |
| 103 | + def project_config_update(self): |
| 104 | + return { |
| 105 | + "config-version": 2, |
| 106 | + "vars": { |
| 107 | + "alternate_db": os.getenv("BIGQUERY_TEST_ALT_DATABASE"), |
| 108 | + }, |
| 109 | + "models": { |
| 110 | + "project": os.getenv("BIGQUERY_TEST_ALT_DATABASE"), |
| 111 | + "test": { |
| 112 | + "subfolder": { |
| 113 | + "project": "{{ target.database }}" |
| 114 | + } |
| 115 | + } |
| 116 | + }, |
| 117 | + "seed-paths": ["seeds"], |
| 118 | + "vars": { |
| 119 | + "alternate_db": os.getenv("BIGQUERY_TEST_ALT_DATABASE"), |
| 120 | + }, |
| 121 | + "quoting": { |
| 122 | + "database": True, |
| 123 | + }, |
| 124 | + "seeds": { |
| 125 | + "quote_columns": False, |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + def test_bigquery_project_override(self, project): |
| 130 | + self.run_database_override(project) |
| 131 | + |
| 132 | + |
| 133 | +class TestProjectSeedOverrideBigQuery(BaseOverrideDatabase): |
| 134 | + @pytest.fixture(scope="class") |
| 135 | + def project_config_update(self): |
| 136 | + return { |
| 137 | + "config-version": 2, |
| 138 | + "seed-paths": ["seeds"], |
| 139 | + "vars": { |
| 140 | + "alternate_db": os.getenv("BIGQUERY_TEST_ALT_DATABASE"), |
| 141 | + }, |
| 142 | + "seeds": { |
| 143 | + "database": os.getenv("BIGQUERY_TEST_ALT_DATABASE") |
| 144 | + } |
| 145 | + } |
| 146 | + def run_database_override(self, project): |
| 147 | + run_dbt(["seed"]) |
| 148 | + assert len(run_dbt(["run"])) == 4 |
| 149 | + check_relations_equal_with_relations(project.adapter, [ |
| 150 | + project.adapter.Relation.create(database=os.getenv("BIGQUERY_TEST_ALT_DATABASE"), schema=project.test_schema, identifier="seed"), |
| 151 | + project.adapter.Relation.create(database=os.getenv("BIGQUERY_TEST_ALT_DATABASE"), schema=project.test_schema, identifier="view_2"), |
| 152 | + project.adapter.Relation.create(schema=project.test_schema, identifier="view_1"), |
| 153 | + project.adapter.Relation.create(schema=project.test_schema, identifier="view_3"), |
| 154 | + project.adapter.Relation.create(database=os.getenv("BIGQUERY_TEST_ALT_DATABASE"), schema=project.test_schema, identifier="view_4") |
| 155 | + ]) |
| 156 | + |
| 157 | + def test_bigquery_database_override(self, project): |
| 158 | + self.run_database_override(project) |
0 commit comments