|
| 1 | +"""Functional coverage for the dbt_sqlserver_use_native_string_types behaviour flag (#626). |
| 2 | +
|
| 3 | +Default (flag off): preserves the pre-existing mappings |
| 4 | + STRING -> VARCHAR(8000), NCHAR -> CHAR(1), NVARCHAR -> VARCHAR(8000). |
| 5 | +
|
| 6 | +Flag on: switches to SQL Server-native mappings |
| 7 | + STRING -> VARCHAR(MAX), NCHAR -> NCHAR(1), NVARCHAR -> NVARCHAR(4000). |
| 8 | +
|
| 9 | +Each path is exercised at two levels: |
| 10 | + - dict assertion on adapter.Column.TYPE_LABELS (cheap sanity check), |
| 11 | + - end-to-end: a contract-enforced model with `data_type: string|nchar|nvarchar` |
| 12 | + is materialised and the resulting column type read back from |
| 13 | + sys.columns + sys.types. |
| 14 | +""" |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from dbt.tests.util import run_dbt |
| 19 | + |
| 20 | +contract_model_sql = """ |
| 21 | +{{ config(materialized='table') }} |
| 22 | +select |
| 23 | + cast('hello' as varchar(50)) as str_col, |
| 24 | + cast('h' as char(1)) as nchar_col, |
| 25 | + cast('hello' as varchar(50)) as nvarchar_col |
| 26 | +""" |
| 27 | + |
| 28 | +contract_model_yml = """ |
| 29 | +version: 2 |
| 30 | +models: |
| 31 | + - name: types_model |
| 32 | + config: |
| 33 | + contract: |
| 34 | + enforced: true |
| 35 | + columns: |
| 36 | + - name: str_col |
| 37 | + data_type: string |
| 38 | + - name: nchar_col |
| 39 | + data_type: nchar |
| 40 | + - name: nvarchar_col |
| 41 | + data_type: nvarchar |
| 42 | +""" |
| 43 | + |
| 44 | + |
| 45 | +def _column_types(project, schema: str, table: str) -> dict: |
| 46 | + """Return {column_name: (data_type, character_maximum_length)} for a table. |
| 47 | +
|
| 48 | + Queries sys.columns + sys.types with a three-part OBJECT_ID so we don't |
| 49 | + depend on the connection's current-database context. |
| 50 | + """ |
| 51 | + # sys.columns.max_length is in bytes; for unicode (n*) types it's two bytes |
| 52 | + # per character, so we halve to get the declared character length. MAX is |
| 53 | + # reported as -1 in both cases. |
| 54 | + rows = project.run_sql( |
| 55 | + f""" |
| 56 | + select c.name, t.name, c.max_length |
| 57 | + from [{project.database}].sys.columns c |
| 58 | + inner join [{project.database}].sys.types t on c.user_type_id = t.user_type_id |
| 59 | + where c.object_id = object_id('[{project.database}].[{schema}].[{table}]') |
| 60 | + """, |
| 61 | + fetch="all", |
| 62 | + ) |
| 63 | + result = {} |
| 64 | + for name, dtype, max_length in rows: |
| 65 | + if dtype in ("nchar", "nvarchar", "sysname") and max_length != -1: |
| 66 | + char_length = max_length // 2 |
| 67 | + else: |
| 68 | + char_length = max_length |
| 69 | + result[name] = (dtype, char_length) |
| 70 | + return result |
| 71 | + |
| 72 | + |
| 73 | +# --------------------------------------------------------------------------- |
| 74 | +# Default behaviour — flag absent, mappings unchanged from pre-#626 |
| 75 | +# --------------------------------------------------------------------------- |
| 76 | + |
| 77 | + |
| 78 | +class TestDefaultStringTypes: |
| 79 | + @pytest.fixture(scope="class") |
| 80 | + def models(self): |
| 81 | + return { |
| 82 | + "types_model.sql": contract_model_sql, |
| 83 | + "schema.yml": contract_model_yml, |
| 84 | + } |
| 85 | + |
| 86 | + def test_type_labels_dict_default(self, project): |
| 87 | + labels = project.adapter.Column.TYPE_LABELS |
| 88 | + assert labels["STRING"] == "VARCHAR(8000)" |
| 89 | + assert labels["NCHAR"] == "CHAR(1)" |
| 90 | + assert labels["NVARCHAR"] == "VARCHAR(8000)" |
| 91 | + |
| 92 | + def test_column_types_in_database_default(self, project): |
| 93 | + results = run_dbt(["run"]) |
| 94 | + assert len(results) == 1 |
| 95 | + assert results[0].status == "success" |
| 96 | + |
| 97 | + types = _column_types(project, project.test_schema, "types_model") |
| 98 | + # STRING -> VARCHAR(8000) |
| 99 | + assert types["str_col"] == ("varchar", 8000) |
| 100 | + # NCHAR -> CHAR(1) (non-unicode under legacy) |
| 101 | + assert types["nchar_col"] == ("char", 1) |
| 102 | + # NVARCHAR -> VARCHAR(8000) (non-unicode under legacy) |
| 103 | + assert types["nvarchar_col"] == ("varchar", 8000) |
| 104 | + |
| 105 | + |
| 106 | +# --------------------------------------------------------------------------- |
| 107 | +# Native behaviour — flag enabled |
| 108 | +# --------------------------------------------------------------------------- |
| 109 | + |
| 110 | + |
| 111 | +class TestNativeStringTypes: |
| 112 | + @pytest.fixture(scope="class") |
| 113 | + def project_config_update(self): |
| 114 | + return { |
| 115 | + "flags": { |
| 116 | + "dbt_sqlserver_use_native_string_types": True, |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @pytest.fixture(scope="class") |
| 121 | + def models(self): |
| 122 | + return { |
| 123 | + "types_model.sql": contract_model_sql, |
| 124 | + "schema.yml": contract_model_yml, |
| 125 | + } |
| 126 | + |
| 127 | + def test_type_labels_dict_native(self, project): |
| 128 | + labels = project.adapter.Column.TYPE_LABELS |
| 129 | + assert labels["STRING"] == "VARCHAR(MAX)" |
| 130 | + assert labels["NCHAR"] == "NCHAR(1)" |
| 131 | + assert labels["NVARCHAR"] == "NVARCHAR(4000)" |
| 132 | + |
| 133 | + def test_column_types_in_database_native(self, project): |
| 134 | + results = run_dbt(["run"]) |
| 135 | + assert len(results) == 1 |
| 136 | + assert results[0].status == "success" |
| 137 | + |
| 138 | + types = _column_types(project, project.test_schema, "types_model") |
| 139 | + # STRING -> VARCHAR(MAX), reported as character_maximum_length = -1 |
| 140 | + assert types["str_col"] == ("varchar", -1) |
| 141 | + # NCHAR -> NCHAR(1) (unicode) |
| 142 | + assert types["nchar_col"] == ("nchar", 1) |
| 143 | + # NVARCHAR -> NVARCHAR(4000) (unicode, max fixed-length) |
| 144 | + assert types["nvarchar_col"] == ("nvarchar", 4000) |
0 commit comments