@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 testExtractDataUris() throws Exception { initializeExpectedTable(2); expect(bigquery.getOptions()).andReturn(mockOptions); Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(EXTRACT_JOB_INFO)); expect(bigquery.create(EXTRACT_JOB_INFO)).andReturn(expectedJob); replay(bigquery); initializeTable(); Job job = table.extract("CSV", ImmutableList.of("URI")); assertSame(expectedJob, job); }
/** Example of copying multiple tables to a destination. */ public void copyTables(String datasetId, String destinationTableId) throws InterruptedException { generateTableWithDdl(datasetId, "table1"); generateTableWithDdl(datasetId, "table2"); // [START bigquery_copy_table_multiple_source] TableId destinationTable = TableId.of(datasetId, destinationTableId); CopyJobConfiguration configuration = CopyJobConfiguration.newBuilder( destinationTable, Arrays.asList(TableId.of(datasetId, "table1"), TableId.of(datasetId, "table2"))) .build(); // Copy the tables. Job job = bigquery.create(JobInfo.of(configuration)); job = job.waitFor(); // Check the table StandardTableDefinition table = bigquery.getTable(destinationTable).getDefinition(); System.out.println("State: " + job.getStatus().getState()); System.out.printf("Copied %d rows.\n", table.getNumRows()); // [END bigquery_copy_table_multiple_source] }
@Test public void testToAndFromPb() { initializeExpectedTable(4); replay(bigquery); compareTable(expectedTable, Table.fromPb(serviceMockReturnsOptions, expectedTable.toPb())); }
@Test public void testReload() throws Exception { initializeExpectedTable(4); TableInfo updatedInfo = TABLE_INFO.toBuilder().setDescription("Description").build(); Table expectedTable = new Table(serviceMockReturnsOptions, new TableInfo.BuilderImpl(updatedInfo)); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getTable(TABLE_INFO.getTableId())).andReturn(expectedTable); replay(bigquery); initializeTable(); Table updatedTable = table.reload(); compareTable(expectedTable, updatedTable); }
@Test public void testListTables() { bigquery = options.getService(); ImmutableList<Table> tableList = ImmutableList.of( new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO)), new Table(bigquery, new TableInfo.BuilderImpl(MODEL_TABLE_INFO_WITH_PROJECT))); Tuple<String, Iterable<com.google.api.services.bigquery.model.Table>> result = Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); EasyMock.expect(bigqueryRpcMock.listTables(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .andReturn(result); EasyMock.replay(bigqueryRpcMock); Page<Table> page = bigquery.listTables(DATASET); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.getValues(), Table.class)); }
@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 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 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 testLoadDataUris() throws Exception { initializeExpectedTable(2); expect(bigquery.getOptions()).andReturn(mockOptions); Job expectedJob = new Job(serviceMockReturnsOptions, new JobInfo.BuilderImpl(LOAD_JOB_INFO)); expect(bigquery.create(LOAD_JOB_INFO)).andReturn(expectedJob); replay(bigquery); initializeTable(); Job job = table.load(FormatOptions.json(), ImmutableList.of("URI")); assertSame(expectedJob, job); }
@Test public void testDeleteFalse() { initializeExpectedTable(1); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.delete(TABLE_INFO.getTableId())).andReturn(false); replay(bigquery); initializeTable(); assertFalse(table.delete()); }
@Test public void testGetTable() { EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Table table = bigquery.getTable(DATASET, TABLE); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); }
@Test public void testInsert() throws Exception { initializeExpectedTable(1); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.insertAll(INSERT_ALL_REQUEST)).andReturn(EMPTY_INSERT_ALL_RESPONSE); replay(bigquery); initializeTable(); InsertAllResponse response = table.insert(ROWS_TO_INSERT); assertSame(EMPTY_INSERT_ALL_RESPONSE, response); }
private void initializeExpectedTable(int optionsCalls) { expect(serviceMockReturnsOptions.getOptions()).andReturn(mockOptions).times(optionsCalls); replay(serviceMockReturnsOptions); bigquery = createStrictMock(BigQuery.class); expectedTable = new Table(serviceMockReturnsOptions, new TableInfo.BuilderImpl(TABLE_INFO)); }
@Test public void testGetTableWithSelectedFields() { Capture<Map<BigQueryRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( bigqueryRpcMock.getTable(eq(PROJECT), eq(DATASET), eq(TABLE), capture(capturedOptions))) .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Table table = bigquery.getTable(TABLE_ID, TABLE_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(TABLE_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("tableReference")); assertTrue(selector.contains("schema")); assertTrue(selector.contains("etag")); assertEquals(31, selector.length()); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); }
@Test public void testCreateTableWithSelectedFields() { Capture<Map<BigQueryRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( bigqueryRpcMock.create(eq(TABLE_INFO_WITH_PROJECT.toPb()), capture(capturedOptions))) .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Table table = bigquery.create(TABLE_INFO, TABLE_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(TABLE_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("tableReference")); assertTrue(selector.contains("schema")); assertTrue(selector.contains("etag")); assertEquals(31, selector.length()); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); }
@Test public void testUpdateTableWithSelectedFields() { Capture<Map<BigQueryRpc.Option, Object>> capturedOptions = Capture.newInstance(); TableInfo updatedTableInfo = TABLE_INFO.toBuilder().setDescription("newDescription").build(); TableInfo updatedTableInfoWithProject = TABLE_INFO_WITH_PROJECT.toBuilder().setDescription("newDescription").build(); EasyMock.expect( bigqueryRpcMock.patch(eq(updatedTableInfoWithProject.toPb()), capture(capturedOptions))) .andReturn(updatedTableInfoWithProject.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Table table = bigquery.update(updatedTableInfo, TABLE_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(TABLE_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("tableReference")); assertTrue(selector.contains("schema")); assertTrue(selector.contains("etag")); assertEquals(31, selector.length()); assertEquals( new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfoWithProject)), table); }
@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 testGetTableFromTableIdWithProject() { TableInfo tableInfo = TABLE_INFO.setProjectId(OTHER_PROJECT); TableId tableId = TABLE_ID.setProjectId(OTHER_PROJECT); EasyMock.expect(bigqueryRpcMock.getTable(OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .andReturn(tableInfo.toPb()); EasyMock.replay(bigqueryRpcMock); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.getTable(tableId); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); }
@Test public void testCreateTable() { TableInfo tableInfo = TABLE_INFO.setProjectId(OTHER_PROJECT); EasyMock.expect(bigqueryRpcMock.create(tableInfo.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(tableInfo.toPb()); EasyMock.replay(bigqueryRpcMock); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.create(tableInfo); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); }