Skip to content

Commit

Permalink
Fix setProjectId for insertAll (#4196)
Browse files Browse the repository at this point in the history
  • Loading branch information
Praful Makani authored and JesseLovelace committed Dec 12, 2018
1 parent bb3cfb4 commit 74a4517
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,13 @@ public Table apply(com.google.api.services.bigquery.model.Table table) {

@Override
public InsertAllResponse insertAll(InsertAllRequest request) {
final TableId tableId = request.getTable().setProjectId(getOptions().getProjectId());
final TableId tableId =
request
.getTable()
.setProjectId(
Strings.isNullOrEmpty(request.getTable().getProject())
? getOptions().getProjectId()
: request.getTable().getProject());
final TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest();
requestPb.setIgnoreUnknownValues(request.ignoreUnknownValues());
requestPb.setSkipInvalidRows(request.skipInvalidRows());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,57 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) {
assertEquals("ErrorMessage", response.getErrorsFor(0L).get(0).getMessage());
}

@Test
public void testInsertAllWithProjectInTable() {
Map<String, Object> row1 = ImmutableMap.<String, Object>of("field", "value1");
Map<String, Object> row2 = ImmutableMap.<String, Object>of("field", "value2");
List<RowToInsert> rows =
ImmutableList.of(new RowToInsert("row1", row1), new RowToInsert("row2", row2));
TableId tableId = TableId.of("project-different-from-option", DATASET, TABLE);
InsertAllRequest request =
InsertAllRequest.newBuilder(tableId)
.setRows(rows)
.setSkipInvalidRows(false)
.setIgnoreUnknownValues(true)
.setTemplateSuffix("suffix")
.build();
TableDataInsertAllRequest requestPb =
new TableDataInsertAllRequest()
.setRows(
Lists.transform(
rows,
new Function<RowToInsert, TableDataInsertAllRequest.Rows>() {
@Override
public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) {
return new TableDataInsertAllRequest.Rows()
.setInsertId(rowToInsert.getId())
.setJson(rowToInsert.getContent());
}
}))
.setSkipInvalidRows(false)
.setIgnoreUnknownValues(true)
.setTemplateSuffix("suffix");
TableDataInsertAllResponse responsePb =
new TableDataInsertAllResponse()
.setInsertErrors(
ImmutableList.of(
new TableDataInsertAllResponse.InsertErrors()
.setIndex(0L)
.setErrors(ImmutableList.of(new ErrorProto().setMessage("ErrorMessage")))));
EasyMock.expect(
bigqueryRpcMock.insertAll("project-different-from-option", DATASET, TABLE, requestPb))
.andReturn(responsePb);
EasyMock.replay(bigqueryRpcMock);
BigQueryOptions bigQueryOptions =
createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock);
bigquery = bigQueryOptions.getService();
InsertAllResponse response = bigquery.insertAll(request);
assertNotNull(response.getErrorsFor(0L));
assertNull(response.getErrorsFor(1L));
assertEquals(1, response.getErrorsFor(0L).size());
assertEquals("ErrorMessage", response.getErrorsFor(0L).get(0).getMessage());
}

@Test
public void testListTableData() {
EasyMock.expect(bigqueryRpcMock.listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS))
Expand Down

0 comments on commit 74a4517

Please sign in to comment.