-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBigQueryResultSetMetadataTest.java
More file actions
321 lines (285 loc) · 13 KB
/
BigQueryResultSetMetadataTest.java
File metadata and controls
321 lines (285 loc) · 13 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
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigquery.jdbc;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.FieldList;
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.common.collect.ImmutableList;
import java.sql.Array;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class BigQueryResultSetMetadataTest {
private BigQueryStatement statement;
private static Field tenthField =
Field.newBuilder("tenth", LegacySQLTypeName.NUMERIC)
.setName("tenth")
.setType(StandardSQLTypeName.NUMERIC)
.setPrecision(12L)
.setScale(9L)
.build();
private static final FieldList fieldList =
FieldList.of(
Field.of("first", StandardSQLTypeName.BOOL),
Field.of("second", StandardSQLTypeName.INT64),
Field.of("third", StandardSQLTypeName.FLOAT64),
Field.of("fourth", StandardSQLTypeName.STRING),
Field.of("fifth", StandardSQLTypeName.TIMESTAMP),
Field.of("sixth", StandardSQLTypeName.BYTES),
Field.of("seventh", StandardSQLTypeName.STRING),
Field.newBuilder("eight", StandardSQLTypeName.STRING)
.setMode(Field.Mode.REPEATED)
.build(),
Field.of(
"ninth",
StandardSQLTypeName.STRUCT,
Field.of("first", StandardSQLTypeName.FLOAT64),
Field.of("second", StandardSQLTypeName.TIMESTAMP)),
tenthField,
Field.of("eleventh", StandardSQLTypeName.BIGNUMERIC),
Field.of("twelfth", LegacySQLTypeName.TIME),
Field.of("thirteenth", LegacySQLTypeName.DATE));
private static final List fieldListSqlTypes =
ImmutableList.of(
Types.BOOLEAN,
Types.BIGINT,
Types.DOUBLE,
Types.NVARCHAR,
Types.TIMESTAMP,
Types.VARBINARY,
Types.NVARCHAR,
Types.ARRAY,
Types.STRUCT,
Types.NUMERIC,
Types.NUMERIC,
Types.TIME,
Types.DATE);
private static final List fieldListClassNames =
ImmutableList.of(
"java.lang.Boolean",
"java.lang.Long",
"java.lang.Double",
"java.lang.String",
"java.sql.Timestamp",
byte[].class.getName(),
"java.lang.String",
Array.class.getName(),
"java.sql.Struct",
"java.math.BigDecimal",
"java.math.BigDecimal",
"java.sql.Time",
"java.sql.Date");
private static final Schema QUERY_SCHEMA = Schema.of(fieldList);
private ResultSetMetaData resultSetMetaData;
private ResultSetMetaData resultSetMetaDataNested;
@BeforeEach
public void setUp() throws SQLException {
statement = mock(BigQueryStatement.class);
Thread[] workerThreads = {new Thread()};
BigQueryJsonResultSet bigQueryJsonResultSet =
BigQueryJsonResultSet.of(QUERY_SCHEMA, 1L, null, statement, workerThreads);
// values for nested types
resultSetMetaData = bigQueryJsonResultSet.getMetaData();
// values for nested types
Field fieldEight = fieldList.get("eight");
// The schema for the nested result set should describe the elements of the array.
Field elementField = fieldEight.toBuilder().setMode(Field.Mode.NULLABLE).build();
FieldList nestedFieldList = FieldList.of(elementField);
BigQueryFieldValueListWrapper bigQueryFieldValueListWrapperNested =
BigQueryFieldValueListWrapper.getNestedFieldValueListWrapper(nestedFieldList, null);
BigQueryJsonResultSet bigQueryJsonResultSetNested =
BigQueryJsonResultSet.getNestedResultSet(
Schema.of(nestedFieldList), bigQueryFieldValueListWrapperNested, -1, -1);
resultSetMetaDataNested = bigQueryJsonResultSetNested.getMetaData();
}
@Test
public void testGetColumnType() throws SQLException {
// match the mapping for all the types in the test dataset
for (int colIndex = 1; colIndex <= 13; colIndex++) {
assertThat(resultSetMetaData.getColumnType(colIndex))
.isEqualTo(fieldListSqlTypes.get(colIndex - 1));
}
}
@Test
public void testGetColumnTypeName() throws SQLException {
assertThat(resultSetMetaData.getColumnTypeName(1)).isEqualTo("BOOL");
assertThat(resultSetMetaData.getColumnTypeName(2)).isEqualTo("INT64");
assertThat(resultSetMetaData.getColumnTypeName(3)).isEqualTo("FLOAT64");
assertThat(resultSetMetaData.getColumnTypeName(4)).isEqualTo("STRING");
assertThat(resultSetMetaData.getColumnTypeName(5)).isEqualTo("TIMESTAMP");
assertThat(resultSetMetaData.getColumnTypeName(6)).isEqualTo("BYTES");
assertThat(resultSetMetaData.getColumnTypeName(7)).isEqualTo("STRING");
assertThat(resultSetMetaData.getColumnTypeName(8)).isEqualTo("ARRAY");
assertThat(resultSetMetaData.getColumnTypeName(9)).isEqualTo("STRUCT");
assertThat(resultSetMetaData.getColumnTypeName(10)).isEqualTo("NUMERIC");
assertThat(resultSetMetaData.getColumnTypeName(11)).isEqualTo("BIGNUMERIC");
assertThat(resultSetMetaData.getColumnTypeName(12)).isEqualTo("TIME");
assertThat(resultSetMetaData.getColumnTypeName(13)).isEqualTo("DATE");
}
@Test
public void testColumnClassName()
throws SQLException { // match the mapping for all the types in the test dataset
for (int colIndex = 1; colIndex <= 13; colIndex++) {
assertThat(resultSetMetaData.getColumnClassName(colIndex))
.isEqualTo(fieldListClassNames.get(colIndex - 1));
}
}
@Test
public void testResultSetMetadataProperties() throws SQLException {
assertThat(resultSetMetaData).isNotNull();
assertThat(resultSetMetaData.getColumnCount()).isEqualTo(13);
assertThat(resultSetMetaData.isAutoIncrement(1)).isFalse();
assertThat(resultSetMetaData.isSearchable(4)).isTrue();
assertThat(resultSetMetaData.isCurrency(4)).isFalse();
assertThat(resultSetMetaData.isReadOnly(4)).isFalse();
assertThat(resultSetMetaData.isDefinitelyWritable(4)).isFalse();
assertThat(resultSetMetaData.isWritable(4)).isTrue();
assertThat(resultSetMetaData.isNullable(4)).isEqualTo(ResultSetMetaData.columnNullableUnknown);
}
@Test
public void testPrecision() throws SQLException {
assertThat(resultSetMetaData.getPrecision(10)).isEqualTo(12L);
assertThat(resultSetMetaData.getPrecision(1))
.isEqualTo(0); // schema doesn't have this info, should be defaulted to 0
}
@Test
public void testSigned() throws SQLException {
assertThat(resultSetMetaData.isSigned(4)).isFalse();
assertThat(resultSetMetaData.isSigned(2)).isTrue();
}
@Test
public void testCheckNameLabelCatalog() throws SQLException {
assertThat(resultSetMetaData.getColumnLabel(1)).isEqualTo("first");
assertThat(resultSetMetaData.getColumnName(10)).isEqualTo("tenth");
assertThat(resultSetMetaData.getColumnName(10)).isEqualTo("tenth");
assertThat(resultSetMetaData.getSchemaName(10)).isEqualTo("");
assertThat(resultSetMetaData.getCatalogName(10)).isEqualTo("");
}
@Test
public void testCheckCaseSensitive() throws SQLException {
assertThat(resultSetMetaData.isCaseSensitive(2)).isFalse();
assertThat(resultSetMetaData.isCaseSensitive(4)).isTrue();
}
@Test
public void testScale() throws SQLException {
assertThat(resultSetMetaData.getScale(10)).isEqualTo(9L);
assertThat(resultSetMetaData.getScale(4)).isEqualTo(0L);
}
@Test
public void testColumnDisplaySize() throws SQLException {
assertThat(resultSetMetaData.getColumnDisplaySize(1)).isEqualTo(5);
assertThat(resultSetMetaData.getColumnDisplaySize(13)).isEqualTo(10);
assertThat(resultSetMetaData.getColumnDisplaySize(2)).isEqualTo(10);
assertThat(resultSetMetaData.getColumnDisplaySize(3)).isEqualTo(14);
assertThat(resultSetMetaData.getColumnDisplaySize(12)).isEqualTo(50);
assertThat(resultSetMetaData.getColumnDisplaySize(5)).isEqualTo(16);
}
// Nested Types
@Test
public void testResultSetMetaDataNestedColType() throws SQLException {
assertThat(resultSetMetaDataNested).isNotNull();
assertThat(resultSetMetaDataNested.getColumnType(1)).isEqualTo(Types.NVARCHAR);
assertThat(resultSetMetaDataNested.getColumnClassName(1)).isEqualTo("java.lang.String");
}
@Test
public void testNestedresultSetMetaDataNestedProperties() throws SQLException {
assertThat(resultSetMetaDataNested.getColumnCount()).isEqualTo(1);
assertThat(resultSetMetaDataNested.isAutoIncrement(1)).isFalse();
assertThat(resultSetMetaDataNested.isSearchable(1)).isTrue();
assertThat(resultSetMetaDataNested.isCurrency(1)).isFalse();
assertThat(resultSetMetaDataNested.isReadOnly(1)).isFalse();
assertThat(resultSetMetaDataNested.isDefinitelyWritable(1)).isFalse();
assertThat(resultSetMetaDataNested.isWritable(1)).isTrue();
assertThat(resultSetMetaDataNested.isNullable(1)).isEqualTo(ResultSetMetaData.columnNullable);
}
@Test
public void testNestedPrecision() throws SQLException {
assertThat(resultSetMetaDataNested.getPrecision(1))
.isEqualTo(0); // schema doesn't have this info, should be defaulted to 0
}
@Test
public void testNestedSigned() throws SQLException {
assertThat(resultSetMetaDataNested.isSigned(1)).isFalse();
}
@Test
public void testNestedCheckNameLabelCatalog() throws SQLException {
assertThat(resultSetMetaDataNested.getColumnLabel(1)).isEqualTo("eight");
assertThat(resultSetMetaDataNested.getColumnName(1)).isEqualTo("eight");
assertThat(resultSetMetaDataNested.getSchemaName(1)).isEqualTo("");
assertThat(resultSetMetaDataNested.getCatalogName(1)).isEqualTo("");
}
@Test
public void testNestedCheckCaseSensitive() throws SQLException {
assertThat(resultSetMetaDataNested.isCaseSensitive(1)).isTrue();
}
@Test
public void testNestedScale() throws SQLException {
assertThat(resultSetMetaDataNested.getScale(1)).isEqualTo(0L);
}
@Test
public void testNestedColumnDisplaySize() throws SQLException {
assertThat(resultSetMetaDataNested.getColumnDisplaySize(1)).isEqualTo(50);
}
@Test
public void testWrapperMethods() throws SQLException {
assertThat(resultSetMetaData.isWrapperFor(ResultSetMetaData.class)).isTrue();
assertThat(resultSetMetaData.isWrapperFor(BigQueryResultSetMetadata.class)).isTrue();
assertThat(resultSetMetaData.isWrapperFor(java.sql.Connection.class)).isFalse();
assertThat(resultSetMetaData.isWrapperFor(null)).isFalse();
Object unwrappedMeta = resultSetMetaData.unwrap(ResultSetMetaData.class);
assertThat(unwrappedMeta).isNotSameInstanceAs(resultSetMetaData);
assertThat(unwrappedMeta).isInstanceOf(BigQueryResultSetMetadata.class);
Object unwrappedImpl = resultSetMetaData.unwrap(BigQueryResultSetMetadata.class);
assertThat(unwrappedImpl).isNotSameInstanceAs(resultSetMetaData);
assertThat(unwrappedImpl).isInstanceOf(BigQueryResultSetMetadata.class);
SQLException e =
assertThrows(SQLException.class, () -> resultSetMetaData.unwrap(java.sql.Connection.class));
assertThat((Throwable) e).hasMessageThat().contains("Cannot unwrap to java.sql.Connection");
}
@ParameterizedTest
@EnumSource(StandardSQLTypeName.class)
public void testIsSearchableForAllTypes(StandardSQLTypeName type) throws SQLException {
Field field;
if (type == StandardSQLTypeName.STRUCT) {
field =
Field.of("col", StandardSQLTypeName.STRUCT, Field.of("sub", StandardSQLTypeName.STRING));
} else if (type == StandardSQLTypeName.ARRAY) {
field =
Field.newBuilder("col", StandardSQLTypeName.STRING).setMode(Field.Mode.REPEATED).build();
} else {
field = Field.of("col", type);
}
FieldList schemaFields = FieldList.of(field);
BigQueryJsonResultSet resultSet =
BigQueryJsonResultSet.of(
Schema.of(schemaFields), 1L, null, statement, new Thread[] {new Thread()});
ResultSetMetaData metaData = resultSet.getMetaData();
assertThat(metaData.isSearchable(1)).isTrue();
}
}