Skip to content

Commit 2647a4e

Browse files
committed
cleanup ITTableSnippets
1 parent 84904d8 commit 2647a4e

2 files changed

Lines changed: 53 additions & 79 deletions

File tree

google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/TableSnippets.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public TableSnippets(Table table) {
5858
public void checkExists() {
5959
// [START checkExists]
6060
if (!table.exists()) {
61-
throw new RuntimeException("Table does not exist.");
61+
throw new IllegalArgumentException("Table does not exist.");
6262
}
6363
// [END checkExists]
6464
}

google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITTableSnippets.java

Lines changed: 52 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
import static org.junit.Assert.assertFalse;
2121
import static org.junit.Assert.assertTrue;
2222

23-
import java.util.ArrayList;
24-
import java.util.HashMap;
2523
import java.util.List;
26-
import java.util.Map;
2724
import java.util.Set;
2825
import java.util.logging.Logger;
2926

@@ -43,7 +40,6 @@
4340
import com.google.cloud.bigquery.Field;
4441
import com.google.cloud.bigquery.Field.Type;
4542
import com.google.cloud.bigquery.FieldValue;
46-
import com.google.cloud.bigquery.InsertAllRequest.RowToInsert;
4743
import com.google.cloud.bigquery.InsertAllResponse;
4844
import com.google.cloud.bigquery.Schema;
4945
import com.google.cloud.bigquery.StandardTableDefinition;
@@ -78,7 +74,7 @@ public class ITTableSnippets {
7874
private static Table doomedTable;
7975
private static TableSnippets doomedTableSnippets;
8076

81-
private static int nextTableNumber = 0;
77+
private static int nextTableNumber;
8278

8379
@BeforeClass
8480
public static void beforeClass() {
@@ -169,7 +165,7 @@ public void testInsertParams() throws InterruptedException {
169165
InsertAllResponse response = tableSnippets.insertWithParams("row1", "row2");
170166
assertTrue(response.hasErrors());
171167
List<List<FieldValue>> rows = ImmutableList.copyOf(tableSnippets.list().values());
172-
while (rows.size() == 0) {
168+
while (rows.isEmpty()) {
173169
Thread.sleep(500);
174170
rows = ImmutableList.copyOf(tableSnippets.list().values());
175171
}
@@ -191,7 +187,7 @@ public void testList() throws InterruptedException {
191187
InsertAllResponse response = tableSnippets.insert("row1", "row2");
192188
assertFalse(response.hasErrors());
193189
rows = ImmutableList.copyOf(tableSnippets.list().values());
194-
while (rows.size() == 0) {
190+
while (rows.isEmpty()) {
195191
Thread.sleep(500);
196192
rows = ImmutableList.copyOf(tableSnippets.list().values());
197193
}
@@ -203,6 +199,55 @@ public void testCopy() {
203199
tableSnippets.copy(COPY_DATASET_NAME, BASE_TABLE_NAME);
204200
}
205201

202+
@Test
203+
public void testCopyTableId() {
204+
log.info("testCopyTableId");
205+
tableSnippets.copyTableId(COPY_DATASET_NAME, getCopyTableName());
206+
}
207+
208+
@Test
209+
public void testExtractList() {
210+
log.info("testExtractList");
211+
String projectId = bigquery.options().projectId();
212+
String gcsFile1 = "gs://" + projectId + ".appspot.com/extractTestA_*.csv";
213+
String gcsFile2 = "gs://" + projectId + ".appspot.com/extractTestB_*.csv";
214+
tableSnippets.extractList("CSV", gcsFile1, gcsFile2);
215+
}
216+
217+
@Test
218+
public void testExtractSingle() {
219+
log.info("testExtractSingle");
220+
String projectId = bigquery.options().projectId();
221+
String gcsFile = "gs://" + projectId + ".appspot.com/extractTest.csv";
222+
tableSnippets.extractSingle("CSV", gcsFile);
223+
}
224+
225+
@Test
226+
public void testLoadList() {
227+
log.info("testLoadList");
228+
String projectId = bigquery.options().projectId();
229+
String gcsFile1 = "gs://" + projectId + ".appspot.com/loadTest1.csv";
230+
String gcsFile2 = "gs://" + projectId + ".appspot.com/loadTest2.csv";
231+
232+
// Before we can load, we should make sure those files exist.
233+
tableSnippets.extractSingle("CSV", gcsFile1);
234+
tableSnippets.extractSingle("CSV", gcsFile2);
235+
236+
tableSnippets.loadList(gcsFile1, gcsFile2);
237+
}
238+
239+
@Test
240+
public void testLoadSingle() {
241+
log.info("testLoadSingle");
242+
String projectId = bigquery.options().projectId();
243+
String gcsFile = "gs://" + projectId + ".appspot.com/loadSingle.csv";
244+
245+
// Before we can load, we should make sure the file exists.
246+
tableSnippets.extractSingle("CSV", gcsFile);
247+
248+
tableSnippets.loadSingle(gcsFile);
249+
}
250+
206251
private static class Value {
207252
final String stringField;
208253
final boolean booleanField;
@@ -232,28 +277,6 @@ public String toString() {
232277
}
233278
}
234279

235-
/**
236-
* Inserts some data into the test table.
237-
*/
238-
private void insertTestRows() {
239-
String rowId1 = "row1";
240-
String rowId2 = "row2";
241-
List<RowToInsert> rows = new ArrayList<>();
242-
Map<String, Object> row1 = new HashMap<>();
243-
row1.put("stringField", ROW1.stringField);
244-
row1.put("booleanField", ROW1.booleanField);
245-
Map<String, Object> row2 = new HashMap<>();
246-
row2.put("stringField", ROW2.stringField);
247-
row2.put("booleanField", ROW2.booleanField);
248-
rows.add(RowToInsert.of(rowId1, row1));
249-
rows.add(RowToInsert.of(rowId2, row2));
250-
InsertAllResponse response = table.insert(rows);
251-
while (response.hasErrors()) {
252-
log.info("Error inserting rows. Trying again...");
253-
response = table.insert(rows);
254-
}
255-
}
256-
257280
/**
258281
* Verifies that the given table has the rows inserted by InsertTestRows().
259282
*
@@ -299,53 +322,4 @@ private List<List<FieldValue>> waitForTableRows(Table checkTable, int numRows) {
299322
}
300323
return rows;
301324
}
302-
303-
@Test
304-
public void testCopyTableId() {
305-
log.info("testCopyTableId");
306-
tableSnippets.copyTableId(COPY_DATASET_NAME, getCopyTableName());
307-
}
308-
309-
@Test
310-
public void testExtractList() {
311-
log.info("testExtractList");
312-
String projectId = bigquery.options().projectId();
313-
String gcsFile1 = "gs://" + projectId + ".appspot.com/extractTestA_*.csv";
314-
String gcsFile2 = "gs://" + projectId + ".appspot.com/extractTestB_*.csv";
315-
tableSnippets.extractList("CSV", gcsFile1, gcsFile2);
316-
}
317-
318-
@Test
319-
public void testExtractSingle() {
320-
log.info("testExtractSingle");
321-
String projectId = bigquery.options().projectId();
322-
String gcsFile = "gs://" + projectId + ".appspot.com/extractTest.csv";
323-
tableSnippets.extractSingle("CSV", gcsFile);
324-
}
325-
326-
@Test
327-
public void testLoadList() {
328-
log.info("testLoadList");
329-
String projectId = bigquery.options().projectId();
330-
String gcsFile1 = "gs://" + projectId + ".appspot.com/loadTest1.csv";
331-
String gcsFile2 = "gs://" + projectId + ".appspot.com/loadTest2.csv";
332-
333-
// Before we can load, we should make sure those files exist.
334-
tableSnippets.extractSingle("CSV", gcsFile1);
335-
tableSnippets.extractSingle("CSV", gcsFile2);
336-
337-
tableSnippets.loadList(gcsFile1, gcsFile2);
338-
}
339-
340-
@Test
341-
public void testLoadSingle() {
342-
log.info("testLoadSingle");
343-
String projectId = bigquery.options().projectId();
344-
String gcsFile = "gs://" + projectId + ".appspot.com/loadSingle.csv";
345-
346-
// Before we can load, we should make sure the file exists.
347-
tableSnippets.extractSingle("CSV", gcsFile);
348-
349-
tableSnippets.loadSingle(gcsFile);
350-
}
351325
}

0 commit comments

Comments
 (0)