Skip to content

Commit

Permalink
add unit tests for schema handling
Browse files Browse the repository at this point in the history
  • Loading branch information
the-other-tim-brown committed Sep 13, 2022
1 parent 7c2dc11 commit e0c78e0
Showing 1 changed file with 35 additions and 0 deletions.
Expand Up @@ -43,6 +43,7 @@
import com.google.api.services.bigquery.model.TableDataInsertAllResponse;
import com.google.api.services.bigquery.model.TableDataList;
import com.google.api.services.bigquery.model.TableRow;
import com.google.api.services.bigquery.model.TableSchema;
import com.google.cloud.Policy;
import com.google.cloud.ServiceOptions;
import com.google.cloud.Tuple;
Expand Down Expand Up @@ -812,6 +813,23 @@ public void testCreateTable() {
verify(bigqueryRpcMock).create(tableInfo.toPb(), EMPTY_RPC_OPTIONS);
}

@Test
public void tesCreateExternalTable() {
TableInfo createTableInfo =
TableInfo.of(TABLE_ID, ExternalTableDefinition.newBuilder().setSchema(TABLE_SCHEMA).build()).setProjectId(OTHER_PROJECT);

com.google.api.services.bigquery.model.Table expectedCreateInput = createTableInfo.toPb().setSchema(TABLE_SCHEMA.toPb());
expectedCreateInput.getExternalDataConfiguration().setSchema(null);
when(bigqueryRpcMock.create(expectedCreateInput, EMPTY_RPC_OPTIONS))
.thenReturn(createTableInfo.toPb());
BigQueryOptions bigQueryOptions =
createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock);
bigquery = bigQueryOptions.getService();
Table table = bigquery.create(createTableInfo);
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(createTableInfo)), table);
verify(bigqueryRpcMock).create(expectedCreateInput, EMPTY_RPC_OPTIONS);
}

@Test
public void testCreateTableWithoutProject() {
TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT);
Expand Down Expand Up @@ -1189,6 +1207,23 @@ public void testUpdateTable() {
verify(bigqueryRpcMock).patch(updatedTableInfo.toPb(), EMPTY_RPC_OPTIONS);
}

@Test
public void testUpdateExternalTableWithNewSchema() {
TableInfo updatedTableInfo =
TableInfo.of(TABLE_ID, ExternalTableDefinition.newBuilder().setSchema(TABLE_SCHEMA).build()).setProjectId(OTHER_PROJECT);

com.google.api.services.bigquery.model.Table expectedPatchInput = updatedTableInfo.toPb().setSchema(TABLE_SCHEMA.toPb());
expectedPatchInput.getExternalDataConfiguration().setSchema(null);
when(bigqueryRpcMock.patch(expectedPatchInput, EMPTY_RPC_OPTIONS))
.thenReturn(updatedTableInfo.toPb());
BigQueryOptions bigQueryOptions =
createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock);
bigquery = bigQueryOptions.getService();
Table table = bigquery.update(updatedTableInfo);
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfo)), table);
verify(bigqueryRpcMock).patch(expectedPatchInput, EMPTY_RPC_OPTIONS);
}

@Test
public void testUpdateTableWithoutProject() {
TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT);
Expand Down

0 comments on commit e0c78e0

Please sign in to comment.