19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
import static junit .framework .TestCase .assertNotNull ;
21
21
22
+ import com .google .cloud .bigquery .Field ;
23
+ import com .google .cloud .bigquery .Schema ;
24
+ import com .google .cloud .bigquery .StandardSQLTypeName ;
22
25
import java .io .ByteArrayOutputStream ;
23
26
import java .io .PrintStream ;
24
27
import java .util .UUID ;
32
35
public class CopyMultipleTablesIT {
33
36
34
37
private final Logger log = Logger .getLogger (this .getClass ().getName ());
38
+ private String datasetName ;
35
39
private String tableName ;
40
+ private String sourceTable1Name ;
41
+ private String sourceTable2Name ;
36
42
private ByteArrayOutputStream bout ;
37
43
private PrintStream out ;
38
44
private PrintStream originalPrintStream ;
39
45
40
- private static final String BIGQUERY_DATASET_NAME = System . getenv ( "BIGQUERY_DATASET_NAME " );
46
+ private static final String PROJECT_ID = requireEnvVar ( "GOOGLE_CLOUD_PROJECT " );
41
47
42
- private static void requireEnvVar (String varName ) {
48
+ private static String requireEnvVar (String varName ) {
49
+ String value = System .getenv (varName );
43
50
assertNotNull (
44
51
"Environment variable " + varName + " is required to perform these tests." ,
45
52
System .getenv (varName ));
53
+ return value ;
46
54
}
47
55
48
56
@ BeforeClass
49
57
public static void checkRequirements () {
50
- requireEnvVar ("BIGQUERY_DATASET_NAME " );
58
+ requireEnvVar ("GOOGLE_CLOUD_PROJECT " );
51
59
}
52
60
53
61
@ Before
@@ -56,15 +64,30 @@ public void setUp() throws Exception {
56
64
out = new PrintStream (bout );
57
65
originalPrintStream = System .out ;
58
66
System .setOut (out );
67
+
59
68
// Create a new destination table for each test since existing table cannot be overwritten
69
+ datasetName = "MY_DATASET_NAME_TEST_" + UUID .randomUUID ().toString ().substring (0 , 8 );
60
70
tableName = "COPY_MULTIPLE_TABLE_TEST" + UUID .randomUUID ().toString ().substring (0 , 8 );
61
- CreateTable .createTable (BIGQUERY_DATASET_NAME , tableName , null );
71
+ sourceTable1Name =
72
+ "COPY_MULTIPLE_TABLE_SOURCE1_TEST" + UUID .randomUUID ().toString ().substring (0 , 8 );
73
+ sourceTable2Name =
74
+ "COPY_MULTIPLE_TABLE_SOURCE2_TEST" + UUID .randomUUID ().toString ().substring (0 , 8 );
75
+ CreateDataset .createDataset (datasetName );
76
+
77
+ Schema schema =
78
+ Schema .of (
79
+ Field .of ("timestampField" , StandardSQLTypeName .TIMESTAMP ),
80
+ Field .of ("stringField" , StandardSQLTypeName .STRING ),
81
+ Field .of ("booleanField" , StandardSQLTypeName .BOOL ));
82
+ CreateTable .createTable (datasetName , tableName , schema );
83
+ CreateTable .createTable (datasetName , sourceTable1Name , schema );
84
+ CreateTable .createTable (datasetName , sourceTable2Name , schema );
62
85
}
63
86
64
87
@ After
65
88
public void tearDown () {
66
89
// Clean up
67
- DeleteTable . deleteTable ( BIGQUERY_DATASET_NAME , tableName );
90
+ DeleteDataset . deleteDataset ( PROJECT_ID , datasetName );
68
91
// restores print statements in the original method
69
92
System .out .flush ();
70
93
System .setOut (originalPrintStream );
@@ -73,7 +96,8 @@ public void tearDown() {
73
96
74
97
@ Test
75
98
public void testCopyMultipleTables () {
76
- CopyMultipleTables .copyMultipleTables (BIGQUERY_DATASET_NAME , tableName );
99
+ CopyMultipleTables .copyMultipleTables (
100
+ datasetName , tableName , sourceTable1Name , sourceTable2Name );
77
101
assertThat (bout .toString ()).contains ("Table copied successfully." );
78
102
}
79
103
}
0 commit comments