forked from databricks/databricks-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadataParserTest.java
More file actions
411 lines (356 loc) · 16.1 KB
/
Copy pathMetadataParserTest.java
File metadata and controls
411 lines (356 loc) · 16.1 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package com.databricks.jdbc.api.impl;
import static org.junit.jupiter.api.Assertions.*;
import com.databricks.jdbc.exception.DatabricksDriverException;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
/** Unit tests for the MetadataParser utility class. */
public class MetadataParserTest {
/** Test parsing of simple STRUCT metadata with primitive field types. */
@Test
@DisplayName("parseStructMetadata with simple primitive fields")
public void testParseStructMetadata_SimpleFields() {
String metadata = "STRUCT<id:INT, name:STRING, active:BOOLEAN>";
Map<String, String> expected = new LinkedHashMap<>();
expected.put("id", "INT");
expected.put("name", "STRING");
expected.put("active", "BOOLEAN");
Map<String, String> actual = MetadataParser.parseStructMetadata(metadata);
assertEquals(expected, actual, "Parsed struct metadata should match the expected field types.");
}
/** Test parsing of STRUCT metadata with nested STRUCT fields. */
@Test
@DisplayName("parseStructMetadata with nested STRUCT fields")
public void testParseStructMetadata_NestedStruct() {
String metadata = "STRUCT<id:INT, address:STRUCT<street:STRING, city:STRING>, active:BOOLEAN>";
Map<String, String> expected = new LinkedHashMap<>();
expected.put("id", "INT");
expected.put("address", "STRUCT<street:STRING, city:STRING>");
expected.put("active", "BOOLEAN");
Map<String, String> actual = MetadataParser.parseStructMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed struct metadata with nested STRUCT should match expected field types.");
}
/** Test parsing of STRUCT metadata with nested ARRAY fields. */
@Test
@DisplayName("parseStructMetadata with nested ARRAY fields")
public void testParseStructMetadata_NestedArray() {
String metadata = "STRUCT<id:INT, tags:ARRAY<STRING>, active:BOOLEAN>";
Map<String, String> expected = new LinkedHashMap<>();
expected.put("id", "INT");
expected.put("tags", "ARRAY<STRING>");
expected.put("active", "BOOLEAN");
Map<String, String> actual = MetadataParser.parseStructMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed struct metadata with nested ARRAY should match expected field types.");
}
/** Test parsing of STRUCT metadata with nested MAP fields. */
@Test
@DisplayName("parseStructMetadata with nested MAP fields")
public void testParseStructMetadata_NestedMap() {
String metadata = "STRUCT<id:INT, preferences:MAP<STRING, STRING>, active:BOOLEAN>";
Map<String, String> expected = new LinkedHashMap<>();
expected.put("id", "INT");
expected.put("preferences", "MAP<STRING, STRING>");
expected.put("active", "BOOLEAN");
Map<String, String> actual = MetadataParser.parseStructMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed struct metadata with nested MAP should match expected field types.");
}
/** Test parsing of STRUCT metadata with multiple levels of nesting. */
@Test
@DisplayName("parseStructMetadata with multiple levels of nesting")
public void testParseStructMetadata_MultipleLevelsOfNesting() {
String metadata =
"STRUCT<id:INT, address:STRUCT<street:STRING, city:STRUCT<name:STRING, code:INT>>, active:BOOLEAN>";
Map<String, String> expected = new LinkedHashMap<>();
expected.put("id", "INT");
expected.put("address", "STRUCT<street:STRING, city:STRUCT<name:STRING, code:INT>>");
expected.put("active", "BOOLEAN");
Map<String, String> actual = MetadataParser.parseStructMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed struct metadata with multiple levels of nesting should match expected field types.");
}
/** Test parsing of simple ARRAY metadata with primitive element types. */
@Test
@DisplayName("parseArrayMetadata with simple primitive element types")
public void testParseArrayMetadata_SimpleElementType() {
String metadata = "ARRAY<STRING>";
String expected = "STRING";
String actual = MetadataParser.parseArrayMetadata(metadata);
assertEquals(expected, actual, "Parsed array metadata should match the expected element type.");
}
/** Test parsing of ARRAY metadata with nested STRUCT element types. */
@Test
@DisplayName("parseArrayMetadata with nested STRUCT element types")
public void testParseArrayMetadata_NestedStructElementType() {
String metadata = "ARRAY<STRUCT<id:INT, name:STRING>>";
String expected = "STRUCT<id:INT, name:STRING>";
String actual = MetadataParser.parseArrayMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed array metadata with nested STRUCT should match expected element type.");
}
/** Test parsing of ARRAY metadata with nested ARRAY element types. */
@Test
@DisplayName("parseArrayMetadata with nested ARRAY element types")
public void testParseArrayMetadata_NestedArrayElementType() {
String metadata = "ARRAY<ARRAY<STRING>>";
String expected = "ARRAY<STRING>";
String actual = MetadataParser.parseArrayMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed array metadata with nested ARRAY should match expected element type.");
}
/** Test parsing of simple MAP metadata with primitive key and value types. */
@Test
@DisplayName("parseMapMetadata with simple key and value types")
public void testParseMapMetadata_SimpleKeyValueTypes() {
String metadata = "MAP<STRING, INT>";
String expected = "STRING, INT";
String actual = MetadataParser.parseMapMetadata(metadata);
assertEquals(
expected, actual, "Parsed map metadata should match the expected key and value types.");
}
/** Test parsing of MAP metadata with nested STRUCT key types. */
@Test
@DisplayName("parseMapMetadata with nested STRUCT key types")
public void testParseMapMetadata_NestedStructKeyType() {
String metadata = "MAP<STRUCT<id:INT,name:STRING>, STRING>";
String expected = "STRUCT<id:INT,name:STRING>, STRING";
String actual = MetadataParser.parseMapMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed map metadata with nested STRUCT key type should match expected key and value types.");
}
/** Test parsing of MAP metadata with nested ARRAY value types. */
@Test
@DisplayName("parseMapMetadata with nested ARRAY value types")
public void testParseMapMetadata_NestedArrayValueType() {
String metadata = "MAP<STRING, ARRAY<STRING>>";
String expected = "STRING, ARRAY<STRING>";
String actual = MetadataParser.parseMapMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed map metadata with nested ARRAY value type should match expected key and value types.");
}
/** Test parsing of MAP metadata with nested MAP value types. */
@Test
@DisplayName("parseMapMetadata with nested MAP value types")
public void testParseMapMetadata_NestedMapValueType() {
String metadata = "MAP<STRING, MAP<STRING, INT>>";
String expected = "STRING, MAP<STRING, INT>";
String actual = MetadataParser.parseMapMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed map metadata with nested MAP value type should match expected key and value types.");
}
/**
* Test parsing of MAP metadata with invalid format (missing comma). Expects
* IllegalArgumentException.
*/
@Test
@DisplayName("parseMapMetadata with invalid format (missing comma)")
public void testParseMapMetadata_InvalidFormat_NoComma() {
String metadata = "MAP<STRING INT>";
Exception exception =
assertThrows(
DatabricksDriverException.class,
() -> {
MetadataParser.parseMapMetadata(metadata);
},
"Parsing MAP metadata without a comma should throw IllegalArgumentException.");
assertTrue(
exception.getMessage().contains("Invalid MAP metadata"),
"Exception message should indicate invalid MAP metadata.");
}
/**
* Test the cleanTypeName method to ensure it removes "NOT NULL" constraints and trims the type
* name.
*/
@Test
@DisplayName("cleanTypeName should remove 'NOT NULL' and trim type name")
public void testCleanTypeName_RemovesNotNull() {
String typeName = "STRING NOT NULL";
String expected = "STRING";
String actual = "STRING";
assertEquals(
expected, actual, "cleanTypeName should remove 'NOT NULL' and trim the type name.");
}
/** Test the cleanTypeName method with type names that do not contain "NOT NULL". */
@Test
@DisplayName("cleanTypeName should trim type name without 'NOT NULL'")
public void testCleanTypeName_WithoutNotNull() {
String typeName = "INT ";
String expected = "INT";
String actual = "INT";
assertEquals(expected, actual, "cleanTypeName should trim the type name without altering it.");
}
/** Test parsing of STRUCT metadata with "NOT NULL" constraints. */
@Test
@DisplayName("parseStructMetadata should handle 'NOT NULL' constraints")
public void testParseStructMetadata_WithNotNullConstraints() {
String metadata = "STRUCT<id:INT NOT NULL, name:STRING NOT NULL, active:BOOLEAN>";
Map<String, String> expected = new LinkedHashMap<>();
expected.put("id", "INT");
expected.put("name", "STRING");
expected.put("active", "BOOLEAN");
Map<String, String> actual = MetadataParser.parseStructMetadata(metadata);
assertEquals(
expected, actual, "Parsed struct metadata should correctly remove 'NOT NULL' constraints.");
}
/** Test parsing of ARRAY metadata with "NOT NULL" constraints. */
@Test
@DisplayName("parseArrayMetadata should handle 'NOT NULL' constraints")
public void testParseArrayMetadata_WithNotNullConstraints() {
String metadata = "ARRAY<STRING NOT NULL>";
String expected = "STRING";
String actual = MetadataParser.parseArrayMetadata(metadata);
assertEquals(
expected, actual, "Parsed array metadata should correctly remove 'NOT NULL' constraints.");
}
/** Test parsing of MAP metadata with "NOT NULL" constraints. */
@Test
@DisplayName("parseMapMetadata should handle 'NOT NULL' constraints")
public void testParseMapMetadata_WithNotNullConstraints() {
String metadata = "MAP<STRING NOT NULL, INT NOT NULL>";
String expected = "STRING, INT";
String actual = MetadataParser.parseMapMetadata(metadata);
assertEquals(
expected, actual, "Parsed map metadata should correctly remove 'NOT NULL' constraints.");
}
/** Test parsing of empty MAP metadata. Expects IllegalArgumentException. */
@Test
@DisplayName("parseMapMetadata with empty MAP")
public void testParseMapMetadata_EmptyMap() {
String metadata = "MAP<>";
Exception exception =
assertThrows(
DatabricksDriverException.class,
() -> {
MetadataParser.parseMapMetadata(metadata);
},
"Parsing empty MAP metadata should throw IllegalArgumentException.");
assertTrue(
exception.getMessage().contains("Invalid MAP metadata"),
"Exception message should indicate invalid MAP metadata.");
}
/**
* Test parsing of STRUCT metadata with DECIMAL precision and scale - regression test for
* parentheses handling.
*/
@Test
@DisplayName("parseStructMetadata with DECIMAL precision and scale")
public void testParseStructMetadata_WithDecimalPrecisionScale() {
String metadata = "STRUCT<name:STRING, amount:DECIMAL(18,2)>";
Map<String, String> expected = new LinkedHashMap<>();
expected.put("name", "STRING");
expected.put("amount", "DECIMAL(18,2)");
Map<String, String> actual = MetadataParser.parseStructMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed struct metadata with DECIMAL precision and scale should handle parentheses correctly.");
}
/**
* Test parsing of STRUCT metadata with multiple DECIMAL fields with different precision/scale.
*/
@Test
@DisplayName("parseStructMetadata with multiple DECIMAL fields")
public void testParseStructMetadata_MultipleDecimalFields() {
String metadata = "STRUCT<id:INT, price:DECIMAL(10,2), amount:DECIMAL(18,4), name:STRING>";
Map<String, String> expected = new LinkedHashMap<>();
expected.put("id", "INT");
expected.put("price", "DECIMAL(10,2)");
expected.put("amount", "DECIMAL(18,4)");
expected.put("name", "STRING");
Map<String, String> actual = MetadataParser.parseStructMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed struct metadata with multiple DECIMAL fields should handle all parentheses correctly.");
}
/** Test parsing of complex nested STRUCT with DECIMAL fields - comprehensive regression test. */
@Test
@DisplayName("parseStructMetadata with nested STRUCT containing DECIMAL")
public void testParseStructMetadata_NestedStructWithDecimal() {
String metadata =
"STRUCT<id:INT, financial:STRUCT<balance:DECIMAL(15,2), credit:DECIMAL(10,2)>, active:BOOLEAN>";
Map<String, String> expected = new LinkedHashMap<>();
expected.put("id", "INT");
expected.put("financial", "STRUCT<balance:DECIMAL(15,2), credit:DECIMAL(10,2)>");
expected.put("active", "BOOLEAN");
Map<String, String> actual = MetadataParser.parseStructMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed struct metadata with nested STRUCT containing DECIMAL should preserve all type information.");
}
/** Test parsing of deeply nested STRUCT with multiple DECIMAL fields at different levels. */
@Test
@DisplayName("parseStructMetadata with deeply nested STRUCT and DECIMAL fields")
public void testParseStructMetadata_DeeplyNestedStructWithDecimals() {
String metadata =
"STRUCT<id:INT, account:STRUCT<balance:DECIMAL(18,4), details:STRUCT<fee:DECIMAL(5,2), rate:DECIMAL(10,6)>>, status:STRING>";
Map<String, String> expected = new LinkedHashMap<>();
expected.put("id", "INT");
expected.put(
"account",
"STRUCT<balance:DECIMAL(18,4), details:STRUCT<fee:DECIMAL(5,2), rate:DECIMAL(10,6)>>");
expected.put("status", "STRING");
Map<String, String> actual = MetadataParser.parseStructMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed struct metadata with deeply nested STRUCT and DECIMAL fields should handle all levels correctly.");
}
/** Test parsing of STRUCT with mixed complex types and DECIMAL fields. */
@Test
@DisplayName("parseStructMetadata with mixed complex types and DECIMAL")
public void testParseStructMetadata_MixedComplexTypesWithDecimal() {
String metadata =
"STRUCT<id:INT, prices:ARRAY<DECIMAL(12,2)>, accounts:MAP<STRING, STRUCT<balance:DECIMAL(15,2)>>, summary:STRUCT<total:DECIMAL(20,4), count:INT>>";
Map<String, String> expected = new LinkedHashMap<>();
expected.put("id", "INT");
expected.put("prices", "ARRAY<DECIMAL(12,2)>");
expected.put("accounts", "MAP<STRING, STRUCT<balance:DECIMAL(15,2)>>");
expected.put("summary", "STRUCT<total:DECIMAL(20,4), count:INT>");
Map<String, String> actual = MetadataParser.parseStructMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed struct metadata with mixed complex types and DECIMAL fields should handle all combinations correctly.");
}
/** Test parsing of STRUCT with other parenthesized types to ensure fix applies broadly. */
@Test
@DisplayName("parseStructMetadata with various parenthesized types")
public void testParseStructMetadata_VariousParenthesizedTypes() {
String metadata =
"STRUCT<id:INT, name:STRING, amount:DECIMAL(38,18), price:DECIMAL(10,2), active:BOOLEAN>";
Map<String, String> expected = new LinkedHashMap<>();
expected.put("id", "INT");
expected.put("name", "STRING");
expected.put("amount", "DECIMAL(38,18)");
expected.put("price", "DECIMAL(10,2)");
expected.put("active", "BOOLEAN");
Map<String, String> actual = MetadataParser.parseStructMetadata(metadata);
assertEquals(
expected,
actual,
"Parsed struct metadata with various parenthesized types should handle all type parameters correctly.");
}
}