|
| 1 | +""" |
| 2 | +Unit tests for metadata column normalization constants and functions. |
| 3 | +""" |
| 4 | + |
| 5 | +import unittest |
| 6 | +from databricks.sql.backend.sea.metadata_constants import ( |
| 7 | + CATALOG_COLUMNS, |
| 8 | + SCHEMA_COLUMNS, |
| 9 | + TABLE_COLUMNS, |
| 10 | + COLUMN_COLUMNS, |
| 11 | + get_column_names, |
| 12 | + get_column_mapping, |
| 13 | + normalize_metadata_description, |
| 14 | + normalize_columns_metadata_description, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class TestMetadataConstants(unittest.TestCase): |
| 19 | + """Test metadata column constants and helper functions.""" |
| 20 | + |
| 21 | + def test_catalog_columns_structure(self): |
| 22 | + """Test CATALOG_COLUMNS has correct structure.""" |
| 23 | + self.assertEqual(len(CATALOG_COLUMNS), 1) |
| 24 | + self.assertEqual(CATALOG_COLUMNS[0], ("TABLE_CAT", "catalog")) |
| 25 | + |
| 26 | + def test_schema_columns_structure(self): |
| 27 | + """Test SCHEMA_COLUMNS has correct structure.""" |
| 28 | + self.assertEqual(len(SCHEMA_COLUMNS), 2) |
| 29 | + self.assertEqual(SCHEMA_COLUMNS[0], ("TABLE_SCHEM", "databaseName")) |
| 30 | + self.assertEqual(SCHEMA_COLUMNS[1], ("TABLE_CATALOG", "catalogName")) |
| 31 | + |
| 32 | + def test_table_columns_structure(self): |
| 33 | + """Test TABLE_COLUMNS has correct structure and count.""" |
| 34 | + self.assertEqual(len(TABLE_COLUMNS), 10) |
| 35 | + # Check key columns |
| 36 | + self.assertEqual(TABLE_COLUMNS[0], ("TABLE_CAT", "catalogName")) |
| 37 | + self.assertEqual(TABLE_COLUMNS[1], ("TABLE_SCHEM", "namespace")) |
| 38 | + self.assertEqual(TABLE_COLUMNS[2], ("TABLE_NAME", "tableName")) |
| 39 | + self.assertEqual(TABLE_COLUMNS[3], ("TABLE_TYPE", "tableType")) |
| 40 | + self.assertEqual(TABLE_COLUMNS[4], ("REMARKS", "remarks")) |
| 41 | + |
| 42 | + def test_column_columns_structure(self): |
| 43 | + """Test COLUMN_COLUMNS has correct structure and count.""" |
| 44 | + self.assertEqual(len(COLUMN_COLUMNS), 24) |
| 45 | + # Check key columns |
| 46 | + self.assertEqual(COLUMN_COLUMNS[0], ("TABLE_CAT", "catalogName")) |
| 47 | + self.assertEqual(COLUMN_COLUMNS[1], ("TABLE_SCHEM", "namespace")) |
| 48 | + self.assertEqual(COLUMN_COLUMNS[2], ("TABLE_NAME", "tableName")) |
| 49 | + self.assertEqual(COLUMN_COLUMNS[3], ("COLUMN_NAME", "col_name")) |
| 50 | + self.assertEqual(COLUMN_COLUMNS[4], ("DATA_TYPE", "dataType")) |
| 51 | + self.assertEqual(COLUMN_COLUMNS[5], ("TYPE_NAME", "columnType")) |
| 52 | + # Check that COLUMN_DEF also maps to columnType (same source) |
| 53 | + self.assertEqual(COLUMN_COLUMNS[12], ("COLUMN_DEF", "columnType")) |
| 54 | + |
| 55 | + def test_get_column_names(self): |
| 56 | + """Test get_column_names helper function.""" |
| 57 | + test_columns = [("JDBC_NAME1", "sea_name1"), ("JDBC_NAME2", "sea_name2")] |
| 58 | + result = get_column_names(test_columns) |
| 59 | + self.assertEqual(result, ["JDBC_NAME1", "JDBC_NAME2"]) |
| 60 | + |
| 61 | + def test_get_column_mapping(self): |
| 62 | + """Test get_column_mapping helper function.""" |
| 63 | + test_columns = [ |
| 64 | + ("JDBC_NAME1", "sea_name1"), |
| 65 | + ("JDBC_NAME2", "sea_name2"), |
| 66 | + ("JDBC_NAME3", None), # Should be excluded |
| 67 | + ] |
| 68 | + result = get_column_mapping(test_columns) |
| 69 | + expected = {"sea_name1": "JDBC_NAME1", "sea_name2": "JDBC_NAME2"} |
| 70 | + self.assertEqual(result, expected) |
| 71 | + |
| 72 | + def test_normalize_metadata_description_basic(self): |
| 73 | + """Test basic metadata description normalization.""" |
| 74 | + # Mock original description |
| 75 | + original_desc = [ |
| 76 | + ("catalog", "string", None, None, None, None, True), |
| 77 | + ] |
| 78 | + |
| 79 | + result = normalize_metadata_description(original_desc, CATALOG_COLUMNS) |
| 80 | + |
| 81 | + expected = [ |
| 82 | + ("TABLE_CAT", "string", None, None, None, None, True), |
| 83 | + ] |
| 84 | + self.assertEqual(result, expected) |
| 85 | + |
| 86 | + def test_normalize_metadata_description_with_missing_columns(self): |
| 87 | + """Test normalization when some columns are missing from source.""" |
| 88 | + # Original description has only one column |
| 89 | + original_desc = [ |
| 90 | + ("databaseName", "string", None, None, None, None, True), |
| 91 | + ] |
| 92 | + |
| 93 | + result = normalize_metadata_description(original_desc, SCHEMA_COLUMNS) |
| 94 | + |
| 95 | + expected = [ |
| 96 | + ("TABLE_SCHEM", "string", None, None, None, None, True), |
| 97 | + ( |
| 98 | + "TABLE_CATALOG", |
| 99 | + "string", |
| 100 | + None, |
| 101 | + None, |
| 102 | + None, |
| 103 | + None, |
| 104 | + None, |
| 105 | + ), # Missing column gets defaults |
| 106 | + ] |
| 107 | + self.assertEqual(result, expected) |
| 108 | + |
| 109 | + def test_normalize_metadata_description_empty_input(self): |
| 110 | + """Test normalization with empty input.""" |
| 111 | + result = normalize_metadata_description([], CATALOG_COLUMNS) |
| 112 | + self.assertEqual(result, []) |
| 113 | + |
| 114 | + def test_normalize_columns_metadata_description(self): |
| 115 | + """Test columns-specific normalization function.""" |
| 116 | + # Mock original description with key columns |
| 117 | + original_desc = [ |
| 118 | + ("catalogName", "string", None, None, None, None, True), |
| 119 | + ("namespace", "string", None, None, None, None, True), |
| 120 | + ("tableName", "string", None, None, None, None, True), |
| 121 | + ("col_name", "string", None, None, None, None, True), |
| 122 | + ("dataType", "int", None, None, None, None, True), |
| 123 | + ("columnType", "string", None, None, None, None, True), |
| 124 | + ] |
| 125 | + |
| 126 | + result = normalize_columns_metadata_description(original_desc) |
| 127 | + |
| 128 | + # Should have 24 columns total |
| 129 | + self.assertEqual(len(result), 24) |
| 130 | + |
| 131 | + # Check that key columns are mapped correctly |
| 132 | + self.assertEqual(result[0][0], "TABLE_CAT") # catalogName -> TABLE_CAT |
| 133 | + self.assertEqual(result[1][0], "TABLE_SCHEM") # namespace -> TABLE_SCHEM |
| 134 | + self.assertEqual(result[5][0], "TYPE_NAME") # columnType -> TYPE_NAME |
| 135 | + self.assertEqual( |
| 136 | + result[12][0], "COLUMN_DEF" |
| 137 | + ) # columnType -> COLUMN_DEF (same source) |
| 138 | + |
| 139 | + # Both TYPE_NAME and COLUMN_DEF should have same metadata (except name) |
| 140 | + self.assertEqual(result[5][1:], result[12][1:]) |
| 141 | + |
| 142 | + def test_normalize_metadata_description_preserves_metadata(self): |
| 143 | + """Test that normalization preserves non-name metadata.""" |
| 144 | + original_desc = [ |
| 145 | + ("catalog", "varchar", 100, 50, 10, 2, False), |
| 146 | + ] |
| 147 | + |
| 148 | + result = normalize_metadata_description(original_desc, CATALOG_COLUMNS) |
| 149 | + |
| 150 | + expected = [ |
| 151 | + ("TABLE_CAT", "varchar", 100, 50, 10, 2, False), |
| 152 | + ] |
| 153 | + self.assertEqual(result, expected) |
| 154 | + |
| 155 | + def test_columns_with_duplicate_source_mapping(self): |
| 156 | + """Test that TYPE_NAME and COLUMN_DEF both map to columnType correctly.""" |
| 157 | + original_desc = [ |
| 158 | + ("columnType", "string", None, None, None, None, True), |
| 159 | + ] |
| 160 | + |
| 161 | + # Create a subset of column definitions that includes both TYPE_NAME and COLUMN_DEF |
| 162 | + test_columns = [ |
| 163 | + ("TYPE_NAME", "columnType"), |
| 164 | + ("COLUMN_DEF", "columnType"), |
| 165 | + ] |
| 166 | + |
| 167 | + result = normalize_metadata_description(original_desc, test_columns) |
| 168 | + |
| 169 | + expected = [ |
| 170 | + ("TYPE_NAME", "string", None, None, None, None, True), |
| 171 | + ("COLUMN_DEF", "string", None, None, None, None, True), |
| 172 | + ] |
| 173 | + self.assertEqual(result, expected) |
| 174 | + |
| 175 | + # Both should have identical metadata except for the name |
| 176 | + self.assertEqual(result[0][1:], result[1][1:]) |
| 177 | + |
| 178 | + |
| 179 | +if __name__ == "__main__": |
| 180 | + unittest.main() |
0 commit comments