diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java index 7395d4d18d69..05be4df39773 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java @@ -33,9 +33,12 @@ import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.util.Iterator; +import java.util.List; public class DatasetTest { @@ -48,7 +51,12 @@ public class DatasetTest { ExternalTableInfo.builder(TableId.of("dataset", "table2"), ExternalDataConfiguration.of(ImmutableList.of("URI"), Schema.of(), FormatOptions.csv())) .build()); + private static final UserDefinedFunction FUNCTION1 = UserDefinedFunction.inline("inline"); + private static final UserDefinedFunction FUNCTION2 = UserDefinedFunction.inline("gs://b/f"); + private static final List FUNCTIONS = ImmutableList.of(FUNCTION1, FUNCTION2); + @Rule + public ExpectedException thrown = ExpectedException.none(); private BigQuery bigquery; private Dataset dataset; @@ -69,6 +77,12 @@ public void testInfo() throws Exception { replay(bigquery); } + @Test + public void testBigQuery() throws Exception { + assertSame(bigquery, dataset.bigquery()); + replay(bigquery); + } + @Test public void testExists_True() throws Exception { BigQuery.DatasetOption[] expectedOptions = {BigQuery.DatasetOption.fields()}; @@ -119,7 +133,28 @@ public void testUpdate() throws Exception { expect(bigquery.update(updatedInfo)).andReturn(updatedInfo); replay(bigquery); Dataset updatedDataset = dataset.update(updatedInfo); - assertSame(bigquery, dataset.bigquery()); + assertSame(bigquery, updatedDataset.bigquery()); + assertEquals(updatedInfo, updatedDataset.info()); + } + + @Test + public void testUpdateWithDifferentId() throws Exception { + DatasetInfo updatedInfo = DATASET_INFO.toBuilder() + .datasetId(DatasetId.of("dataset2")) + .description("Description") + .build(); + replay(bigquery); + thrown.expect(IllegalArgumentException.class); + dataset.update(updatedInfo); + } + + @Test + public void testUpdateWithOptions() throws Exception { + DatasetInfo updatedInfo = DATASET_INFO.toBuilder().description("Description").build(); + expect(bigquery.update(updatedInfo, BigQuery.DatasetOption.fields())).andReturn(updatedInfo); + replay(bigquery); + Dataset updatedDataset = dataset.update(updatedInfo, BigQuery.DatasetOption.fields()); + assertSame(bigquery, updatedDataset.bigquery()); assertEquals(updatedInfo, updatedDataset.info()); } @@ -150,6 +185,27 @@ public void testList() throws Exception { verify(bigqueryOptions); } + @Test + public void testListWithOptions() throws Exception { + BigQueryOptions bigqueryOptions = createStrictMock(BigQueryOptions.class); + PageImpl tableInfoPage = new PageImpl<>(null, "c", TABLE_INFO_RESULTS); + expect(bigquery.listTables(DATASET_INFO.datasetId(), BigQuery.TableListOption.maxResults(10L))) + .andReturn(tableInfoPage); + expect(bigquery.options()).andReturn(bigqueryOptions); + expect(bigqueryOptions.service()).andReturn(bigquery); + replay(bigquery, bigqueryOptions); + Page tablePage = dataset.list(BigQuery.TableListOption.maxResults(10L)); + Iterator tableInfoIterator = tableInfoPage.values().iterator(); + Iterator
tableIterator = tablePage.values().iterator(); + while (tableInfoIterator.hasNext() && tableIterator.hasNext()) { + assertEquals(tableInfoIterator.next(), tableIterator.next().info()); + } + assertFalse(tableInfoIterator.hasNext()); + assertFalse(tableIterator.hasNext()); + assertEquals(tableInfoPage.nextPageCursor(), tablePage.nextPageCursor()); + verify(bigqueryOptions); + } + @Test public void testGet() throws Exception { BaseTableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), Schema.of()).build(); @@ -167,6 +223,17 @@ public void testGetNull() throws Exception { assertNull(dataset.get("table1")); } + @Test + public void testGetWithOptions() throws Exception { + BaseTableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), Schema.of()).build(); + expect(bigquery.getTable(TableId.of("dataset", "table1"), BigQuery.TableOption.fields())) + .andReturn(info); + replay(bigquery); + Table table = dataset.get("table1", BigQuery.TableOption.fields()); + assertNotNull(table); + assertEquals(info, table.info()); + } + @Test public void testCreateTable() throws Exception { TableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), Schema.of(FIELD)).build(); @@ -176,6 +243,15 @@ public void testCreateTable() throws Exception { assertEquals(info, table.info()); } + @Test + public void testCreateTableWithOptions() throws Exception { + TableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), Schema.of(FIELD)).build(); + expect(bigquery.create(info, BigQuery.TableOption.fields())).andReturn(info); + replay(bigquery); + Table table = dataset.create("table1", Schema.of(FIELD), BigQuery.TableOption.fields()); + assertEquals(info, table.info()); + } + @Test public void testCreateView() throws Exception { ViewInfo info = ViewInfo.builder(TableId.of("dataset", "table2"), "QUERY").build(); @@ -185,6 +261,24 @@ public void testCreateView() throws Exception { assertEquals(info, table.info()); } + @Test + public void testCreateViewWithUserDefinedFunctions() throws Exception { + ViewInfo info = ViewInfo.builder(TableId.of("dataset", "table2"), "QUERY", FUNCTIONS).build(); + expect(bigquery.create(info)).andReturn(info); + replay(bigquery); + Table table = dataset.create("table2", "QUERY", FUNCTIONS); + assertEquals(info, table.info()); + } + + @Test + public void testCreateViewWithOptions() throws Exception { + ViewInfo info = ViewInfo.builder(TableId.of("dataset", "table2"), "QUERY").build(); + expect(bigquery.create(info, BigQuery.TableOption.fields())).andReturn(info); + replay(bigquery); + Table table = dataset.create("table2", "QUERY", BigQuery.TableOption.fields()); + assertEquals(info, table.info()); + } + @Test public void testCreateExternalTable() throws Exception { ExternalTableInfo info = ExternalTableInfo.builder(TableId.of("dataset", "table3"), @@ -197,6 +291,18 @@ public void testCreateExternalTable() throws Exception { assertEquals(info, table.info()); } + @Test + public void testCreateExternalTableWithOptions() throws Exception { + ExternalTableInfo info = ExternalTableInfo.builder(TableId.of("dataset", "table3"), + ExternalDataConfiguration.of(ImmutableList.of("URI"), Schema.of(), FormatOptions.csv())) + .build(); + expect(bigquery.create(info, BigQuery.TableOption.fields())).andReturn(info); + replay(bigquery); + Table table = dataset.create("table3", ExternalDataConfiguration.of( + ImmutableList.of("URI"), Schema.of(), FormatOptions.csv()), BigQuery.TableOption.fields()); + assertEquals(info, table.info()); + } + @Test public void testLoad() throws Exception { expect(bigquery.getDataset(DATASET_INFO.datasetId().dataset())).andReturn(DATASET_INFO); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java index 99b3e80a0206..d99176b6904c 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java @@ -58,6 +58,12 @@ public void testInfo() throws Exception { replay(bigquery); } + @Test + public void testBigQuery() throws Exception { + assertSame(bigquery, job.bigquery()); + replay(bigquery); + } + @Test public void testExists_True() throws Exception { BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields()}; diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java index fb022cb13b40..e5b152a91a5f 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java @@ -36,7 +36,9 @@ import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.util.Iterator; import java.util.List; @@ -75,6 +77,8 @@ public class TableTest { private static final Iterable> ROWS = ImmutableList.of( (List) ImmutableList.of(FIELD_VALUE1), ImmutableList.of(FIELD_VALUE2)); + @Rule + public ExpectedException thrown = ExpectedException.none(); private BigQuery bigquery; private Table table; @@ -95,6 +99,12 @@ public void testInfo() throws Exception { replay(bigquery); } + @Test + public void testBigQuery() throws Exception { + assertSame(bigquery, table.bigquery()); + replay(bigquery); + } + @Test public void testExists_True() throws Exception { BigQuery.TableOption[] expectedOptions = {BigQuery.TableOption.fields()}; @@ -139,6 +149,48 @@ public void testReloadWithOptions() throws Exception { assertEquals(updatedInfo, updatedTable.info()); } + @Test + public void testUpdate() throws Exception { + BaseTableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build(); + expect(bigquery.update(updatedInfo)).andReturn(updatedInfo); + replay(bigquery); + Table updatedTable = table.update(updatedInfo); + assertSame(bigquery, updatedTable.bigquery()); + assertEquals(updatedInfo, updatedTable.info()); + } + + @Test + public void testUpdateWithDifferentId() throws Exception { + TableInfo updatedInfo = TABLE_INFO.toBuilder() + .tableId(TableId.of("dataset", "table3")) + .description("Description") + .build(); + replay(bigquery); + thrown.expect(IllegalArgumentException.class); + table.update(updatedInfo); + } + + @Test + public void testUpdateWithDifferentDatasetId() throws Exception { + TableInfo updatedInfo = TABLE_INFO.toBuilder() + .tableId(TableId.of("dataset1", "table1")) + .description("Description") + .build(); + replay(bigquery); + thrown.expect(IllegalArgumentException.class); + table.update(updatedInfo); + } + + @Test + public void testUpdateWithOptions() throws Exception { + BaseTableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build(); + expect(bigquery.update(updatedInfo, BigQuery.TableOption.fields())).andReturn(updatedInfo); + replay(bigquery); + Table updatedTable = table.update(updatedInfo, BigQuery.TableOption.fields()); + assertSame(bigquery, updatedTable.bigquery()); + assertEquals(updatedInfo, updatedTable.info()); + } + @Test public void testDelete() throws Exception { expect(bigquery.delete(TABLE_INFO.tableId())).andReturn(true); @@ -173,6 +225,18 @@ public void testList() throws Exception { assertTrue(Iterators.elementsEqual(tableDataIterator, dataIterator)); } + @Test + public void testListWithOptions() throws Exception { + PageImpl> tableDataPage = new PageImpl<>(null, "c", ROWS); + expect(bigquery.listTableData(TABLE_ID1, BigQuery.TableDataListOption.maxResults(10L))) + .andReturn(tableDataPage); + replay(bigquery); + Page> dataPage = table.list(BigQuery.TableDataListOption.maxResults(10L)); + Iterator> tableDataIterator = tableDataPage.values().iterator(); + Iterator> dataIterator = dataPage.values().iterator(); + assertTrue(Iterators.elementsEqual(tableDataIterator, dataIterator)); + } + @Test public void testCopyFromString() throws Exception { expect(bigquery.create(COPY_JOB_INFO)).andReturn(COPY_JOB_INFO);