Skip to content

Commit 698306e

Browse files
authored
docs(sample): Table exists sample fix (#1868)
Added `null` check in the sample and modified the test case to test for "Table not found"
1 parent bd6dd2a commit 698306e

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

samples/snippets/src/main/java/com/example/bigquery/TableExists.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public static void tableExists(String datasetName, String tableName) {
4040
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
4141

4242
Table table = bigquery.getTable(TableId.of(datasetName, tableName));
43-
if (table.exists()) {
43+
if (table != null
44+
&& table
45+
.exists()) { // table will be null if it is not found and setThrowNotFound is not set
46+
// to `true`
4447
System.out.println("Table already exist");
4548
} else {
4649
System.out.println("Table not found");

samples/snippets/src/test/java/com/example/bigquery/TableExistsIT.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public void setUp() {
6161
System.setOut(out);
6262
// create a temporary table
6363
tableName = "MY_TABLE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
64-
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of());
6564
}
6665

6766
@After
@@ -76,6 +75,9 @@ public void tearDown() {
7675

7776
@Test
7877
public void testTableExists() {
78+
TableExists.tableExists(BIGQUERY_DATASET_NAME, tableName);
79+
assertThat(bout.toString()).contains("Table not found");
80+
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of());
7981
TableExists.tableExists(BIGQUERY_DATASET_NAME, tableName);
8082
assertThat(bout.toString()).contains("Table already exist");
8183
}

0 commit comments

Comments
 (0)