@Test
  public void testInsertAll() throws Exception, IOException {
    // Build up a list of indices to fail on each invocation. This should result in
    // 5 calls to insertAll.
    List<List<Long>> errorsIndices = new ArrayList<>();
    errorsIndices.add(Arrays.asList(0L, 5L, 10L, 15L, 20L));
    errorsIndices.add(Arrays.asList(0L, 2L, 4L));
    errorsIndices.add(Arrays.asList(0L, 2L));
    errorsIndices.add(new ArrayList<Long>());
    onInsertAll(errorsIndices);

    TableReference ref = BigQueryIO.parseTableSpec("project:dataset.table");
    BigQueryTableInserter inserter = new BigQueryTableInserter(mockClient, 5);

    List<TableRow> rows = new ArrayList<>();
    List<String> ids = new ArrayList<>();
    for (int i = 0; i < 25; ++i) {
      rows.add(new TableRow());
      ids.add(new String());
    }

    try {
      inserter.insertAll(ref, rows, ids);
    } finally {
      verifyInsertAll(5);
    }
  }
  @Test
  public void testWriteAppend() throws IOException {
    onTableGet(basicTableSchema());

    TableReference ref = BigQueryIO.parseTableSpec("project:dataset.table");

    BigQueryTableInserter inserter = new BigQueryTableInserter(mockClient);

    inserter.getOrCreateTable(
        ref,
        BigQueryIO.Write.WriteDisposition.WRITE_APPEND,
        BigQueryIO.Write.CreateDisposition.CREATE_NEVER,
        null);

    verifyTableGet();
  }
  @Test
  public void testWriteEmpty() throws IOException {
    onTableGet(basicTableSchema());

    TableDataList dataList = new TableDataList().setTotalRows(0L);
    onTableList(dataList);

    TableReference ref = BigQueryIO.parseTableSpec("project:dataset.table");

    BigQueryTableInserter inserter = new BigQueryTableInserter(mockClient);

    inserter.getOrCreateTable(
        ref,
        BigQueryIO.Write.WriteDisposition.WRITE_EMPTY,
        BigQueryIO.Write.CreateDisposition.CREATE_NEVER,
        null);

    verifyTableGet();
    verifyTabledataList();
  }
  @Test
  public void testWriteEmptyFail() throws IOException {
    thrown.expect(IOException.class);

    onTableGet(basicTableSchema());

    TableDataList dataList = rawDataList(rawRow("Arthur", 42));
    onTableList(dataList);

    TableReference ref = BigQueryIO.parseTableSpec("project:dataset.table");

    BigQueryTableInserter inserter = new BigQueryTableInserter(mockClient);

    try {
      inserter.getOrCreateTable(
          ref,
          BigQueryIO.Write.WriteDisposition.WRITE_EMPTY,
          BigQueryIO.Write.CreateDisposition.CREATE_NEVER,
          null);
    } finally {
      verifyTableGet();
      verifyTabledataList();
    }
  }