public class WriteChannelConfigurationTest { private static final CsvOptions CSV_OPTIONS = CsvOptions.builder() .allowJaggedRows(true) .allowQuotedNewLines(false) .encoding(StandardCharsets.UTF_8) .build(); private static final TableId TABLE_ID = TableId.of("dataset", "table"); private static final CreateDisposition CREATE_DISPOSITION = CreateDisposition.CREATE_IF_NEEDED; private static final WriteDisposition WRITE_DISPOSITION = WriteDisposition.WRITE_APPEND; private static final Integer MAX_BAD_RECORDS = 42; private static final String FORMAT = "CSV"; private static final Boolean IGNORE_UNKNOWN_VALUES = true; private static final List<String> PROJECTION_FIELDS = ImmutableList.of("field1", "field2"); private static final Field FIELD_SCHEMA = Field.builder("IntegerField", Field.Type.integer()) .mode(Field.Mode.REQUIRED) .description("FieldDescription") .build(); private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA); private static final WriteChannelConfiguration LOAD_CONFIGURATION = WriteChannelConfiguration.builder(TABLE_ID) .createDisposition(CREATE_DISPOSITION) .writeDisposition(WRITE_DISPOSITION) .formatOptions(CSV_OPTIONS) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) .maxBadRecords(MAX_BAD_RECORDS) .projectionFields(PROJECTION_FIELDS) .schema(TABLE_SCHEMA) .build(); @Test public void testToBuilder() { compareLoadConfiguration(LOAD_CONFIGURATION, LOAD_CONFIGURATION.toBuilder().build()); WriteChannelConfiguration configuration = LOAD_CONFIGURATION.toBuilder().destinationTable(TableId.of("dataset", "newTable")).build(); assertEquals("newTable", configuration.destinationTable().table()); configuration = configuration.toBuilder().destinationTable(TABLE_ID).build(); compareLoadConfiguration(LOAD_CONFIGURATION, configuration); } @Test public void testOf() { WriteChannelConfiguration configuration = WriteChannelConfiguration.of(TABLE_ID); assertEquals(TABLE_ID, configuration.destinationTable()); configuration = WriteChannelConfiguration.of(TABLE_ID, CSV_OPTIONS); assertEquals(TABLE_ID, configuration.destinationTable()); assertEquals(FORMAT, configuration.format()); assertEquals(CSV_OPTIONS, configuration.csvOptions()); } @Test public void testToBuilderIncomplete() { WriteChannelConfiguration configuration = WriteChannelConfiguration.of(TABLE_ID); compareLoadConfiguration(configuration, configuration.toBuilder().build()); } @Test public void testBuilder() { assertEquals(TABLE_ID, LOAD_CONFIGURATION.destinationTable()); assertEquals(CREATE_DISPOSITION, LOAD_CONFIGURATION.createDisposition()); assertEquals(WRITE_DISPOSITION, LOAD_CONFIGURATION.writeDisposition()); assertEquals(CSV_OPTIONS, LOAD_CONFIGURATION.csvOptions()); assertEquals(FORMAT, LOAD_CONFIGURATION.format()); assertEquals(IGNORE_UNKNOWN_VALUES, LOAD_CONFIGURATION.ignoreUnknownValues()); assertEquals(MAX_BAD_RECORDS, LOAD_CONFIGURATION.maxBadRecords()); assertEquals(PROJECTION_FIELDS, LOAD_CONFIGURATION.projectionFields()); assertEquals(TABLE_SCHEMA, LOAD_CONFIGURATION.schema()); } @Test public void testToPbAndFromPb() { assertNull(LOAD_CONFIGURATION.toPb().getLoad().getSourceUris()); compareLoadConfiguration( LOAD_CONFIGURATION, WriteChannelConfiguration.fromPb(LOAD_CONFIGURATION.toPb())); WriteChannelConfiguration configuration = WriteChannelConfiguration.of(TABLE_ID); compareLoadConfiguration(configuration, WriteChannelConfiguration.fromPb(configuration.toPb())); } private void compareLoadConfiguration( WriteChannelConfiguration expected, WriteChannelConfiguration value) { assertEquals(expected, value); assertEquals(expected.hashCode(), value.hashCode()); assertEquals(expected.toString(), value.toString()); assertEquals(expected.destinationTable(), value.destinationTable()); assertEquals(expected.createDisposition(), value.createDisposition()); assertEquals(expected.writeDisposition(), value.writeDisposition()); assertEquals(expected.csvOptions(), value.csvOptions()); assertEquals(expected.format(), value.format()); assertEquals(expected.ignoreUnknownValues(), value.ignoreUnknownValues()); assertEquals(expected.maxBadRecords(), value.maxBadRecords()); assertEquals(expected.projectionFields(), value.projectionFields()); assertEquals(expected.schema(), value.schema()); } }
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()); } }