Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bug fix for get method of Bigquery Dataset #1379

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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