Skip to content

Commit 6b4081d

Browse files
authored
chore: Add IT for positional parameter on higher precision timestamp field (#4075)
* chore: Add IT for positional parameter on higher precision timestamp field * chore: Fix gemini comments * chore: Fix typo
1 parent e3eeb94 commit 6b4081d

File tree

1 file changed

+54
-34
lines changed

1 file changed

+54
-34
lines changed

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITHighPrecisionTimestamp.java

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.cloud.bigquery.it;
1717

18+
import static com.google.common.truth.Truth.assertThat;
1819
import static org.junit.jupiter.api.Assertions.assertEquals;
1920
import static org.junit.jupiter.api.Assertions.assertFalse;
2021
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -51,14 +52,14 @@
5152
import org.junit.jupiter.api.BeforeAll;
5253
import org.junit.jupiter.api.Test;
5354

54-
public class ITHighPrecisionTimestamp {
55+
class ITHighPrecisionTimestamp {
5556

56-
public static final String TEST_HIGH_PRECISION_TIMESTAMP_TABLE_NAME =
57-
"test_high_precision_timestamp";
57+
private static final String TEST_HIGH_PRECISION_TIMESTAMP_TABLE_NAME =
58+
generateTempTableName("test_high_precision_timestamp");
5859
private static BigQuery bigquery;
5960
private static final String DATASET = RemoteBigQueryHelper.generateDatasetName();
6061
private static TableId defaultTableId;
61-
public static final long TIMESTAMP_PICOSECOND_PRECISION = 12L;
62+
private static final long TIMESTAMP_PICOSECOND_PRECISION = 12L;
6263
private static final Field TIMESTAMP_HIGH_PRECISION_FIELD_SCHEMA =
6364
Field.newBuilder("timestampHighPrecisionField", StandardSQLTypeName.TIMESTAMP)
6465
.setTimestampPrecision(TIMESTAMP_PICOSECOND_PRECISION)
@@ -69,8 +70,12 @@ public class ITHighPrecisionTimestamp {
6970
private static final String TIMESTAMP2 = "1970-01-01T12:34:56.123456789123Z";
7071
private static final String TIMESTAMP3 = "2000-01-01T12:34:56.123456789123Z";
7172

73+
private static String generateTempTableName(String prefix) {
74+
return String.format("%s_%s", prefix, UUID.randomUUID().toString().substring(0, 8));
75+
}
76+
7277
@BeforeAll
73-
public static void beforeClass() {
78+
static void beforeClass() {
7479
BigQueryOptions.Builder builder =
7580
BigQueryOptions.newBuilder()
7681
.setDataFormatOptions(
@@ -111,21 +116,15 @@ public static void beforeClass() {
111116
}
112117

113118
@AfterAll
114-
public static void afterClass() {
119+
static void afterClass() {
115120
if (bigquery != null) {
116121
bigquery.delete(defaultTableId);
117122
RemoteBigQueryHelper.forceDelete(bigquery, DATASET);
118123
}
119124
}
120125

121-
private static String generateTempTableName() {
122-
return String.format(
123-
"insert_temp_%s%s",
124-
UUID.randomUUID().toString().substring(0, 6), TEST_HIGH_PRECISION_TIMESTAMP_TABLE_NAME);
125-
}
126-
127126
@Test
128-
public void query_highPrecisionTimestamp() throws InterruptedException {
127+
void query_highPrecisionTimestamp() throws InterruptedException {
129128
String sql =
130129
String.format("SELECT timestampHighPrecisionField FROM %s;", defaultTableId.getTable());
131130
QueryJobConfiguration queryJobConfiguration =
@@ -141,16 +140,14 @@ public void query_highPrecisionTimestamp() throws InterruptedException {
141140
.map(x -> (String) x.get(0).getValue())
142141
.collect(Collectors.toList());
143142
assertEquals(expected.length, timestamps.size());
144-
for (int i = 0; i < timestamps.size(); i++) {
145-
assertEquals(expected[i], timestamps.get(i));
146-
}
143+
assertThat(timestamps).containsAtLeastElementsIn(expected);
147144
}
148145

149146
@Test
150-
public void insert_highPrecisionTimestamp_ISOValidFormat() {
147+
void insert_highPrecisionTimestamp_ISOValidFormat() {
151148
StandardTableDefinition tableDefinition =
152149
StandardTableDefinition.newBuilder().setSchema(TABLE_SCHEMA).build();
153-
String tempTableName = generateTempTableName();
150+
String tempTableName = generateTempTableName("insert_temp");
154151
TableId tableId = TableId.of(DATASET, tempTableName);
155152
Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));
156153
assertNotNull(createdTable);
@@ -166,10 +163,10 @@ public void insert_highPrecisionTimestamp_ISOValidFormat() {
166163
}
167164

168165
@Test
169-
public void insert_highPrecisionTimestamp_invalidFormats() {
166+
void insert_highPrecisionTimestamp_invalidFormats() {
170167
StandardTableDefinition tableDefinition =
171168
StandardTableDefinition.newBuilder().setSchema(TABLE_SCHEMA).build();
172-
String tempTable = generateTempTableName();
169+
String tempTable = generateTempTableName("insert_temp");
173170
TableId tableId = TableId.of(DATASET, tempTable);
174171
Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));
175172
assertNotNull(createdTable);
@@ -207,7 +204,7 @@ public void insert_highPrecisionTimestamp_invalidFormats() {
207204
}
208205

209206
@Test
210-
public void queryNamedParameter_highPrecisionTimestamp() throws InterruptedException {
207+
void queryNamedParameter_highPrecisionTimestamp() throws InterruptedException {
211208
String query =
212209
String.format(
213210
"SELECT * FROM %s.%s WHERE timestampHighPrecisionField >= CAST(@timestampParam AS TIMESTAMP(12))",
@@ -219,6 +216,7 @@ public void queryNamedParameter_highPrecisionTimestamp() throws InterruptedExcep
219216
.setUseLegacySql(false)
220217
.addNamedParameter(
221218
"timestampParam",
219+
// For named parameters, java-bigquery does not expect the 'T'
222220
QueryParameterValue.timestamp("2000-01-01 12:34:56.123456789123Z"))
223221
.build();
224222

@@ -230,13 +228,38 @@ public void queryNamedParameter_highPrecisionTimestamp() throws InterruptedExcep
230228
.map(x -> (String) x.get(0).getValue())
231229
.collect(Collectors.toList());
232230
assertEquals(expected.length, timestamps.size());
233-
for (int i = 0; i < timestamps.size(); i++) {
234-
assertEquals(expected[i], timestamps.get(i));
235-
}
231+
assertThat(timestamps).containsAtLeastElementsIn(expected);
232+
}
233+
234+
@Test
235+
void queryPositionalParameter_highPrecisionTimestamp() throws InterruptedException {
236+
String query =
237+
String.format(
238+
"SELECT * FROM %s.%s WHERE timestampHighPrecisionField >= CAST(? AS TIMESTAMP(12))",
239+
DATASET, defaultTableId.getTable());
240+
241+
QueryJobConfiguration queryConfig =
242+
QueryJobConfiguration.newBuilder(query)
243+
.setDefaultDataset(DATASET)
244+
.setUseLegacySql(false)
245+
.addPositionalParameter(
246+
// For positional parameters, java-bigquery does not expect the 'T'
247+
QueryParameterValue.timestamp("2000-01-01 12:34:56.123456789123Z"))
248+
.build();
249+
250+
TableResult result = bigquery.query(queryConfig);
251+
assertNotNull(result);
252+
String[] expected = new String[] {TIMESTAMP1, TIMESTAMP3};
253+
List<String> timestamps =
254+
StreamSupport.stream(result.getValues().spliterator(), false)
255+
.map(x -> (String) x.get(0).getValue())
256+
.collect(Collectors.toList());
257+
assertEquals(expected.length, timestamps.size());
258+
assertThat(timestamps).containsAtLeastElementsIn(expected);
236259
}
237260

238261
@Test
239-
public void queryNamedParameter_highPrecisionTimestamp_microsLong() throws InterruptedException {
262+
void queryNamedParameter_highPrecisionTimestamp_microsLong() throws InterruptedException {
240263
String query =
241264
String.format(
242265
"SELECT * FROM %s.%s WHERE timestampHighPrecisionField >= CAST(@timestampParam AS TIMESTAMP(12))",
@@ -263,14 +286,11 @@ public void queryNamedParameter_highPrecisionTimestamp_microsLong() throws Inter
263286
.map(x -> (String) x.get(0).getValue())
264287
.collect(Collectors.toList());
265288
assertEquals(expected.length, timestamps.size());
266-
for (int i = 0; i < timestamps.size(); i++) {
267-
assertEquals(expected[i], timestamps.get(i));
268-
}
289+
assertThat(timestamps).containsAtLeastElementsIn(expected);
269290
}
270291

271292
@Test
272-
public void queryNamedParameter_highPrecisionTimestamp_microsISOString()
273-
throws InterruptedException {
293+
void queryNamedParameter_highPrecisionTimestamp_microsISOString() throws InterruptedException {
274294
String query =
275295
String.format(
276296
"SELECT * FROM %s.%s WHERE timestampHighPrecisionField >= CAST(@timestampParam AS TIMESTAMP(12))",
@@ -281,6 +301,7 @@ public void queryNamedParameter_highPrecisionTimestamp_microsISOString()
281301
.setDefaultDataset(DATASET)
282302
.setUseLegacySql(false)
283303
.addNamedParameter(
304+
// For named parameters, java-bigquery does not expect the 'T'
284305
"timestampParam", QueryParameterValue.timestamp("2000-01-01 12:34:56.123456Z"))
285306
.build();
286307

@@ -292,13 +313,11 @@ public void queryNamedParameter_highPrecisionTimestamp_microsISOString()
292313
.collect(Collectors.toList());
293314
String[] expected = new String[] {TIMESTAMP1, TIMESTAMP3};
294315
assertEquals(expected.length, timestamps.size());
295-
for (int i = 0; i < timestamps.size(); i++) {
296-
assertEquals(expected[i], timestamps.get(i));
297-
}
316+
assertThat(timestamps).containsAtLeastElementsIn(expected);
298317
}
299318

300319
@Test
301-
public void queryNamedParameter_highPrecisionTimestamp_noExplicitCastInQuery_fails() {
320+
void queryNamedParameter_highPrecisionTimestamp_noExplicitCastInQuery_fails() {
302321
String query =
303322
String.format(
304323
"SELECT * FROM %s.%s WHERE timestampHighPrecisionField >= @timestampParam",
@@ -309,6 +328,7 @@ public void queryNamedParameter_highPrecisionTimestamp_noExplicitCastInQuery_fai
309328
.setDefaultDataset(DATASET)
310329
.setUseLegacySql(false)
311330
.addNamedParameter(
331+
// For named parameters, java-bigquery does not expect the 'T'
312332
"timestampParam", QueryParameterValue.timestamp("2000-01-01 12:34:56.123456789123"))
313333
.build();
314334

0 commit comments

Comments
 (0)