/
StandardTableDefinitionTest.java
181 lines (167 loc) · 7.68 KB
/
StandardTableDefinitionTest.java
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
/*
* Copyright 2016 Google LLC
*
* 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.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.api.services.bigquery.model.Streamingbuffer;
import com.google.api.services.bigquery.model.Table;
import com.google.api.services.bigquery.model.TableReference;
import com.google.cloud.bigquery.StandardTableDefinition.StreamingBuffer;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
public class StandardTableDefinitionTest {
private static final Field FIELD_SCHEMA1 =
Field.newBuilder("StringField", LegacySQLTypeName.STRING)
.setMode(Field.Mode.NULLABLE)
.setDescription("FieldDescription1")
.build();
private static final Field FIELD_SCHEMA2 =
Field.newBuilder("IntegerField", LegacySQLTypeName.INTEGER)
.setMode(Field.Mode.REPEATED)
.setDescription("FieldDescription2")
.build();
private static final Field FIELD_SCHEMA3 =
Field.newBuilder("RecordField", LegacySQLTypeName.RECORD, FIELD_SCHEMA1, FIELD_SCHEMA2)
.setMode(Field.Mode.REQUIRED)
.setDescription("FieldDescription3")
.build();
private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA1, FIELD_SCHEMA2, FIELD_SCHEMA3);
private static final Long NUM_BYTES = 42L;
private static final Long NUM_LONG_TERM_BYTES = 18L;
private static final Long NUM_ROWS = 43L;
private static final String LOCATION = "US";
private static final StreamingBuffer STREAMING_BUFFER = new StreamingBuffer(1L, 2L, 3L);
private static final TimePartitioning TIME_PARTITIONING =
TimePartitioning.of(TimePartitioning.Type.DAY, 42);
private static final Clustering CLUSTERING =
Clustering.newBuilder().setFields(ImmutableList.of("Foo", "Bar")).build();
private static final StandardTableDefinition TABLE_DEFINITION =
StandardTableDefinition.newBuilder()
.setLocation(LOCATION)
.setNumBytes(NUM_BYTES)
.setNumRows(NUM_ROWS)
.setNumLongTermBytes(NUM_LONG_TERM_BYTES)
.setStreamingBuffer(STREAMING_BUFFER)
.setSchema(TABLE_SCHEMA)
.setTimePartitioning(TIME_PARTITIONING)
.setClustering(CLUSTERING)
.build();
@Test
public void testToBuilder() {
compareStandardTableDefinition(TABLE_DEFINITION, TABLE_DEFINITION.toBuilder().build());
StandardTableDefinition tableDefinition =
TABLE_DEFINITION.toBuilder().setLocation("EU").build();
assertEquals("EU", tableDefinition.getLocation());
tableDefinition = tableDefinition.toBuilder().setLocation(LOCATION).build();
compareStandardTableDefinition(TABLE_DEFINITION, tableDefinition);
}
@Test
public void testToBuilderIncomplete() {
StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
assertEquals(tableDefinition, tableDefinition.toBuilder().build());
}
@Test
public void testBuilder() {
assertEquals(TableDefinition.Type.TABLE, TABLE_DEFINITION.getType());
assertEquals(TABLE_SCHEMA, TABLE_DEFINITION.getSchema());
assertEquals(LOCATION, TABLE_DEFINITION.getLocation());
assertEquals(NUM_BYTES, TABLE_DEFINITION.getNumBytes());
assertEquals(NUM_LONG_TERM_BYTES, TABLE_DEFINITION.getNumLongTermBytes());
assertEquals(NUM_ROWS, TABLE_DEFINITION.getNumRows());
assertEquals(STREAMING_BUFFER, TABLE_DEFINITION.getStreamingBuffer());
assertEquals(TIME_PARTITIONING, TABLE_DEFINITION.getTimePartitioning());
assertEquals(CLUSTERING, TABLE_DEFINITION.getClustering());
}
@Test
public void testOf() {
StandardTableDefinition definition = StandardTableDefinition.of(TABLE_SCHEMA);
assertEquals(TableDefinition.Type.TABLE, TABLE_DEFINITION.getType());
assertEquals(TABLE_SCHEMA, TABLE_DEFINITION.getSchema());
assertNull(definition.getLocation());
assertNull(definition.getNumBytes());
assertNull(definition.getNumLongTermBytes());
assertNull(definition.getNumRows());
assertNull(definition.getStreamingBuffer());
assertNull(definition.getTimePartitioning());
assertNull(definition.getClustering());
}
@Test
public void testToAndFromPb() {
assertTrue(TableDefinition.fromPb(TABLE_DEFINITION.toPb()) instanceof StandardTableDefinition);
compareStandardTableDefinition(
TABLE_DEFINITION, TableDefinition.<StandardTableDefinition>fromPb(TABLE_DEFINITION.toPb()));
StandardTableDefinition definition = StandardTableDefinition.of(TABLE_SCHEMA);
assertTrue(TableDefinition.fromPb(definition.toPb()) instanceof StandardTableDefinition);
compareStandardTableDefinition(
definition, TableDefinition.<StandardTableDefinition>fromPb(definition.toPb()));
}
@Test
public void testFromPbWithUnexpectedTimePartitioningTypeRaisesInvalidArgumentException() {
Table invalidTable =
new Table()
.setType("TABLE")
.setTableReference(
new TableReference()
.setProjectId("ILLEGAL_ARG_TEST_PROJECT")
.setDatasetId("ILLEGAL_ARG_TEST_DATASET")
.setTableId("ILLEGAL_ARG_TEST_TABLE"))
.setTimePartitioning(
new com.google.api.services.bigquery.model.TimePartitioning().setType("GHURRY"));
try {
StandardTableDefinition.fromPb(invalidTable);
} catch (IllegalArgumentException ie) {
assertThat(
ie.getMessage(),
allOf(
containsString("Illegal Argument - Got unexpected time partitioning"),
containsString("GHURRY"),
containsString("ILLEGAL_ARG_TEST_PROJECT"),
containsString("ILLEGAL_ARG_TEST_DATASET"),
containsString("ILLEGAL_ARG_TEST_TABLE")));
return;
}
fail("testFromPb illegal argument exception did not throw!");
}
@Test
public void testFromPbWithNullEstimatedRowsAndBytes() {
StandardTableDefinition.fromPb(
TABLE_DEFINITION.toPb().setStreamingBuffer(new Streamingbuffer()));
}
@Test
public void testStreamingBufferWithNullFieldsToPb() {
new StreamingBuffer(null, null, null).toPb();
}
private void compareStandardTableDefinition(
StandardTableDefinition expected, StandardTableDefinition value) {
assertEquals(expected, value);
assertEquals(expected.getSchema(), value.getSchema());
assertEquals(expected.getType(), value.getType());
assertEquals(expected.getNumBytes(), value.getNumBytes());
assertEquals(expected.getNumLongTermBytes(), value.getNumLongTermBytes());
assertEquals(expected.getNumRows(), value.getNumRows());
assertEquals(expected.getLocation(), value.getLocation());
assertEquals(expected.getStreamingBuffer(), value.getStreamingBuffer());
assertEquals(expected.getType(), value.getType());
assertEquals(expected.getTimePartitioning(), value.getTimePartitioning());
assertEquals(expected.getClustering(), value.getClustering());
assertEquals(expected.hashCode(), value.hashCode());
}
}