Skip to content

Commit

Permalink
fix: bug fix for get method of Bigquery Dataset (#1379)
Browse files Browse the repository at this point in the history
* Adding the projectId used of getting the DataSet as a parameter for the issue: #1369

* Adding the projectId used of getting the DataSet as a parameter for the issue: #1369

* Added testcase for getTable with projectId.
  • Loading branch information
prash-mi committed Jun 19, 2021
1 parent 4a6906a commit f034a99
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.bigquery.BigQuery.DatasetOption;
import com.google.cloud.bigquery.BigQuery.TableListOption;
import com.google.cloud.bigquery.BigQuery.TableOption;
import com.google.common.base.Strings;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
Expand Down Expand Up @@ -275,7 +276,13 @@ public Page<Table> list(TableListOption... options) {
* @throws BigQueryException upon failure
*/
public Table get(String tableId, TableOption... options) {
return bigquery.getTable(TableId.of(getDatasetId().getDataset(), tableId), options);
// Adding the projectId used of getting the DataSet as a parameter for the issue:
// https://github.com/googleapis/java-bigquery/issues/1369
TableId tabId =
Strings.isNullOrEmpty(getDatasetId().getProject())
? TableId.of(getDatasetId().getDataset(), tableId)
: TableId.of(getDatasetId().getProject(), getDatasetId().getDataset(), tableId);
return bigquery.getTable(tabId, options);
}

/**
Expand Down
Expand Up @@ -77,6 +77,12 @@ public class DatasetTest {
TableInfo.newBuilder(TableId.of("dataset", "table2"), VIEW_DEFINITION).build();
private static final TableInfo TABLE_INFO3 =
TableInfo.newBuilder(TableId.of("dataset", "table3"), EXTERNAL_TABLE_DEFINITION).build();
private static final String NEW_PROJECT_ID = "projectId2";
private static final TableId TABLE_ID1 = TableId.of(NEW_PROJECT_ID, "dataset", "table3");
private static final TableInfo TABLE_INFO4 =
TableInfo.newBuilder(
TableId.of(NEW_PROJECT_ID, "dataset", "table3"), EXTERNAL_TABLE_DEFINITION)
.build();

@Rule public MockitoRule rule;

Expand Down Expand Up @@ -255,6 +261,16 @@ public void testGet() {
verify(bigquery).getTable(TABLE_INFO1.getTableId());
}

@Test
public void testGetTableWithNewProjectId() {
Table expectedTable = new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO4));
when(bigquery.getTable(TABLE_ID1, null)).thenReturn(expectedTable);
Table table = bigquery.getTable(TABLE_ID1, null);
assertNotNull(table);
assertEquals(table.getTableId().getProject(), NEW_PROJECT_ID);
verify(bigquery).getTable(TABLE_ID1, null);
}

@Test
public void testGetNull() {
when(bigquery.getTable(TABLE_INFO1.getTableId())).thenReturn(null);
Expand Down

0 comments on commit f034a99

Please sign in to comment.