Skip to content

Commit

Permalink
BigQuery: Fix TableId behaviour for non default BigQueryClient project (
Browse files Browse the repository at this point in the history
#4115)

* Fixes create, update and delete methods

* remove comments

* Fix formatting
  • Loading branch information
Praful Makani authored and chingor13 committed Nov 30, 2018
1 parent 8da11b0 commit e53621b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ public com.google.api.services.bigquery.model.Dataset call() {
@Override
public Table create(TableInfo tableInfo, TableOption... options) {
final com.google.api.services.bigquery.model.Table tablePb =
tableInfo.setProjectId(getOptions().getProjectId()).toPb();
tableInfo
.setProjectId(
Strings.isNullOrEmpty(tableInfo.getTableId().getProject())
? getOptions().getProjectId()
: tableInfo.getTableId().getProject())
.toPb();
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
try {
return Table.fromPb(this,
Expand Down Expand Up @@ -345,7 +350,11 @@ public boolean delete(String datasetId, String tableId) {

@Override
public boolean delete(TableId tableId) {
final TableId completeTableId = tableId.setProjectId(getOptions().getProjectId());
final TableId completeTableId = tableId.setProjectId(
Strings.isNullOrEmpty(tableId.getProject())
? getOptions().getProjectId()
: tableId.getProject()
);
try {
return runWithRetries(new Callable<Boolean>() {
@Override
Expand Down Expand Up @@ -380,7 +389,12 @@ public com.google.api.services.bigquery.model.Dataset call() {
@Override
public Table update(TableInfo tableInfo, TableOption... options) {
final com.google.api.services.bigquery.model.Table tablePb =
tableInfo.setProjectId(getOptions().getProjectId()).toPb();
tableInfo
.setProjectId(
Strings.isNullOrEmpty(tableInfo.getTableId().getProject())
? getOptions().getProjectId()
: tableInfo.getTableId().getProject())
.toPb();
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
try {
return Table.fromPb(this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,20 @@ public void testCreateTable() {
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table);
}

@Test
public void testCreateTableWithoutProject() {
TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT);
TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable());
tableInfo.toBuilder().setTableId(tableId);
EasyMock.expect(bigqueryRpcMock.create(tableInfo.toPb(), EMPTY_RPC_OPTIONS))
.andReturn(tableInfo.toPb());
EasyMock.replay(bigqueryRpcMock);
BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock);
bigquery = bigQueryOptions.getService();
Table table = bigquery.create(tableInfo);
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table);
}

@Test
public void testCreateTableWithSelectedFields() {
Capture<Map<BigQueryRpc.Option, Object>> capturedOptions = Capture.newInstance();
Expand Down Expand Up @@ -728,6 +742,16 @@ public void testDeleteTableFromTableIdWithProject() {
assertTrue(bigquery.delete(tableId));
}

@Test
public void testDeleteTableFromTableIdWithoutProject() {
TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable());
EasyMock.expect(bigqueryRpcMock.deleteTable(PROJECT, DATASET, TABLE)).andReturn(true);
EasyMock.replay(bigqueryRpcMock);
BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock);
bigquery = bigQueryOptions.getService();
assertTrue(bigquery.delete(tableId));
}

@Test
public void testUpdateTable() {
TableInfo updatedTableInfo =
Expand All @@ -741,6 +765,20 @@ public void testUpdateTable() {
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfo)), table);
}

@Test
public void testUpdateTableWithoutProject() {
TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT);
TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable());
tableInfo.toBuilder().setTableId(tableId);
EasyMock.expect(bigqueryRpcMock.patch(tableInfo.toPb(), EMPTY_RPC_OPTIONS))
.andReturn(tableInfo.toPb());
EasyMock.replay(bigqueryRpcMock);
BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock);
bigquery = bigQueryOptions.getService();
Table table = bigquery.update(tableInfo);
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table);
}

@Test
public void testUpdateTableWithSelectedFields() {
Capture<Map<BigQueryRpc.Option, Object>> capturedOptions = Capture.newInstance();
Expand Down

0 comments on commit e53621b

Please sign in to comment.