Repository navigation
Expand file tree
/
Copy pathLoadJobConfigurationTest.java
More file actions
163 lines (148 loc) · 7.63 KB
/
Copy pathLoadJobConfigurationTest.java
File metadata and controls
163 lines (148 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigquery;
import static org.junit.Assert.assertEquals;
import com.google.cloud.bigquery.JobInfo.CreateDisposition;
import com.google.cloud.bigquery.JobInfo.WriteDisposition;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class LoadJobConfigurationTest {
private static final CsvOptions CSV_OPTIONS = CsvOptions.newBuilder()
.setAllowJaggedRows(true)
.setAllowQuotedNewLines(false)
.setEncoding(StandardCharsets.UTF_8)
.build();
private static final TableId TABLE_ID = TableId.of("dataset", "table");
private static final CreateDisposition CREATE_DISPOSITION = CreateDisposition.CREATE_IF_NEEDED;
private static final WriteDisposition WRITE_DISPOSITION = WriteDisposition.WRITE_APPEND;
private static final Integer MAX_BAD_RECORDS = 42;
private static final String FORMAT = "CSV";
private static final Boolean IGNORE_UNKNOWN_VALUES = true;
private static final List<String> PROJECTION_FIELDS = ImmutableList.of("field1", "field2");
private static final Field FIELD_SCHEMA = Field.newBuilder("IntegerField", Field.Type.integer())
.setMode(Field.Mode.REQUIRED)
.setDescription("FieldDescription")
.build();
private static final List<String> SOURCE_URIS = ImmutableList.of("uri1", "uri2");
private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA);
private static final LoadJobConfiguration LOAD_CONFIGURATION =
LoadJobConfiguration.newBuilder(TABLE_ID, SOURCE_URIS)
.setCreateDisposition(CREATE_DISPOSITION)
.setWriteDisposition(WRITE_DISPOSITION)
.setFormatOptions(CSV_OPTIONS)
.setIgnoreUnknownValues(IGNORE_UNKNOWN_VALUES)
.setMaxBadRecords(MAX_BAD_RECORDS)
.setProjectionFields(PROJECTION_FIELDS)
.setSchema(TABLE_SCHEMA)
.build();
private static final LoadJobConfiguration DEPRECATED_LOAD_CONFIGURATION =
LoadJobConfiguration.builder(TABLE_ID, SOURCE_URIS)
.createDisposition(CREATE_DISPOSITION)
.writeDisposition(WRITE_DISPOSITION)
.formatOptions(CSV_OPTIONS)
.ignoreUnknownValues(IGNORE_UNKNOWN_VALUES)
.maxBadRecords(MAX_BAD_RECORDS)
.projectionFields(PROJECTION_FIELDS)
.schema(TABLE_SCHEMA)
.build();
@Test
public void testToBuilder() {
compareLoadJobConfiguration(LOAD_CONFIGURATION, LOAD_CONFIGURATION.toBuilder().build());
LoadJobConfiguration configuration = LOAD_CONFIGURATION.toBuilder()
.setDestinationTable(TableId.of("dataset", "newTable"))
.build();
assertEquals("newTable", configuration.getDestinationTable().getTable());
configuration = configuration.toBuilder().setDestinationTable(TABLE_ID).build();
compareLoadJobConfiguration(LOAD_CONFIGURATION, configuration);
}
@Test
public void testOf() {
LoadJobConfiguration configuration = LoadJobConfiguration.of(TABLE_ID, SOURCE_URIS);
assertEquals(TABLE_ID, configuration.getDestinationTable());
assertEquals(SOURCE_URIS, configuration.getSourceUris());
configuration = LoadJobConfiguration.of(TABLE_ID, SOURCE_URIS, CSV_OPTIONS);
assertEquals(TABLE_ID, configuration.getDestinationTable());
assertEquals(FORMAT, configuration.getFormat());
assertEquals(CSV_OPTIONS, configuration.getCsvOptions());
assertEquals(SOURCE_URIS, configuration.getSourceUris());
configuration = LoadJobConfiguration.of(TABLE_ID, "uri1");
assertEquals(TABLE_ID, configuration.getDestinationTable());
assertEquals(ImmutableList.of("uri1"), configuration.getSourceUris());
configuration = LoadJobConfiguration.of(TABLE_ID, "uri1", CSV_OPTIONS);
assertEquals(TABLE_ID, configuration.getDestinationTable());
assertEquals(FORMAT, configuration.getFormat());
assertEquals(CSV_OPTIONS, configuration.getCsvOptions());
assertEquals(ImmutableList.of("uri1"), configuration.getSourceUris());
}
@Test
public void testToBuilderIncomplete() {
LoadJobConfiguration configuration = LoadJobConfiguration.of(TABLE_ID, SOURCE_URIS);
compareLoadJobConfiguration(configuration, configuration.toBuilder().build());
}
@Test
public void testBuilder() {
assertEquals(TABLE_ID, DEPRECATED_LOAD_CONFIGURATION.destinationTable());
assertEquals(CREATE_DISPOSITION, DEPRECATED_LOAD_CONFIGURATION.createDisposition());
assertEquals(WRITE_DISPOSITION, DEPRECATED_LOAD_CONFIGURATION.writeDisposition());
assertEquals(CSV_OPTIONS, DEPRECATED_LOAD_CONFIGURATION.csvOptions());
assertEquals(FORMAT, DEPRECATED_LOAD_CONFIGURATION.format());
assertEquals(IGNORE_UNKNOWN_VALUES, DEPRECATED_LOAD_CONFIGURATION.ignoreUnknownValues());
assertEquals(MAX_BAD_RECORDS, DEPRECATED_LOAD_CONFIGURATION.maxBadRecords());
assertEquals(PROJECTION_FIELDS, DEPRECATED_LOAD_CONFIGURATION.projectionFields());
assertEquals(TABLE_SCHEMA, DEPRECATED_LOAD_CONFIGURATION.schema());
}
@Test
public void testBuilderDeprecated() {
assertEquals(TABLE_ID, LOAD_CONFIGURATION.getDestinationTable());
assertEquals(CREATE_DISPOSITION, LOAD_CONFIGURATION.getCreateDisposition());
assertEquals(WRITE_DISPOSITION, LOAD_CONFIGURATION.getWriteDisposition());
assertEquals(CSV_OPTIONS, LOAD_CONFIGURATION.getCsvOptions());
assertEquals(FORMAT, LOAD_CONFIGURATION.getFormat());
assertEquals(IGNORE_UNKNOWN_VALUES, LOAD_CONFIGURATION.ignoreUnknownValues());
assertEquals(MAX_BAD_RECORDS, LOAD_CONFIGURATION.getMaxBadRecords());
assertEquals(PROJECTION_FIELDS, LOAD_CONFIGURATION.getProjectionFields());
assertEquals(TABLE_SCHEMA, LOAD_CONFIGURATION.getSchema());
}
@Test
public void testToPbAndFromPb() {
compareLoadJobConfiguration(LOAD_CONFIGURATION,
LoadJobConfiguration.fromPb(LOAD_CONFIGURATION.toPb()));
LoadJobConfiguration configuration = LoadJobConfiguration.of(TABLE_ID, SOURCE_URIS);
compareLoadJobConfiguration(configuration, LoadJobConfiguration.fromPb(configuration.toPb()));
}
@Test
public void testSetProjectId() {
LoadConfiguration configuration = LOAD_CONFIGURATION.setProjectId("p");
assertEquals("p", configuration.getDestinationTable().getProject());
}
private void compareLoadJobConfiguration(LoadJobConfiguration expected,
LoadJobConfiguration value) {
assertEquals(expected, value);
assertEquals(expected.hashCode(), value.hashCode());
assertEquals(expected.toString(), value.toString());
assertEquals(expected.getDestinationTable(), value.getDestinationTable());
assertEquals(expected.getCreateDisposition(), value.getCreateDisposition());
assertEquals(expected.getWriteDisposition(), value.getWriteDisposition());
assertEquals(expected.getCsvOptions(), value.getCsvOptions());
assertEquals(expected.getFormat(), value.getFormat());
assertEquals(expected.ignoreUnknownValues(), value.ignoreUnknownValues());
assertEquals(expected.getMaxBadRecords(), value.getMaxBadRecords());
assertEquals(expected.getProjectionFields(), value.getProjectionFields());
assertEquals(expected.getSchema(), value.getSchema());
}
}