Skip to content

Commit

Permalink
docs(sample): Table exists sample fix (#1868)
Browse files Browse the repository at this point in the history
Added `null` check in the sample and modified the test case to test for "Table not found"
  • Loading branch information
prash-mi committed Feb 28, 2022
1 parent bd6dd2a commit 698306e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Expand Up @@ -40,7 +40,10 @@ public static void tableExists(String datasetName, String tableName) {
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

Table table = bigquery.getTable(TableId.of(datasetName, tableName));
if (table.exists()) {
if (table != null
&& table
.exists()) { // table will be null if it is not found and setThrowNotFound is not set
// to `true`
System.out.println("Table already exist");
} else {
System.out.println("Table not found");
Expand Down
Expand Up @@ -61,7 +61,6 @@ public void setUp() {
System.setOut(out);
// create a temporary table
tableName = "MY_TABLE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of());
}

@After
Expand All @@ -76,6 +75,9 @@ public void tearDown() {

@Test
public void testTableExists() {
TableExists.tableExists(BIGQUERY_DATASET_NAME, tableName);
assertThat(bout.toString()).contains("Table not found");
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of());
TableExists.tableExists(BIGQUERY_DATASET_NAME, tableName);
assertThat(bout.toString()).contains("Table already exist");
}
Expand Down

0 comments on commit 698306e

Please sign in to comment.