Skip to content

Commit

Permalink
refactor(samples): add logs and remove redundant code (#716)
Browse files Browse the repository at this point in the history
* refactor(samples): add logs and remove redundant code

* refactor(samples): fix npe for tablename
  • Loading branch information
Praful Makani authored Sep 2, 2020
1 parent b75b95b commit 70417fc
Show file tree
Hide file tree
Showing 116 changed files with 1,230 additions and 489 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -34,10 +36,12 @@

public class AddColumnLoadAppendIT {

private final Logger log = Logger.getLogger(this.getClass().getName());
private String tableName;
private Schema schema;
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");

Expand All @@ -58,6 +62,7 @@ public static void checkRequirements() {
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);

// create a test table.
Expand All @@ -69,17 +74,16 @@ public void setUp() {
.build());

CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema);

bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
// Clean up
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
System.setOut(null);
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
log.log(Level.INFO, "\n" + bout.toString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class AddEmptyColumnIT {

private final Logger log = Logger.getLogger(this.getClass().getName());
private String tableName;
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");

Expand All @@ -48,28 +54,34 @@ public static void checkRequirements() {
}

@Before
public void setUp() throws Exception {
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
}

@Test
public void addEmptyColumn() {
String tableName = "AddEmptyColumnTestTable_" + UUID.randomUUID().toString().replace('-', '_');
tableName = "AddEmptyColumnTestTable_" + UUID.randomUUID().toString().replace('-', '_');
Schema schema =
Schema.of(
Field.of("booleanField", LegacySQLTypeName.BOOLEAN),
Field.of("numericField", LegacySQLTypeName.NUMERIC));

// Create table in dataset for testing
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema);
}

@After
public void tearDown() {
// clean up
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
log.log(Level.INFO, "\n" + bout.toString());
}

@Test
public void addEmptyColumn() {
String randomColumnName = "new_" + UUID.randomUUID().toString().replace('-', '_');
AddEmptyColumn.addEmptyColumn(randomColumnName, BIGQUERY_DATASET_NAME, tableName);
assertThat(bout.toString()).contains("Empty column successfully added to table");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,33 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class AuthDriveScopeIT {

private final Logger log = Logger.getLogger(this.getClass().getName());
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

@Before
public void setUp() throws Exception {
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
log.log(Level.INFO, "\n" + bout.toString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -30,19 +32,26 @@
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class AuthSnippetsIT {

private final Logger log = Logger.getLogger(this.getClass().getName());
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
log.log(Level.INFO, "\n" + bout.toString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class AuthorizedViewTutorialIT {

private final Logger log = Logger.getLogger(this.getClass().getName());
private String sourceDatasetId;
private String sourceTableId;
private String sharedDatasetId;
private String sharedViewId;
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");

Expand All @@ -60,6 +64,7 @@ public void setUp() {

bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}

Expand All @@ -68,7 +73,10 @@ public void tearDown() {
// Clean up
DeleteDataset.deleteDataset(PROJECT_ID, sourceDatasetId);
DeleteDataset.deleteDataset(PROJECT_ID, sharedDatasetId);
System.setOut(null);
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
log.log(Level.INFO, "\n" + bout.toString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class BrowseTableIT {

private final Logger log = Logger.getLogger(this.getClass().getName());
private String tableName;
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");

Expand All @@ -51,30 +57,31 @@ public static void checkRequirements() {
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
}

@Test
public void testBrowseTable() {
String tableName = "MY_TABLE_NAME_" + UUID.randomUUID().toString().replace("-", "_");

tableName = "MY_TABLE_NAME_" + UUID.randomUUID().toString().replace("-", "_");
Schema schema =
Schema.of(
Field.of("stringField", StandardSQLTypeName.STRING),
Field.of("booleanField", StandardSQLTypeName.BOOL));

CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema);
}

BrowseTable.browseTable(BIGQUERY_DATASET_NAME, tableName);

assertThat(bout.toString()).contains("Query ran successfully");

@After
public void tearDown() {
// Clean up
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
log.log(Level.INFO, "\n" + bout.toString());
}

@Test
public void testBrowseTable() {
BrowseTable.browseTable(BIGQUERY_DATASET_NAME, tableName);
assertThat(bout.toString()).contains("Query ran successfully");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,33 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CancelJobIT {

private final Logger log = Logger.getLogger(this.getClass().getName());
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
log.log(Level.INFO, "\n" + bout.toString());
}

@Test
Expand Down
Loading

0 comments on commit 70417fc

Please sign in to comment.