/** * Insert rows into the table. * * @param rows rows to be inserted * @param skipInvalidRows whether to insert all valid rows, even if invalid rows exist. If not set * the entire insert operation will fail if rows to be inserted contain an invalid row * @param ignoreUnknownValues whether to accept rows that contain values that do not match the * schema. The unknown values are ignored. If not set, rows with unknown values are considered * to be invalid * @throws BigQueryException upon failure */ InsertAllResponse insert( Iterable<InsertAllRequest.RowToInsert> rows, boolean skipInvalidRows, boolean ignoreUnknownValues) throws BigQueryException { InsertAllRequest request = InsertAllRequest.builder(info.tableId(), rows) .skipInvalidRows(skipInvalidRows) .ignoreUnknownValues(ignoreUnknownValues) .build(); return bigquery.insertAll(request); }
public class InsertAllRequestTest { private static final Map<String, Object> CONTENT1 = ImmutableMap.<String, Object>of("key", "val1"); private static final Map<String, Object> CONTENT2 = ImmutableMap.<String, Object>of("key", "val2"); private static final List<InsertAllRequest.RowToInsert> ROWS = ImmutableList.of( InsertAllRequest.RowToInsert.of(CONTENT1), InsertAllRequest.RowToInsert.of(CONTENT2)); private static final List<InsertAllRequest.RowToInsert> ROWS_WITH_ID = ImmutableList.of( InsertAllRequest.RowToInsert.of("id1", CONTENT1), InsertAllRequest.RowToInsert.of("id2", CONTENT2)); private static final TableId TABLE_ID = TableId.of("dataset", "table"); private static final Schema TABLE_SCHEMA = Schema.of(); private static final BaseTableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_SCHEMA); private static final boolean SKIP_INVALID_ROWS = true; private static final boolean IGNORE_UNKNOWN_VALUES = false; private static final String TEMPLATE_SUFFIX = "templateSuffix"; private static final InsertAllRequest INSERT_ALL_REQUEST1 = InsertAllRequest.builder(TABLE_ID) .addRow(CONTENT1) .addRow(CONTENT2) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) .skipInvalidRows(SKIP_INVALID_ROWS) .build(); private static final InsertAllRequest INSERT_ALL_REQUEST2 = InsertAllRequest.builder(TABLE_ID) .rows(ROWS) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) .skipInvalidRows(SKIP_INVALID_ROWS) .build(); private static final InsertAllRequest INSERT_ALL_REQUEST3 = InsertAllRequest.builder(TABLE_ID.dataset(), TABLE_ID.table()) .rows(ROWS_WITH_ID) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) .skipInvalidRows(SKIP_INVALID_ROWS) .build(); private static final InsertAllRequest INSERT_ALL_REQUEST4 = InsertAllRequest.builder(TABLE_ID, ROWS) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) .skipInvalidRows(SKIP_INVALID_ROWS) .build(); private static final InsertAllRequest INSERT_ALL_REQUEST5 = InsertAllRequest.builder(TABLE_ID.dataset(), TABLE_ID.table(), ROWS_WITH_ID) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) .skipInvalidRows(SKIP_INVALID_ROWS) .build(); private static final InsertAllRequest INSERT_ALL_REQUEST6 = InsertAllRequest.builder(TABLE_ID, ROWS.get(0), ROWS.get(1)) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) .skipInvalidRows(SKIP_INVALID_ROWS) .build(); private static final InsertAllRequest INSERT_ALL_REQUEST7 = InsertAllRequest.builder( TABLE_ID.dataset(), TABLE_ID.table(), ROWS_WITH_ID.get(0), ROWS_WITH_ID.get(1)) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) .skipInvalidRows(SKIP_INVALID_ROWS) .build(); private static final InsertAllRequest INSERT_ALL_REQUEST8 = InsertAllRequest.builder(TABLE_ID.dataset(), TABLE_ID.table()) .addRow("id1", CONTENT1) .addRow("id2", CONTENT2) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) .skipInvalidRows(SKIP_INVALID_ROWS) .build(); private static final InsertAllRequest INSERT_ALL_REQUEST9 = InsertAllRequest.builder(TABLE_INFO) .addRow("id1", CONTENT1) .addRow("id2", CONTENT2) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) .skipInvalidRows(SKIP_INVALID_ROWS) .build(); private static final InsertAllRequest INSERT_ALL_REQUEST10 = InsertAllRequest.builder(TABLE_INFO) .addRow("id1", CONTENT1) .addRow("id2", CONTENT2) .ignoreUnknownValues(true) .skipInvalidRows(false) .build(); private static final InsertAllRequest INSERT_ALL_REQUEST11 = InsertAllRequest.builder(TABLE_INFO) .addRow("id1", CONTENT1) .addRow("id2", CONTENT2) .ignoreUnknownValues(true) .skipInvalidRows(false) .templateSuffix(TEMPLATE_SUFFIX) .build(); @Test public void testBuilder() { assertEquals(TABLE_ID, INSERT_ALL_REQUEST1.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST2.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST3.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST4.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST5.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST6.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST7.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST8.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST9.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST10.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST11.table()); assertEquals(ROWS, INSERT_ALL_REQUEST1.rows()); assertEquals(ROWS, INSERT_ALL_REQUEST2.rows()); assertEquals(ROWS, INSERT_ALL_REQUEST4.rows()); assertEquals(ROWS, INSERT_ALL_REQUEST6.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST3.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST5.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST7.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST8.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST9.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST10.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST11.rows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST1.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST2.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST3.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST4.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST5.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST6.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST7.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST8.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST9.skipInvalidRows()); assertFalse(INSERT_ALL_REQUEST10.skipInvalidRows()); assertFalse(INSERT_ALL_REQUEST11.skipInvalidRows()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST1.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST2.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST3.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST4.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST5.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST6.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST7.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST8.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST9.ignoreUnknownValues()); assertTrue(INSERT_ALL_REQUEST10.ignoreUnknownValues()); assertTrue(INSERT_ALL_REQUEST11.ignoreUnknownValues()); assertNull(INSERT_ALL_REQUEST1.templateSuffix()); assertNull(INSERT_ALL_REQUEST2.templateSuffix()); assertNull(INSERT_ALL_REQUEST3.templateSuffix()); assertNull(INSERT_ALL_REQUEST4.templateSuffix()); assertNull(INSERT_ALL_REQUEST5.templateSuffix()); assertNull(INSERT_ALL_REQUEST6.templateSuffix()); assertNull(INSERT_ALL_REQUEST7.templateSuffix()); assertNull(INSERT_ALL_REQUEST8.templateSuffix()); assertNull(INSERT_ALL_REQUEST9.templateSuffix()); assertNull(INSERT_ALL_REQUEST10.templateSuffix()); assertEquals(TEMPLATE_SUFFIX, INSERT_ALL_REQUEST11.templateSuffix()); } @Test public void testOf() { InsertAllRequest request = InsertAllRequest.of(TABLE_ID, ROWS); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); request = InsertAllRequest.of(TABLE_INFO, ROWS); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); request = InsertAllRequest.of(TABLE_ID.dataset(), TABLE_ID.table(), ROWS); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); request = InsertAllRequest.of(TABLE_ID.dataset(), TABLE_ID.table(), ROWS); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); request = InsertAllRequest.of(TABLE_ID, ROWS.get(0), ROWS.get(1)); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); request = InsertAllRequest.of(TABLE_INFO, ROWS.get(0), ROWS.get(1)); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); request = InsertAllRequest.of(TABLE_ID.dataset(), TABLE_ID.table(), ROWS.get(0), ROWS.get(1)); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); } @Test public void testEquals() { compareInsertAllRequest(INSERT_ALL_REQUEST1, INSERT_ALL_REQUEST2); compareInsertAllRequest(INSERT_ALL_REQUEST2, INSERT_ALL_REQUEST4); compareInsertAllRequest(INSERT_ALL_REQUEST3, INSERT_ALL_REQUEST5); compareInsertAllRequest(INSERT_ALL_REQUEST4, INSERT_ALL_REQUEST6); compareInsertAllRequest(INSERT_ALL_REQUEST5, INSERT_ALL_REQUEST7); compareInsertAllRequest(INSERT_ALL_REQUEST7, INSERT_ALL_REQUEST8); compareInsertAllRequest(INSERT_ALL_REQUEST8, INSERT_ALL_REQUEST9); compareInsertAllRequest(INSERT_ALL_REQUEST10, INSERT_ALL_REQUEST10); compareInsertAllRequest(INSERT_ALL_REQUEST11, INSERT_ALL_REQUEST11); } private void compareInsertAllRequest(InsertAllRequest expected, InsertAllRequest value) { assertEquals(expected, value); assertEquals(expected.toString(), value.toString()); assertEquals(expected.hashCode(), value.hashCode()); assertEquals(expected.table(), value.table()); assertEquals(expected.rows(), value.rows()); assertEquals(expected.ignoreUnknownValues(), value.ignoreUnknownValues()); assertEquals(expected.skipInvalidRows(), value.skipInvalidRows()); assertEquals(expected.templateSuffix(), value.templateSuffix()); } }
private void compareInsertAllRequest(InsertAllRequest expected, InsertAllRequest value) { assertEquals(expected, value); assertEquals(expected.toString(), value.toString()); assertEquals(expected.hashCode(), value.hashCode()); assertEquals(expected.table(), value.table()); assertEquals(expected.rows(), value.rows()); assertEquals(expected.ignoreUnknownValues(), value.ignoreUnknownValues()); assertEquals(expected.skipInvalidRows(), value.skipInvalidRows()); assertEquals(expected.templateSuffix(), value.templateSuffix()); }
@Test public void testOf() { InsertAllRequest request = InsertAllRequest.of(TABLE_ID, ROWS); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); request = InsertAllRequest.of(TABLE_INFO, ROWS); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); request = InsertAllRequest.of(TABLE_ID.dataset(), TABLE_ID.table(), ROWS); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); request = InsertAllRequest.of(TABLE_ID.dataset(), TABLE_ID.table(), ROWS); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); request = InsertAllRequest.of(TABLE_ID, ROWS.get(0), ROWS.get(1)); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); request = InsertAllRequest.of(TABLE_INFO, ROWS.get(0), ROWS.get(1)); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); request = InsertAllRequest.of(TABLE_ID.dataset(), TABLE_ID.table(), ROWS.get(0), ROWS.get(1)); assertEquals(TABLE_ID, request.table()); assertEquals(ROWS, request.rows()); }
@Test public void testBuilder() { assertEquals(TABLE_ID, INSERT_ALL_REQUEST1.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST2.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST3.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST4.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST5.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST6.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST7.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST8.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST9.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST10.table()); assertEquals(TABLE_ID, INSERT_ALL_REQUEST11.table()); assertEquals(ROWS, INSERT_ALL_REQUEST1.rows()); assertEquals(ROWS, INSERT_ALL_REQUEST2.rows()); assertEquals(ROWS, INSERT_ALL_REQUEST4.rows()); assertEquals(ROWS, INSERT_ALL_REQUEST6.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST3.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST5.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST7.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST8.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST9.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST10.rows()); assertEquals(ROWS_WITH_ID, INSERT_ALL_REQUEST11.rows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST1.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST2.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST3.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST4.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST5.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST6.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST7.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST8.skipInvalidRows()); assertEquals(SKIP_INVALID_ROWS, INSERT_ALL_REQUEST9.skipInvalidRows()); assertFalse(INSERT_ALL_REQUEST10.skipInvalidRows()); assertFalse(INSERT_ALL_REQUEST11.skipInvalidRows()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST1.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST2.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST3.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST4.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST5.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST6.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST7.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST8.ignoreUnknownValues()); assertEquals(IGNORE_UNKNOWN_VALUES, INSERT_ALL_REQUEST9.ignoreUnknownValues()); assertTrue(INSERT_ALL_REQUEST10.ignoreUnknownValues()); assertTrue(INSERT_ALL_REQUEST11.ignoreUnknownValues()); assertNull(INSERT_ALL_REQUEST1.templateSuffix()); assertNull(INSERT_ALL_REQUEST2.templateSuffix()); assertNull(INSERT_ALL_REQUEST3.templateSuffix()); assertNull(INSERT_ALL_REQUEST4.templateSuffix()); assertNull(INSERT_ALL_REQUEST5.templateSuffix()); assertNull(INSERT_ALL_REQUEST6.templateSuffix()); assertNull(INSERT_ALL_REQUEST7.templateSuffix()); assertNull(INSERT_ALL_REQUEST8.templateSuffix()); assertNull(INSERT_ALL_REQUEST9.templateSuffix()); assertNull(INSERT_ALL_REQUEST10.templateSuffix()); assertEquals(TEMPLATE_SUFFIX, INSERT_ALL_REQUEST11.templateSuffix()); }
/** * Insert rows into the table. * * @param rows rows to be inserted * @throws BigQueryException upon failure */ InsertAllResponse insert(Iterable<InsertAllRequest.RowToInsert> rows) throws BigQueryException { return bigquery.insertAll(InsertAllRequest.of(info.tableId(), rows)); }
public class TableTest { private static final TableId TABLE_ID1 = TableId.of("dataset", "table1"); private static final TableId TABLE_ID2 = TableId.of("dataset", "table2"); private static final JobInfo COPY_JOB_INFO = CopyJobInfo.of(TABLE_ID2, TABLE_ID1); private static final JobInfo LOAD_JOB_INFO = LoadJobInfo.builder( LoadConfiguration.builder(TABLE_ID1).formatOptions(FormatOptions.json()).build(), ImmutableList.of("URI")) .build(); private static final JobInfo EXTRACT_JOB_INFO = ExtractJobInfo.builder(TABLE_ID1, ImmutableList.of("URI")).format("CSV").build(); private static final Field FIELD = Field.of("FieldName", Field.Type.integer()); private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID1, Schema.of(FIELD)); private static final List<RowToInsert> ROWS_TO_INSERT = ImmutableList.of( RowToInsert.of("id1", ImmutableMap.<String, Object>of("key", "val1")), RowToInsert.of("id2", ImmutableMap.<String, Object>of("key", "val2"))); private static final InsertAllRequest INSERT_ALL_REQUEST = InsertAllRequest.of(TABLE_ID1, ROWS_TO_INSERT); private static final InsertAllRequest INSERT_ALL_REQUEST_COMPLETE = InsertAllRequest.builder(TABLE_ID1, ROWS_TO_INSERT) .skipInvalidRows(true) .ignoreUnknownValues(true) .build(); private static final InsertAllResponse EMPTY_INSERT_ALL_RESPONSE = new InsertAllResponse(ImmutableMap.<Long, List<BigQueryError>>of()); private static final FieldValue FIELD_VALUE1 = new FieldValue(FieldValue.Attribute.PRIMITIVE, "val1"); private static final FieldValue FIELD_VALUE2 = new FieldValue(FieldValue.Attribute.PRIMITIVE, "val1"); private static final Iterable<List<FieldValue>> ROWS = ImmutableList.of( (List<FieldValue>) ImmutableList.of(FIELD_VALUE1), ImmutableList.of(FIELD_VALUE2)); @Rule public ExpectedException thrown = ExpectedException.none(); private BigQuery bigquery; private Table table; @Before public void setUp() throws Exception { bigquery = createStrictMock(BigQuery.class); table = new Table(bigquery, TABLE_INFO); } @After public void tearDown() throws Exception { verify(bigquery); } @Test public void testInfo() throws Exception { assertEquals(TABLE_INFO, table.info()); 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()}; expect(bigquery.getTable(TABLE_INFO.tableId(), expectedOptions)).andReturn(TABLE_INFO); replay(bigquery); assertTrue(table.exists()); } @Test public void testExists_False() throws Exception { BigQuery.TableOption[] expectedOptions = {BigQuery.TableOption.fields()}; expect(bigquery.getTable(TABLE_INFO.tableId(), expectedOptions)).andReturn(null); replay(bigquery); assertFalse(table.exists()); } @Test public void testReload() throws Exception { TableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build(); expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(updatedInfo); replay(bigquery); Table updatedTable = table.reload(); assertSame(bigquery, updatedTable.bigquery()); assertEquals(updatedInfo, updatedTable.info()); } @Test public void testReloadNull() throws Exception { expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null); replay(bigquery); assertNull(table.reload()); } @Test public void testReloadWithOptions() throws Exception { TableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build(); expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields())) .andReturn(updatedInfo); replay(bigquery); Table updatedTable = table.reload(BigQuery.TableOption.fields()); assertSame(bigquery, updatedTable.bigquery()); 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); replay(bigquery); assertTrue(table.delete()); } @Test public void testInsert() throws Exception { expect(bigquery.insertAll(INSERT_ALL_REQUEST)).andReturn(EMPTY_INSERT_ALL_RESPONSE); replay(bigquery); InsertAllResponse response = table.insert(ROWS_TO_INSERT); assertSame(EMPTY_INSERT_ALL_RESPONSE, response); } @Test public void testInsertComplete() throws Exception { expect(bigquery.insertAll(INSERT_ALL_REQUEST_COMPLETE)).andReturn(EMPTY_INSERT_ALL_RESPONSE); replay(bigquery); InsertAllResponse response = table.insert(ROWS_TO_INSERT, true, true); assertSame(EMPTY_INSERT_ALL_RESPONSE, response); } @Test public void testList() throws Exception { PageImpl<List<FieldValue>> tableDataPage = new PageImpl<>(null, "c", ROWS); expect(bigquery.listTableData(TABLE_ID1)).andReturn(tableDataPage); replay(bigquery); Page<List<FieldValue>> dataPage = table.list(); Iterator<List<FieldValue>> tableDataIterator = tableDataPage.values().iterator(); Iterator<List<FieldValue>> dataIterator = dataPage.values().iterator(); assertTrue(Iterators.elementsEqual(tableDataIterator, dataIterator)); } @Test public void testListWithOptions() throws Exception { PageImpl<List<FieldValue>> tableDataPage = new PageImpl<>(null, "c", ROWS); expect(bigquery.listTableData(TABLE_ID1, BigQuery.TableDataListOption.maxResults(10L))) .andReturn(tableDataPage); replay(bigquery); Page<List<FieldValue>> dataPage = table.list(BigQuery.TableDataListOption.maxResults(10L)); Iterator<List<FieldValue>> tableDataIterator = tableDataPage.values().iterator(); Iterator<List<FieldValue>> 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); replay(bigquery); Job job = table.copy(TABLE_ID2.dataset(), TABLE_ID2.table()); assertSame(bigquery, job.bigquery()); assertEquals(COPY_JOB_INFO, job.info()); } @Test public void testCopyFromId() throws Exception { expect(bigquery.create(COPY_JOB_INFO)).andReturn(COPY_JOB_INFO); replay(bigquery); Job job = table.copy(TABLE_ID2); assertSame(bigquery, job.bigquery()); assertEquals(COPY_JOB_INFO, job.info()); } @Test public void testLoadDataUri() throws Exception { expect(bigquery.create(LOAD_JOB_INFO)).andReturn(LOAD_JOB_INFO); replay(bigquery); Job job = table.load(FormatOptions.json(), "URI"); assertSame(bigquery, job.bigquery()); assertEquals(LOAD_JOB_INFO, job.info()); } @Test public void testLoadDataUris() throws Exception { expect(bigquery.create(LOAD_JOB_INFO)).andReturn(LOAD_JOB_INFO); replay(bigquery); Job job = table.load(FormatOptions.json(), ImmutableList.of("URI")); assertSame(bigquery, job.bigquery()); assertEquals(LOAD_JOB_INFO, job.info()); } @Test public void testExtractDataUri() throws Exception { expect(bigquery.create(EXTRACT_JOB_INFO)).andReturn(EXTRACT_JOB_INFO); replay(bigquery); Job job = table.extract("CSV", "URI"); assertSame(bigquery, job.bigquery()); assertEquals(EXTRACT_JOB_INFO, job.info()); } @Test public void testExtractDataUris() throws Exception { expect(bigquery.create(EXTRACT_JOB_INFO)).andReturn(EXTRACT_JOB_INFO); replay(bigquery); Job job = table.extract("CSV", ImmutableList.of("URI")); assertSame(bigquery, job.bigquery()); assertEquals(EXTRACT_JOB_INFO, job.info()); } @Test public void testGetFromId() throws Exception { expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(TABLE_INFO); replay(bigquery); Table loadedTable = Table.get(bigquery, TABLE_INFO.tableId()); assertNotNull(loadedTable); assertEquals(TABLE_INFO, loadedTable.info()); } @Test public void testGetFromStrings() throws Exception { expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(TABLE_INFO); replay(bigquery); Table loadedTable = Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table()); assertNotNull(loadedTable); assertEquals(TABLE_INFO, loadedTable.info()); } @Test public void testGetFromIdNull() throws Exception { expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null); replay(bigquery); assertNull(Table.get(bigquery, TABLE_INFO.tableId())); } @Test public void testGetFromStringsNull() throws Exception { expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null); replay(bigquery); assertNull(Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table())); } @Test public void testGetFromIdWithOptions() throws Exception { expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields())) .andReturn(TABLE_INFO); replay(bigquery); Table loadedTable = Table.get(bigquery, TABLE_INFO.tableId(), BigQuery.TableOption.fields()); assertNotNull(loadedTable); assertEquals(TABLE_INFO, loadedTable.info()); } @Test public void testGetFromStringsWithOptions() throws Exception { expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields())) .andReturn(TABLE_INFO); replay(bigquery); Table loadedTable = Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table(), BigQuery.TableOption.fields()); assertNotNull(loadedTable); assertEquals(TABLE_INFO, loadedTable.info()); } }