forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableTest.java
More file actions
346 lines (308 loc) · 12.5 KB
/
Copy pathTableTest.java
File metadata and controls
346 lines (308 loc) · 12.5 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Copyright 2015 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.gcloud.bigquery;
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterators;
import com.google.gcloud.Page;
import com.google.gcloud.PageImpl;
import com.google.gcloud.bigquery.InsertAllRequest.RowToInsert;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.Iterator;
import java.util.List;
public class TableTest {
private static final TableId TABLE_ID1 = TableId.of("dataset", "table1");
private static final TableId TABLE_ID2 = TableId.of("dataset", "table2");
private static final JobInfo COPY_JOB_INFO = CopyJobInfo.of(TABLE_ID2, TABLE_ID1);
private static final JobInfo LOAD_JOB_INFO =
LoadJobInfo.builder(TABLE_ID1, ImmutableList.of("URI"))
.formatOptions(FormatOptions.json())
.build();
private static final JobInfo EXTRACT_JOB_INFO =
ExtractJobInfo.builder(TABLE_ID1, ImmutableList.of("URI"))
.format("CSV")
.build();
private static final Field FIELD = Field.of("FieldName", Field.Type.integer());
private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID1, Schema.of(FIELD));
private static final List<RowToInsert> ROWS_TO_INSERT = ImmutableList.of(
RowToInsert.of("id1", ImmutableMap.<String, Object>of("key", "val1")),
RowToInsert.of("id2", ImmutableMap.<String, Object>of("key", "val2")));
private static final InsertAllRequest INSERT_ALL_REQUEST =
InsertAllRequest.of(TABLE_ID1, ROWS_TO_INSERT);
private static final InsertAllRequest INSERT_ALL_REQUEST_COMPLETE =
InsertAllRequest.builder(TABLE_ID1, ROWS_TO_INSERT)
.skipInvalidRows(true)
.ignoreUnknownValues(true)
.build();
private static final InsertAllResponse EMPTY_INSERT_ALL_RESPONSE =
new InsertAllResponse(ImmutableMap.<Long, List<BigQueryError>>of());
private static final FieldValue FIELD_VALUE1 =
new FieldValue(FieldValue.Attribute.PRIMITIVE, "val1");
private static final FieldValue FIELD_VALUE2 =
new FieldValue(FieldValue.Attribute.PRIMITIVE, "val1");
private static final Iterable<List<FieldValue>> ROWS = ImmutableList.of(
(List<FieldValue>) ImmutableList.of(FIELD_VALUE1), ImmutableList.of(FIELD_VALUE2));
@Rule
public ExpectedException thrown = ExpectedException.none();
private BigQuery bigquery;
private Table table;
@Before
public void setUp() throws Exception {
bigquery = createStrictMock(BigQuery.class);
table = new Table(bigquery, TABLE_INFO);
}
@After
public void tearDown() throws Exception {
verify(bigquery);
}
@Test
public void testInfo() throws Exception {
assertEquals(TABLE_INFO, table.info());
replay(bigquery);
}
@Test
public void testBigQuery() throws Exception {
assertSame(bigquery, table.bigquery());
replay(bigquery);
}
@Test
public void testExists_True() throws Exception {
BigQuery.TableOption[] expectedOptions = {BigQuery.TableOption.fields()};
expect(bigquery.getTable(TABLE_INFO.tableId(), expectedOptions)).andReturn(TABLE_INFO);
replay(bigquery);
assertTrue(table.exists());
}
@Test
public void testExists_False() throws Exception {
BigQuery.TableOption[] expectedOptions = {BigQuery.TableOption.fields()};
expect(bigquery.getTable(TABLE_INFO.tableId(), expectedOptions)).andReturn(null);
replay(bigquery);
assertFalse(table.exists());
}
@Test
public void testReload() throws Exception {
TableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build();
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(updatedInfo);
replay(bigquery);
Table updatedTable = table.reload();
assertSame(bigquery, updatedTable.bigquery());
assertEquals(updatedInfo, updatedTable.info());
}
@Test
public void testReloadNull() throws Exception {
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null);
replay(bigquery);
assertNull(table.reload());
}
@Test
public void testReloadWithOptions() throws Exception {
TableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build();
expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields()))
.andReturn(updatedInfo);
replay(bigquery);
Table updatedTable = table.reload(BigQuery.TableOption.fields());
assertSame(bigquery, updatedTable.bigquery());
assertEquals(updatedInfo, updatedTable.info());
}
@Test
public void testUpdate() throws Exception {
BaseTableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build();
expect(bigquery.update(updatedInfo)).andReturn(updatedInfo);
replay(bigquery);
Table updatedTable = table.update(updatedInfo);
assertSame(bigquery, updatedTable.bigquery());
assertEquals(updatedInfo, updatedTable.info());
}
@Test
public void testUpdateWithDifferentId() throws Exception {
TableInfo updatedInfo = TABLE_INFO.toBuilder()
.tableId(TableId.of("dataset", "table3"))
.description("Description")
.build();
replay(bigquery);
thrown.expect(IllegalArgumentException.class);
table.update(updatedInfo);
}
@Test
public void testUpdateWithDifferentDatasetId() throws Exception {
TableInfo updatedInfo = TABLE_INFO.toBuilder()
.tableId(TableId.of("dataset1", "table1"))
.description("Description")
.build();
replay(bigquery);
thrown.expect(IllegalArgumentException.class);
table.update(updatedInfo);
}
@Test
public void testUpdateWithOptions() throws Exception {
BaseTableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build();
expect(bigquery.update(updatedInfo, BigQuery.TableOption.fields())).andReturn(updatedInfo);
replay(bigquery);
Table updatedTable = table.update(updatedInfo, BigQuery.TableOption.fields());
assertSame(bigquery, updatedTable.bigquery());
assertEquals(updatedInfo, updatedTable.info());
}
@Test
public void testDelete() throws Exception {
expect(bigquery.delete(TABLE_INFO.tableId())).andReturn(true);
replay(bigquery);
assertTrue(table.delete());
}
@Test
public void testInsert() throws Exception {
expect(bigquery.insertAll(INSERT_ALL_REQUEST)).andReturn(EMPTY_INSERT_ALL_RESPONSE);
replay(bigquery);
InsertAllResponse response = table.insert(ROWS_TO_INSERT);
assertSame(EMPTY_INSERT_ALL_RESPONSE, response);
}
@Test
public void testInsertComplete() throws Exception {
expect(bigquery.insertAll(INSERT_ALL_REQUEST_COMPLETE)).andReturn(EMPTY_INSERT_ALL_RESPONSE);
replay(bigquery);
InsertAllResponse response = table.insert(ROWS_TO_INSERT, true, true);
assertSame(EMPTY_INSERT_ALL_RESPONSE, response);
}
@Test
public void testList() throws Exception {
PageImpl<List<FieldValue>> tableDataPage = new PageImpl<>(null, "c", ROWS);
expect(bigquery.listTableData(TABLE_ID1)).andReturn(tableDataPage);
replay(bigquery);
Page<List<FieldValue>> dataPage = table.list();
Iterator<List<FieldValue>> tableDataIterator = tableDataPage.values().iterator();
Iterator<List<FieldValue>> dataIterator = dataPage.values().iterator();
assertTrue(Iterators.elementsEqual(tableDataIterator, dataIterator));
}
@Test
public void testListWithOptions() throws Exception {
PageImpl<List<FieldValue>> tableDataPage = new PageImpl<>(null, "c", ROWS);
expect(bigquery.listTableData(TABLE_ID1, BigQuery.TableDataListOption.maxResults(10L)))
.andReturn(tableDataPage);
replay(bigquery);
Page<List<FieldValue>> dataPage = table.list(BigQuery.TableDataListOption.maxResults(10L));
Iterator<List<FieldValue>> tableDataIterator = tableDataPage.values().iterator();
Iterator<List<FieldValue>> dataIterator = dataPage.values().iterator();
assertTrue(Iterators.elementsEqual(tableDataIterator, dataIterator));
}
@Test
public void testCopyFromString() throws Exception {
expect(bigquery.create(COPY_JOB_INFO)).andReturn(COPY_JOB_INFO);
replay(bigquery);
Job job = table.copy(TABLE_ID2.dataset(), TABLE_ID2.table());
assertSame(bigquery, job.bigquery());
assertEquals(COPY_JOB_INFO, job.info());
}
@Test
public void testCopyFromId() throws Exception {
expect(bigquery.create(COPY_JOB_INFO)).andReturn(COPY_JOB_INFO);
replay(bigquery);
Job job = table.copy(TABLE_ID2);
assertSame(bigquery, job.bigquery());
assertEquals(COPY_JOB_INFO, job.info());
}
@Test
public void testLoadDataUri() throws Exception {
expect(bigquery.create(LOAD_JOB_INFO)).andReturn(LOAD_JOB_INFO);
replay(bigquery);
Job job = table.load(FormatOptions.json(), "URI");
assertSame(bigquery, job.bigquery());
assertEquals(LOAD_JOB_INFO, job.info());
}
@Test
public void testLoadDataUris() throws Exception {
expect(bigquery.create(LOAD_JOB_INFO)).andReturn(LOAD_JOB_INFO);
replay(bigquery);
Job job = table.load(FormatOptions.json(), ImmutableList.of("URI"));
assertSame(bigquery, job.bigquery());
assertEquals(LOAD_JOB_INFO, job.info());
}
@Test
public void testExtractDataUri() throws Exception {
expect(bigquery.create(EXTRACT_JOB_INFO)).andReturn(EXTRACT_JOB_INFO);
replay(bigquery);
Job job = table.extract("CSV", "URI");
assertSame(bigquery, job.bigquery());
assertEquals(EXTRACT_JOB_INFO, job.info());
}
@Test
public void testExtractDataUris() throws Exception {
expect(bigquery.create(EXTRACT_JOB_INFO)).andReturn(EXTRACT_JOB_INFO);
replay(bigquery);
Job job = table.extract("CSV", ImmutableList.of("URI"));
assertSame(bigquery, job.bigquery());
assertEquals(EXTRACT_JOB_INFO, job.info());
}
@Test
public void testGetFromId() throws Exception {
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(TABLE_INFO);
replay(bigquery);
Table loadedTable = Table.get(bigquery, TABLE_INFO.tableId());
assertNotNull(loadedTable);
assertEquals(TABLE_INFO, loadedTable.info());
}
@Test
public void testGetFromStrings() throws Exception {
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(TABLE_INFO);
replay(bigquery);
Table loadedTable = Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table());
assertNotNull(loadedTable);
assertEquals(TABLE_INFO, loadedTable.info());
}
@Test
public void testGetFromIdNull() throws Exception {
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null);
replay(bigquery);
assertNull(Table.get(bigquery, TABLE_INFO.tableId()));
}
@Test
public void testGetFromStringsNull() throws Exception {
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null);
replay(bigquery);
assertNull(Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table()));
}
@Test
public void testGetFromIdWithOptions() throws Exception {
expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields()))
.andReturn(TABLE_INFO);
replay(bigquery);
Table loadedTable = Table.get(bigquery, TABLE_INFO.tableId(), BigQuery.TableOption.fields());
assertNotNull(loadedTable);
assertEquals(TABLE_INFO, loadedTable.info());
}
@Test
public void testGetFromStringsWithOptions() throws Exception {
expect(bigquery.getTable(TABLE_INFO.tableId(), BigQuery.TableOption.fields()))
.andReturn(TABLE_INFO);
replay(bigquery);
Table loadedTable =
Table.get(bigquery, TABLE_ID1.dataset(), TABLE_ID1.table(), BigQuery.TableOption.fields());
assertNotNull(loadedTable);
assertEquals(TABLE_INFO, loadedTable.info());
}
}