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 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());
  }
}
Beispiel #3
0
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());
  }
}