@Override public Table build() { return new Table(bigquery, infoBuilder); } }
static Table fromPb(BigQuery bigquery, com.google.api.services.bigquery.model.Table tablePb) { return new Table(bigquery, new TableInfo.BuilderImpl(tablePb)); } }
private void initializeTable() { table = new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO)); }
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 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 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 testGetTableFromTableId() { 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(TABLE_ID); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); }
@Test public void testListTablesFromDatasetId() { 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))); 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(DatasetId.of(DATASET)); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.getValues(), Table.class)); }
@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 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 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 testListTablesFromDatasetIdWithProject() { bigquery = options.getService(); ImmutableList<Table> tableList = ImmutableList.of( new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO.setProjectId(OTHER_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(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .andReturn(result); EasyMock.replay(bigqueryRpcMock); Page<Table> page = bigquery.listTables(DatasetId.of(OTHER_PROJECT, DATASET)); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.getValues(), Table.class)); }
@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); }
@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 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 testReloadWithOptions() 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(), BigQuery.TableOption.fields())) .andReturn(expectedTable); replay(bigquery); initializeTable(); Table updatedTable = table.reload(BigQuery.TableOption.fields()); compareTable(expectedTable, updatedTable); }
@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 testUpdateTable() { TableInfo updatedTableInfo = TABLE_INFO.setProjectId(OTHER_PROJECT).toBuilder().setDescription("newDescription").build(); EasyMock.expect(bigqueryRpcMock.patch(updatedTableInfo.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(updatedTableInfo.toPb()); EasyMock.replay(bigqueryRpcMock); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.update(updatedTableInfo); 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 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); }