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