From 8e345821e91c57b65d91c670bd90022caec6b0fd Mon Sep 17 00:00:00 2001 From: Liam Huffman Date: Thu, 9 Jan 2025 11:31:57 -0800 Subject: [PATCH] Move tests into IT test file --- .../google/cloud/bigquery/ReadApiTest.java | 140 ------------------ .../cloud/bigquery/it/ITBigQueryTest.java | 56 +++++++ 2 files changed, 56 insertions(+), 140 deletions(-) delete mode 100644 google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ReadApiTest.java diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ReadApiTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ReadApiTest.java deleted file mode 100644 index 87940e829..000000000 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ReadApiTest.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright 2025 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; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.sql.ResultSet; -import org.junit.Test; - -public class ReadApiTest { - public int rowLimit = 5000; - private final String QUERY = - "SELECT * FROM bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2017 LIMIT %s"; - - @Test - public void testWithReadApi() throws BigQuerySQLException { - // Job timeout is somewhat arbitrary - just ensures that fast query is not used. - // min result size and page row count ratio ensure that the ReadAPI is used. - ConnectionSettings connectionSettingsReadAPIEnabledFastQueryDisabled = - ConnectionSettings.newBuilder() - .setUseReadAPI(true) - .setJobTimeoutMs(Long.MAX_VALUE) - .setMinResultSize(500) - .setTotalToPageRowCountRatio(1) - .build(); - - Connection connectionReadAPIEnabled = - BigQueryOptions.getDefaultInstance() - .getService() - .createConnection(connectionSettingsReadAPIEnabledFastQueryDisabled); - - String selectQuery = String.format(QUERY, rowLimit); - try { - BigQueryResult bigQueryResultSet = connectionReadAPIEnabled.executeSelect(selectQuery); - ResultSet rs = bigQueryResultSet.getResultSet(); - // Paginate results to avoid an InterruptedException - while (rs.next()) {} - - assertTrue(bigQueryResultSet.getBigQueryResultStats().getQueryStatistics().getUseReadApi()); - } catch (Exception e) { - e.printStackTrace(); - } finally { - connectionReadAPIEnabled.close(); - } - } - - @Test - public void testWithFastQueryAndReadApi() throws BigQuerySQLException { - ConnectionSettings connectionSettingsReadAPIEnabled = - ConnectionSettings.newBuilder() - .setUseReadAPI(true) - .setMinResultSize(500) - .setTotalToPageRowCountRatio(1) - .build(); - - Connection connectionReadAPIEnabled = - BigQueryOptions.getDefaultInstance() - .getService() - .createConnection(connectionSettingsReadAPIEnabled); - - String selectQuery = String.format(QUERY, rowLimit); - try { - BigQueryResult bigQueryResultSet = connectionReadAPIEnabled.executeSelect(selectQuery); - ResultSet rs = bigQueryResultSet.getResultSet(); - while (rs.next()) {} - - assertTrue(bigQueryResultSet.getBigQueryResultStats().getQueryStatistics().getUseReadApi()); - } catch (Exception e) { - e.printStackTrace(); - } finally { - connectionReadAPIEnabled.close(); - } - } - - @Test - public void testWithFastQueryAndWithoutReadApi() throws BigQuerySQLException { - ConnectionSettings connectionSettingsReadAPIDisabled = - ConnectionSettings.newBuilder().setUseReadAPI(false).build(); - - Connection connectionReadAPIDisabled = - BigQueryOptions.getDefaultInstance() - .getService() - .createConnection(connectionSettingsReadAPIDisabled); - - String selectQuery = String.format(QUERY, rowLimit); - try { - BigQueryResult bigQueryResultSet = connectionReadAPIDisabled.executeSelect(selectQuery); - ResultSet rs = bigQueryResultSet.getResultSet(); - while (rs.next()) {} - - assertFalse(bigQueryResultSet.getBigQueryResultStats().getQueryStatistics().getUseReadApi()); - } catch (Exception e) { - e.printStackTrace(); - } finally { - connectionReadAPIDisabled.close(); - } - } - - @Test - public void testWithoutReadApi() throws BigQuerySQLException { - ConnectionSettings connectionSettingsReadAPIDisabledFastQueryDisabled = - ConnectionSettings.newBuilder() - .setUseReadAPI(false) - .setJobTimeoutMs(Long.MAX_VALUE) - .build(); - - Connection connectionReadAPIDisabled = - BigQueryOptions.getDefaultInstance() - .getService() - .createConnection(connectionSettingsReadAPIDisabledFastQueryDisabled); - - String selectQuery = String.format(QUERY, rowLimit); - try { - BigQueryResult bigQueryResultSet = connectionReadAPIDisabled.executeSelect(selectQuery); - ResultSet rs = bigQueryResultSet.getResultSet(); - while (rs.next()) {} - - assertFalse(bigQueryResultSet.getBigQueryResultStats().getQueryStatistics().getUseReadApi()); - } catch (Exception e) { - e.printStackTrace(); - } finally { - connectionReadAPIDisabled.close(); - } - } -} diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index bf8d51314..26b2eb2a4 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -3489,6 +3489,61 @@ public void testExecuteSelectDefaultConnectionSettings() throws SQLException { String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;"; BigQueryResult bigQueryResult = connection.executeSelect(query); assertEquals(42, bigQueryResult.getTotalRows()); + assertFalse(bigQueryResult.getBigQueryResultStats().getQueryStatistics().getUseReadApi()); + } + + @Test + public void testExecuteSelectWithReadApi() throws SQLException { + final int rowLimit = 5000; + final String QUERY = + "SELECT * FROM bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2017 LIMIT %s"; + // Job timeout is somewhat arbitrary - just ensures that fast query is not used. + // min result size and page row count ratio ensure that the ReadAPI is used. + ConnectionSettings connectionSettingsReadAPIEnabledFastQueryDisabled = + ConnectionSettings.newBuilder() + .setUseReadAPI(true) + .setJobTimeoutMs(Long.MAX_VALUE) + .setMinResultSize(500) + .setTotalToPageRowCountRatio(1) + .build(); + + Connection connectionReadAPIEnabled = bigquery.createConnection(connectionSettingsReadAPIEnabledFastQueryDisabled); + + String selectQuery = String.format(QUERY, rowLimit); + + BigQueryResult bigQueryResultSet = connectionReadAPIEnabled.executeSelect(selectQuery); + ResultSet rs = bigQueryResultSet.getResultSet(); + // Paginate results to avoid an InterruptedException + while (rs.next()) {} + + assertTrue(bigQueryResultSet.getBigQueryResultStats().getQueryStatistics().getUseReadApi()); + connectionReadAPIEnabled.close(); + } + + @Test + public void testExecuteSelectWithFastQueryReadApi() throws SQLException { + final int rowLimit = 5000; + final String QUERY = + "SELECT * FROM bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2017 LIMIT %s"; + // min result size and page row count ratio ensure that the ReadAPI is used. + ConnectionSettings connectionSettingsReadAPIEnabledFastQueryDisabled = + ConnectionSettings.newBuilder() + .setUseReadAPI(true) + .setMinResultSize(500) + .setTotalToPageRowCountRatio(1) + .build(); + + Connection connectionReadAPIEnabled = bigquery.createConnection(connectionSettingsReadAPIEnabledFastQueryDisabled); + + String selectQuery = String.format(QUERY, rowLimit); + + BigQueryResult bigQueryResultSet = connectionReadAPIEnabled.executeSelect(selectQuery); + ResultSet rs = bigQueryResultSet.getResultSet(); + // Paginate results to avoid an InterruptedException + while (rs.next()) {} + + assertTrue(bigQueryResultSet.getBigQueryResultStats().getQueryStatistics().getUseReadApi()); + connectionReadAPIEnabled.close(); } @Test @@ -3522,6 +3577,7 @@ public void testExecuteSelectWithCredentials() throws SQLException { + TABLE_ID_LARGE.getTable(); // Large query result is needed to use BigQueryReadClient. BigQueryResult bigQueryResult = connectionGoodCredentials.executeSelect(query); assertEquals(313348, bigQueryResult.getTotalRows()); + assertTrue(bigQueryResult.getBigQueryResultStats().getQueryStatistics().getUseReadApi()); // Scenario 2. // Create a new bigQuery object but explicitly an invalid credential.