TableId setProjectId(String projectId) { Preconditions.checkArgument( !Strings.isNullOrEmpty(projectId), "Provided projectId is null or empty"); return TableId.of(projectId, getDataset(), getTable()); }
@Override public com.google.api.services.bigquery.model.Table call() { return bigQueryRpc.getTable( completeTableId.getProject(), completeTableId.getDataset(), completeTableId.getTable(), optionsMap); } },
@Override public Boolean call() { return bigQueryRpc.deleteTable( completeTableId.getProject(), completeTableId.getDataset(), completeTableId.getTable()); } },
@Override public TableDataInsertAllResponse call() throws Exception { return bigQueryRpc.insertAll( tableId.getProject(), tableId.getDataset(), tableId.getTable(), requestPb); } },
@Override public TableDataList call() { return serviceOptions .getBigQueryRpcV2() .listTableData( completeTableId.getProject(), completeTableId.getDataset(), completeTableId.getTable(), optionsMap); } },
@Test public void testGetNull() throws Exception { initializeExpectedDataset(1); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getTable(TABLE_INFO1.getTableId())).andReturn(null); replay(bigquery); initializeDataset(); assertNull(dataset.get(TABLE_INFO1.getTableId().getTable())); }
@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 testCreateTable() throws Exception { initializeExpectedDataset(2); Table expectedTable = new Table(serviceMockReturnsOptions, new TableInfo.BuilderImpl(TABLE_INFO1)); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.create(TABLE_INFO1)).andReturn(expectedTable); replay(bigquery); initializeDataset(); Table table = dataset.create(TABLE_INFO1.getTableId().getTable(), TABLE_DEFINITION); assertEquals(expectedTable, table); }
@Test public void testCopyFromString() throws Exception { initializeExpectedTable(2); expect(bigquery.getOptions()).andReturn(mockOptions); Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(COPY_JOB_INFO)); expect(bigquery.create(COPY_JOB_INFO)).andReturn(expectedJob); replay(bigquery); initializeTable(); Job job = table.copy(TABLE_ID2.getDataset(), TABLE_ID2.getTable()); assertSame(expectedJob, job); }
@Test public void testCopyFromId() throws Exception { initializeExpectedTable(2); expect(bigquery.getOptions()).andReturn(mockOptions); Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(COPY_JOB_INFO)); expect(bigquery.create(COPY_JOB_INFO)).andReturn(expectedJob); replay(bigquery); initializeTable(); Job job = table.copy(TABLE_ID2.getDataset(), TABLE_ID2.getTable()); assertSame(expectedJob, job); }
@Test public void testGet() throws Exception { initializeExpectedDataset(2); Table expectedTable = new Table(serviceMockReturnsOptions, new TableInfo.BuilderImpl(TABLE_INFO1)); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getTable(TABLE_INFO1.getTableId())).andReturn(expectedTable); replay(bigquery); initializeDataset(); Table table = dataset.get(TABLE_INFO1.getTableId().getTable()); assertNotNull(table); assertEquals(expectedTable, table); }
@Test public void testCreateTableWithOptions() throws Exception { initializeExpectedDataset(2); Table expectedTable = new Table(serviceMockReturnsOptions, new TableInfo.BuilderImpl(TABLE_INFO1)); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.create(TABLE_INFO1, BigQuery.TableOption.fields())).andReturn(expectedTable); replay(bigquery); initializeDataset(); Table table = dataset.create( TABLE_INFO1.getTableId().getTable(), TABLE_DEFINITION, BigQuery.TableOption.fields()); assertEquals(expectedTable, table); }
@Test public void testGetWithOptions() throws Exception { initializeExpectedDataset(2); Table expectedTable = new Table(serviceMockReturnsOptions, new TableInfo.BuilderImpl(TABLE_INFO1)); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getTable(TABLE_INFO1.getTableId(), BigQuery.TableOption.fields())) .andReturn(expectedTable); replay(bigquery); initializeDataset(); Table table = dataset.get(TABLE_INFO1.getTableId().getTable(), BigQuery.TableOption.fields()); assertNotNull(table); assertEquals(expectedTable, table); }
@Test public void testOf() { assertEquals(null, TABLE.getProject()); assertEquals("dataset", TABLE.getDataset()); assertEquals("table", TABLE.getTable()); assertEquals("project", TABLE_COMPLETE.getProject()); assertEquals("dataset", TABLE_COMPLETE.getDataset()); assertEquals("table", TABLE_COMPLETE.getTable()); }
@Test public void testGetTableFromTableIdWithoutProject() { TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT); TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable()); EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .andReturn(tableInfo.toPb()); EasyMock.replay(bigqueryRpcMock); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.getTable(tableId); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), 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 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 testToBuilder() { compareLoadConfiguration(LOAD_CONFIGURATION_CSV, LOAD_CONFIGURATION_CSV.toBuilder().build()); WriteChannelConfiguration configuration = LOAD_CONFIGURATION_CSV .toBuilder() .setDestinationTable(TableId.of("dataset", "newTable")) .build(); assertEquals("newTable", configuration.getDestinationTable().getTable()); configuration = configuration.toBuilder().setDestinationTable(TABLE_ID).build(); compareLoadConfiguration(LOAD_CONFIGURATION_CSV, configuration); }
@Test public void testToBuilder() { compareExtractJobConfiguration( EXTRACT_CONFIGURATION, EXTRACT_CONFIGURATION.toBuilder().build()); ExtractJobConfiguration job = EXTRACT_CONFIGURATION.toBuilder().setSourceTable(TableId.of("dataset", "newTable")).build(); assertEquals("newTable", job.getSourceTable().getTable()); job = job.toBuilder().setSourceTable(TABLE_ID).build(); compareExtractJobConfiguration(EXTRACT_CONFIGURATION, job); }
@Test public void testCancelJob() throws InterruptedException, TimeoutException { String destinationTableName = "test_cancel_query_job_table"; String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable(); TableId destinationTable = TableId.of(DATASET, destinationTableName); QueryJobConfiguration configuration = QueryJobConfiguration.newBuilder(query) .setDefaultDataset(DatasetId.of(DATASET)) .setDestinationTable(destinationTable) .build(); Job remoteJob = bigquery.create(JobInfo.of(configuration)); assertTrue(remoteJob.cancel()); remoteJob = remoteJob.waitFor(); assertNull(remoteJob.getStatus().getError()); }