|
| 1 | +import pytest |
| 2 | + |
| 3 | +from dbt.tests.util import run_dbt, check_relations_equal |
| 4 | +from dbt.tests.fixtures.project import write_project_files |
| 5 | + |
| 6 | + |
| 7 | +tests__get_columns_in_relation_sql = """ |
| 8 | +{% set columns = adapter.get_columns_in_relation(ref('model')) %} |
| 9 | +{% set limit_query = 0 %} |
| 10 | +{% if (columns | length) == 0 %} |
| 11 | + {% set limit_query = 1 %} |
| 12 | +{% endif %} |
| 13 | +
|
| 14 | +select 1 as id limit {{ limit_query }} |
| 15 | +
|
| 16 | +""" |
| 17 | + |
| 18 | +models__upstream_sql = """ |
| 19 | +select 1 as id |
| 20 | +
|
| 21 | +""" |
| 22 | + |
| 23 | +models__expected_sql = """ |
| 24 | +-- make sure this runs after 'model' |
| 25 | +-- {{ ref('model') }} |
| 26 | +select 2 as id |
| 27 | +
|
| 28 | +""" |
| 29 | + |
| 30 | +models__model_sql = """ |
| 31 | +
|
| 32 | +{% set upstream = ref('upstream') %} |
| 33 | +
|
| 34 | +{% if execute %} |
| 35 | + {# don't ever do any of this #} |
| 36 | + {%- do adapter.drop_schema(upstream) -%} |
| 37 | + {% set existing = adapter.get_relation(upstream.database, upstream.schema, upstream.identifier) %} |
| 38 | + {% if existing is not none %} |
| 39 | + {% do exceptions.raise_compiler_error('expected ' ~ ' to not exist, but it did') %} |
| 40 | + {% endif %} |
| 41 | +
|
| 42 | + {%- do adapter.create_schema(upstream) -%} |
| 43 | +
|
| 44 | + {% set sql = create_view_as(upstream, 'select 2 as id') %} |
| 45 | + {% do run_query(sql) %} |
| 46 | +{% endif %} |
| 47 | +
|
| 48 | +
|
| 49 | +select * from {{ upstream }} |
| 50 | +
|
| 51 | +""" |
| 52 | + |
| 53 | + |
| 54 | +class BaseAdapterMethod: |
| 55 | + """ |
| 56 | + This test will leverage the following adapter methods: |
| 57 | + get_relation |
| 58 | + get_columns_in_relation |
| 59 | + drop_schema |
| 60 | + create_schema |
| 61 | + It will aims to make sure drop_shema actually works, for more context |
| 62 | + checkout #1983 |
| 63 | + """ |
| 64 | + |
| 65 | + @pytest.fixture(scope="class") |
| 66 | + def tests(self): |
| 67 | + return {"get_columns_in_relation.sql": tests__get_columns_in_relation_sql} |
| 68 | + |
| 69 | + @pytest.fixture(scope="class") |
| 70 | + def models(self): |
| 71 | + return { |
| 72 | + "upstream.sql": models__upstream_sql, |
| 73 | + "expected.sql": models__expected_sql, |
| 74 | + "model.sql": models__model_sql, |
| 75 | + } |
| 76 | + |
| 77 | + @pytest.fixture(scope="class") |
| 78 | + def project_files( |
| 79 | + self, |
| 80 | + project_root, |
| 81 | + tests, |
| 82 | + models, |
| 83 | + ): |
| 84 | + write_project_files(project_root, "tests", tests) |
| 85 | + write_project_files(project_root, "models", models) |
| 86 | + |
| 87 | + @pytest.fixture(scope="class") |
| 88 | + def project_config_update(self): |
| 89 | + return { |
| 90 | + "name": "adapter_methods", |
| 91 | + } |
| 92 | + |
| 93 | + @pytest.fixture(autouse=True) |
| 94 | + def clean_up(self, project): |
| 95 | + yield |
| 96 | + with project.adapter.connection_named("__test"): |
| 97 | + relation = project.adapter.Relation.create( |
| 98 | + database=project.database, schema=project.test_schema |
| 99 | + ) |
| 100 | + project.adapter.drop_schema(relation) |
| 101 | + |
| 102 | + pass |
| 103 | + |
| 104 | + # snowflake need all tables in CAP name |
| 105 | + @pytest.fixture(scope="class") |
| 106 | + def equal_tables(self): |
| 107 | + return ["model", "expected"] |
| 108 | + |
| 109 | + def test_adapter_methods(self, project, equal_tables): |
| 110 | + run_dbt(["compile"]) # trigger any compile-time issues |
| 111 | + result = run_dbt() |
| 112 | + assert len(result) == 3 |
| 113 | + check_relations_equal(project.adapter, equal_tables) |
| 114 | + |
| 115 | + |
| 116 | +class TestBaseCaching(BaseAdapterMethod): |
| 117 | + pass |
0 commit comments