17
17
package com .example .bigquery ;
18
18
19
19
import static com .google .common .truth .Truth .assertThat ;
20
- import static junit .framework .TestCase .assertNotNull ;
21
20
22
21
import com .google .cloud .bigquery .Field ;
23
22
import com .google .cloud .bigquery .LegacySQLTypeName ;
26
25
import java .io .PrintStream ;
27
26
import java .util .ArrayList ;
28
27
import java .util .List ;
28
+ import java .util .UUID ;
29
29
import org .junit .After ;
30
+ import org .junit .Assert ;
30
31
import org .junit .Before ;
31
32
import org .junit .BeforeClass ;
32
33
import org .junit .Test ;
33
34
34
35
public class AddColumnLoadAppendIT {
36
+
37
+ private String tableName ;
38
+ private Schema schema ;
35
39
private ByteArrayOutputStream bout ;
36
40
private PrintStream out ;
37
41
38
- private static final String BIGQUERY_DATASET_NAME = System . getenv ("BIGQUERY_DATASET_NAME" );
42
+ private static final String BIGQUERY_DATASET_NAME = requireEnvVar ("BIGQUERY_DATASET_NAME" );
39
43
40
- private static void requireEnvVar (String varName ) {
41
- assertNotNull (
44
+ private static String requireEnvVar (String varName ) {
45
+ String value = System .getenv (varName );
46
+ Assert .assertNotNull (
42
47
"Environment variable " + varName + " is required to perform these tests." ,
43
48
System .getenv (varName ));
49
+ return value ;
44
50
}
45
51
46
52
@ BeforeClass
@@ -53,41 +59,41 @@ public void setUp() {
53
59
bout = new ByteArrayOutputStream ();
54
60
out = new PrintStream (bout );
55
61
System .setOut (out );
62
+
63
+ // create a test table.
64
+ tableName = "ADD_COLUMN_LOAD_APPEND_TEST_" + UUID .randomUUID ().toString ().substring (0 , 8 );
65
+ schema =
66
+ Schema .of (
67
+ Field .newBuilder ("name" , LegacySQLTypeName .STRING )
68
+ .setMode (Field .Mode .REQUIRED )
69
+ .build ());
70
+
71
+ CreateTable .createTable (BIGQUERY_DATASET_NAME , tableName , schema );
72
+
73
+ bout = new ByteArrayOutputStream ();
74
+ out = new PrintStream (bout );
75
+ System .setOut (out );
56
76
}
57
77
58
78
@ After
59
79
public void tearDown () {
80
+ // Clean up
81
+ DeleteTable .deleteTable (BIGQUERY_DATASET_NAME , tableName );
60
82
System .setOut (null );
61
83
}
62
84
63
85
@ Test
64
- public void testAddColumnLoadAppend () throws Exception {
86
+ public void testAddColumnLoadAppend () {
65
87
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv" ;
66
-
67
- String tableName = "ADD_COLUMN_LOAD_APPEND_TEST" ;
68
- Schema originalSchema =
69
- Schema .of (
70
- Field .newBuilder ("name" , LegacySQLTypeName .STRING )
71
- .setMode (Field .Mode .REQUIRED )
72
- .build ());
73
-
74
- CreateTable .createTable (BIGQUERY_DATASET_NAME , tableName , originalSchema );
75
-
76
- List <Field > fields = originalSchema .getFields ();
77
88
// Adding below additional column during the load job
78
89
Field newField =
79
90
Field .newBuilder ("post_abbr" , LegacySQLTypeName .STRING )
80
91
.setMode (Field .Mode .NULLABLE )
81
92
.build ();
82
- List <Field > newFields = new ArrayList <>(fields );
93
+ List <Field > newFields = new ArrayList <>(schema . getFields () );
83
94
newFields .add (newField );
84
- Schema newSchema = Schema .of (newFields );
85
-
86
- AddColumnLoadAppend .addColumnLoadAppend (BIGQUERY_DATASET_NAME , tableName , sourceUri , newSchema );
87
-
95
+ AddColumnLoadAppend .addColumnLoadAppend (
96
+ BIGQUERY_DATASET_NAME , tableName , sourceUri , Schema .of (newFields ));
88
97
assertThat (bout .toString ()).contains ("Column successfully added during load append job" );
89
-
90
- // Clean up
91
- DeleteTable .deleteTable (BIGQUERY_DATASET_NAME , tableName );
92
98
}
93
99
}
0 commit comments