|
| 1 | +import pytest |
| 2 | +from dbt.tests.util import run_dbt, get_relation_columns |
| 3 | + |
| 4 | +# seeds/my_seed.csv |
| 5 | +my_seed_csv = """ |
| 6 | +id,name,some_date |
| 7 | +1,Easton,1981-05-20T06:46:51 |
| 8 | +2,Lillian,1978-09-03T18:10:33 |
| 9 | +3,Jeremiah,1982-03-11T03:59:51 |
| 10 | +4,Nolan,1976-05-06T20:21:35 |
| 11 | +""".lstrip() |
| 12 | + |
| 13 | +# models/my_model.sql |
| 14 | +my_model_sql = """ |
| 15 | +{{config(materialized='table')}} |
| 16 | +select * from {{ ref('my_seed') }} |
| 17 | +""" |
| 18 | + |
| 19 | +# wrapper macro which calls the alter_column_type macro |
| 20 | +alter_column_type_wrapper_macro = """ |
| 21 | +{% macro wrap_alter_column_type() %} |
| 22 | + {%- set relation = adapter.get_relation(database=target.database, schema=schema, identifier='my_model') -%} |
| 23 | + {{ return(adapter.dispatch('alter_column_type')(relation, 'name', 'CLOB')) }} |
| 24 | +{% endmacro %} |
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +class TestAlterColumnDataTypeMacro: |
| 29 | + """ |
| 30 | + Tests the macro `oracle__alter_column_type` |
| 31 | +
|
| 32 | + """ |
| 33 | + |
| 34 | + @pytest.fixture(scope="class") |
| 35 | + def seeds(self): |
| 36 | + return { |
| 37 | + "my_seed.csv": my_seed_csv, |
| 38 | + } |
| 39 | + |
| 40 | + @pytest.fixture(scope="class") |
| 41 | + def models(self): |
| 42 | + return { |
| 43 | + "my_model.sql": my_model_sql, |
| 44 | + } |
| 45 | + |
| 46 | + @pytest.fixture(scope="class") |
| 47 | + def macros(self): |
| 48 | + return {"wrap_alter_column_type.sql": alter_column_type_wrapper_macro} |
| 49 | + |
| 50 | + def test_run_macro(self, project): |
| 51 | + """seed, then run, then macro to alter table column datatype |
| 52 | +
|
| 53 | + """ |
| 54 | + results = run_dbt(['seed']) |
| 55 | + assert len(results) == 1 |
| 56 | + |
| 57 | + results = run_dbt(['run']) |
| 58 | + assert len(results) == 1 |
| 59 | + |
| 60 | + run_dbt(['run-operation', 'wrap_alter_column_type']) |
| 61 | + columns = get_relation_columns(project.adapter, 'my_model') |
| 62 | + expected_columns = [('ID', 'NUMBER', 0), |
| 63 | + ('NAME', 'CLOB', 0), |
| 64 | + ('SOME_DATE', 'TIMESTAMP(6)', 0)] |
| 65 | + assert expected_columns == columns |
| 66 | + |
| 67 | + |
0 commit comments