|
| 1 | +""" |
| 2 | +Tests for the column mapping module. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +from unittest.mock import MagicMock |
| 7 | +from enum import Enum |
| 8 | + |
| 9 | +from databricks.sql.backend.column_mapping import ( |
| 10 | + normalise_metadata_result, |
| 11 | + MetadataOp, |
| 12 | + CATALOG_OP, |
| 13 | + SCHEMA_OP, |
| 14 | + TABLE_OP, |
| 15 | + COLUMN_OP, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class TestColumnMapping: |
| 20 | + """Tests for the column mapping module.""" |
| 21 | + |
| 22 | + def test_normalize_metadata_result_catalogs(self): |
| 23 | + """Test normalizing catalog column names.""" |
| 24 | + # Create a mock result set with a description |
| 25 | + mock_result = MagicMock() |
| 26 | + mock_result.description = [ |
| 27 | + ("catalog", "string", None, None, None, None, True), |
| 28 | + ("other_column", "string", None, None, None, None, True), |
| 29 | + ] |
| 30 | + |
| 31 | + # Normalize the result set |
| 32 | + normalise_metadata_result(mock_result, MetadataOp.CATALOGS) |
| 33 | + |
| 34 | + # Check that the column names were normalized |
| 35 | + assert mock_result.description[0][0] == "TABLE_CAT" |
| 36 | + assert mock_result.description[1][0] == "other_column" |
| 37 | + |
| 38 | + def test_normalize_metadata_result_schemas(self): |
| 39 | + """Test normalizing schema column names.""" |
| 40 | + # Create a mock result set with a description |
| 41 | + mock_result = MagicMock() |
| 42 | + mock_result.description = [ |
| 43 | + ("databaseName", "string", None, None, None, None, True), |
| 44 | + ("catalogName", "string", None, None, None, None, True), |
| 45 | + ("other_column", "string", None, None, None, None, True), |
| 46 | + ] |
| 47 | + |
| 48 | + # Normalize the result set |
| 49 | + normalise_metadata_result(mock_result, MetadataOp.SCHEMAS) |
| 50 | + |
| 51 | + # Check that the column names were normalized |
| 52 | + assert mock_result.description[0][0] == "TABLE_SCHEM" |
| 53 | + assert mock_result.description[1][0] == "TABLE_CATALOG" |
| 54 | + assert mock_result.description[2][0] == "other_column" |
| 55 | + |
| 56 | + def test_normalize_metadata_result_tables(self): |
| 57 | + """Test normalizing table column names.""" |
| 58 | + # Create a mock result set with a description |
| 59 | + mock_result = MagicMock() |
| 60 | + mock_result.description = [ |
| 61 | + ("catalogName", "string", None, None, None, None, True), |
| 62 | + ("namespace", "string", None, None, None, None, True), |
| 63 | + ("tableName", "string", None, None, None, None, True), |
| 64 | + ("tableType", "string", None, None, None, None, True), |
| 65 | + ("remarks", "string", None, None, None, None, True), |
| 66 | + ("TYPE_CATALOG_COLUMN", "string", None, None, None, None, True), |
| 67 | + ("TYPE_SCHEMA_COLUMN", "string", None, None, None, None, True), |
| 68 | + ("TYPE_NAME", "string", None, None, None, None, True), |
| 69 | + ("SELF_REFERENCING_COLUMN_NAME", "string", None, None, None, None, True), |
| 70 | + ("REF_GENERATION_COLUMN", "string", None, None, None, None, True), |
| 71 | + ("other_column", "string", None, None, None, None, True), |
| 72 | + ] |
| 73 | + |
| 74 | + # Normalize the result set |
| 75 | + normalise_metadata_result(mock_result, MetadataOp.TABLES) |
| 76 | + |
| 77 | + # Check that the column names were normalized |
| 78 | + assert mock_result.description[0][0] == "TABLE_CAT" |
| 79 | + assert mock_result.description[1][0] == "TABLE_SCHEM" |
| 80 | + assert mock_result.description[2][0] == "TABLE_NAME" |
| 81 | + assert mock_result.description[3][0] == "TABLE_TYPE" |
| 82 | + assert mock_result.description[4][0] == "REMARKS" |
| 83 | + assert mock_result.description[5][0] == "TYPE_CAT" |
| 84 | + assert mock_result.description[6][0] == "TYPE_SCHEM" |
| 85 | + assert mock_result.description[7][0] == "TYPE_NAME" |
| 86 | + assert mock_result.description[8][0] == "SELF_REFERENCING_COL_NAME" |
| 87 | + assert mock_result.description[9][0] == "REF_GENERATION" |
| 88 | + assert mock_result.description[10][0] == "other_column" |
| 89 | + |
| 90 | + def test_normalize_metadata_result_columns(self): |
| 91 | + """Test normalizing column column names.""" |
| 92 | + # Create a mock result set with a description |
| 93 | + mock_result = MagicMock() |
| 94 | + mock_result.description = [ |
| 95 | + ("catalogName", "string", None, None, None, None, True), |
| 96 | + ("namespace", "string", None, None, None, None, True), |
| 97 | + ("tableName", "string", None, None, None, None, True), |
| 98 | + ("columnName", "string", None, None, None, None, True), |
| 99 | + ("dataType", "string", None, None, None, None, True), |
| 100 | + ("columnType", "string", None, None, None, None, True), |
| 101 | + ("columnSize", "string", None, None, None, None, True), |
| 102 | + ("bufferLength", "string", None, None, None, None, True), |
| 103 | + ("decimalDigits", "string", None, None, None, None, True), |
| 104 | + ("radix", "string", None, None, None, None, True), |
| 105 | + ("nullable", "string", None, None, None, None, True), |
| 106 | + ("remarks", "string", None, None, None, None, True), |
| 107 | + ("columnDef", "string", None, None, None, None, True), |
| 108 | + ("sqlDataType", "string", None, None, None, None, True), |
| 109 | + ("sqlDatetimeSub", "string", None, None, None, None, True), |
| 110 | + ("charOctetLength", "string", None, None, None, None, True), |
| 111 | + ("ordinalPosition", "string", None, None, None, None, True), |
| 112 | + ("isNullable", "string", None, None, None, None, True), |
| 113 | + ("scopeCatalog", "string", None, None, None, None, True), |
| 114 | + ("scopeSchema", "string", None, None, None, None, True), |
| 115 | + ("scopeTable", "string", None, None, None, None, True), |
| 116 | + ("sourceDataType", "string", None, None, None, None, True), |
| 117 | + ("isAutoIncrement", "string", None, None, None, None, True), |
| 118 | + ("isGenerated", "string", None, None, None, None, True), |
| 119 | + ("other_column", "string", None, None, None, None, True), |
| 120 | + ] |
| 121 | + |
| 122 | + # Normalize the result set |
| 123 | + normalise_metadata_result(mock_result, MetadataOp.COLUMNS) |
| 124 | + |
| 125 | + # Check that the column names were normalized |
| 126 | + assert mock_result.description[0][0] == "TABLE_CAT" |
| 127 | + assert mock_result.description[1][0] == "TABLE_SCHEM" |
| 128 | + assert mock_result.description[2][0] == "TABLE_NAME" |
| 129 | + assert mock_result.description[3][0] == "COLUMN_NAME" |
| 130 | + assert mock_result.description[4][0] == "DATA_TYPE" |
| 131 | + assert mock_result.description[5][0] == "TYPE_NAME" |
| 132 | + assert mock_result.description[6][0] == "COLUMN_SIZE" |
| 133 | + assert mock_result.description[7][0] == "BUFFER_LENGTH" |
| 134 | + assert mock_result.description[8][0] == "DECIMAL_DIGITS" |
| 135 | + assert mock_result.description[9][0] == "NUM_PREC_RADIX" |
| 136 | + assert mock_result.description[10][0] == "NULLABLE" |
| 137 | + assert mock_result.description[11][0] == "REMARKS" |
| 138 | + assert mock_result.description[12][0] == "COLUMN_DEF" |
| 139 | + assert mock_result.description[13][0] == "SQL_DATA_TYPE" |
| 140 | + assert mock_result.description[14][0] == "SQL_DATETIME_SUB" |
| 141 | + assert mock_result.description[15][0] == "CHAR_OCTET_LENGTH" |
| 142 | + assert mock_result.description[16][0] == "ORDINAL_POSITION" |
| 143 | + assert mock_result.description[17][0] == "IS_NULLABLE" |
| 144 | + assert mock_result.description[18][0] == "SCOPE_CATALOG" |
| 145 | + assert mock_result.description[19][0] == "SCOPE_SCHEMA" |
| 146 | + assert mock_result.description[20][0] == "SCOPE_TABLE" |
| 147 | + assert mock_result.description[21][0] == "SOURCE_DATA_TYPE" |
| 148 | + assert mock_result.description[22][0] == "IS_AUTOINCREMENT" |
| 149 | + assert mock_result.description[23][0] == "IS_GENERATEDCOLUMN" |
| 150 | + assert mock_result.description[24][0] == "other_column" |
| 151 | + |
| 152 | + def test_normalize_metadata_result_unknown_operation(self): |
| 153 | + """Test normalizing with an unknown operation type.""" |
| 154 | + # Create a mock result set with a description |
| 155 | + mock_result = MagicMock() |
| 156 | + mock_result.description = [ |
| 157 | + ("column1", "string", None, None, None, None, True), |
| 158 | + ("column2", "string", None, None, None, None, True), |
| 159 | + ] |
| 160 | + |
| 161 | + # Save the original description |
| 162 | + original_description = mock_result.description.copy() |
| 163 | + |
| 164 | + # Create a separate enum for testing |
| 165 | + class TestOp(Enum): |
| 166 | + UNKNOWN = "unknown" |
| 167 | + |
| 168 | + # Normalize the result set with an unknown operation |
| 169 | + normalise_metadata_result(mock_result, TestOp.UNKNOWN) |
| 170 | + |
| 171 | + # Check that the description was not modified |
| 172 | + assert mock_result.description == original_description |
| 173 | + |
| 174 | + def test_normalize_metadata_result_preserves_other_fields(self): |
| 175 | + """Test that normalization preserves other fields in the description.""" |
| 176 | + # Create a mock result set with a description |
| 177 | + mock_result = MagicMock() |
| 178 | + mock_result.description = [ |
| 179 | + ( |
| 180 | + "catalog", |
| 181 | + "string", |
| 182 | + "display_size", |
| 183 | + "internal_size", |
| 184 | + "precision", |
| 185 | + "scale", |
| 186 | + True, |
| 187 | + ), |
| 188 | + ] |
| 189 | + |
| 190 | + # Normalize the result set |
| 191 | + normalise_metadata_result(mock_result, MetadataOp.CATALOGS) |
| 192 | + |
| 193 | + # Check that the column name was normalized but other fields preserved |
| 194 | + assert mock_result.description[0][0] == "TABLE_CAT" |
| 195 | + assert mock_result.description[0][1] == "string" |
| 196 | + assert mock_result.description[0][2] == "display_size" |
| 197 | + assert mock_result.description[0][3] == "internal_size" |
| 198 | + assert mock_result.description[0][4] == "precision" |
| 199 | + assert mock_result.description[0][5] == "scale" |
| 200 | + assert mock_result.description[0][6] == True |
0 commit comments