|
| 1 | +import os |
| 2 | +import pytest |
| 3 | +from dbt.tests.util import run_dbt, check_relations_equal, get_relation_columns |
| 4 | + |
| 5 | +class BaseDataTypeMacro: |
| 6 | + @pytest.fixture(scope="class") |
| 7 | + def packages(self): |
| 8 | + return {"packages": [{"local": os.getcwd()}]} |
| 9 | + |
| 10 | + def is_legacy(self): |
| 11 | + return False |
| 12 | + |
| 13 | + def test_check_types_assert_match(self, project): |
| 14 | + run_dbt(['deps']) |
| 15 | + run_dbt(['build']) |
| 16 | + |
| 17 | + # check contents equal |
| 18 | + check_relations_equal(project.adapter, ["expected", "actual"]) |
| 19 | + |
| 20 | + # check types equal |
| 21 | + expected_cols = get_relation_columns(project.adapter, "expected") |
| 22 | + actual_cols = get_relation_columns(project.adapter, "actual") |
| 23 | + print(f"Expected: {expected_cols}") |
| 24 | + print(f"Actual: {actual_cols}") |
| 25 | + |
| 26 | + |
| 27 | + if not self.is_legacy(): |
| 28 | + assert expected_cols == actual_cols, f"Type difference detected: {expected_cols} vs. {actual_cols}" |
| 29 | + # we need to be a little more lenient when mapping between 'legacy' and 'new' types that are equivalent |
| 30 | + # e.g. 'character varying' and 'text' |
| 31 | + elif expected_cols == actual_cols: |
| 32 | + # cool, no need for jank |
| 33 | + pass |
| 34 | + else: |
| 35 | + # this is pretty janky |
| 36 | + for i in range(0, len(expected_cols)): |
| 37 | + expected = project.adapter.Column(*expected_cols[i]) |
| 38 | + actual = project.adapter.Column(*actual_cols[i]) |
| 39 | + print(f"Subtle type difference detected: {expected.data_type} vs. {actual.data_type}") |
| 40 | + if any(( |
| 41 | + expected.is_string() and actual.is_string(), |
| 42 | + expected.is_float() and actual.is_float(), |
| 43 | + expected.is_integer() and actual.is_integer(), |
| 44 | + expected.is_numeric() and actual.is_numeric(), |
| 45 | + )): |
| 46 | + pytest.xfail() |
| 47 | + else: |
| 48 | + pytest.fail() |
0 commit comments