forked from googleapis/java-bigquery
/
MaterializedViewDefinitionTest.java
110 lines (100 loc) · 4.72 KB
/
MaterializedViewDefinitionTest.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
/*
* Copyright 2020 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.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
public class MaterializedViewDefinitionTest {
private static final String MATERIALIZED_VIEW_QUERY = "MATERIALIZED_VIEW_QUERY";
private static final Long LAST_REFRESH_TIME = 1580302008L;
private static final Boolean ENABLE_REFRESH = false;
private static final Long REFRESH_INTERVAL_MS = 60000L;
private static final Schema SCHEMA = Schema.of();
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 MaterializedViewDefinition MATERIALIZED_VIEW_DEFINITION =
MaterializedViewDefinition.newBuilder()
.setSchema(SCHEMA)
.setQuery(MATERIALIZED_VIEW_QUERY)
.setLastRefreshTime(LAST_REFRESH_TIME)
.setEnableRefresh(ENABLE_REFRESH)
.setRefreshIntervalMs(REFRESH_INTERVAL_MS)
.setClustering(CLUSTERING)
.setTimePartitioning(TIME_PARTITIONING)
.build();
@Test
public void testToBuilder() {
compareMaterializedView(
MATERIALIZED_VIEW_DEFINITION, MATERIALIZED_VIEW_DEFINITION.toBuilder().build());
MaterializedViewDefinition materializedViewDefinition =
MATERIALIZED_VIEW_DEFINITION.toBuilder().setQuery("NEW QUERY").build();
assertEquals("NEW QUERY", materializedViewDefinition.getQuery());
materializedViewDefinition =
materializedViewDefinition.toBuilder().setQuery(MATERIALIZED_VIEW_QUERY).build();
compareMaterializedView(MATERIALIZED_VIEW_DEFINITION, materializedViewDefinition);
}
@Test
public void testToBuilderIncomplete() {
TableDefinition materializedViewDefinition =
MaterializedViewDefinition.of(MATERIALIZED_VIEW_QUERY);
assertEquals(materializedViewDefinition, materializedViewDefinition.toBuilder().build());
}
@Test
public void testBuilder() {
assertEquals(MATERIALIZED_VIEW_QUERY, MATERIALIZED_VIEW_DEFINITION.getQuery());
assertEquals(TableDefinition.Type.MATERIALIZED_VIEW, MATERIALIZED_VIEW_DEFINITION.getType());
assertEquals(LAST_REFRESH_TIME, MATERIALIZED_VIEW_DEFINITION.getLastRefreshTime());
MaterializedViewDefinition materializedViewDefinition =
MaterializedViewDefinition.newBuilder()
.setSchema(SCHEMA)
.setQuery(MATERIALIZED_VIEW_QUERY)
.setLastRefreshTime(LAST_REFRESH_TIME)
.setEnableRefresh(ENABLE_REFRESH)
.setRefreshIntervalMs(REFRESH_INTERVAL_MS)
.setClustering(CLUSTERING)
.setTimePartitioning(TIME_PARTITIONING)
.build();
assertEquals(MATERIALIZED_VIEW_DEFINITION, materializedViewDefinition);
}
@Test
public void testToAndFromPb() {
MaterializedViewDefinition materializedViewDefinition =
MATERIALIZED_VIEW_DEFINITION.toBuilder().build();
assertTrue(
TableDefinition.fromPb(materializedViewDefinition.toPb())
instanceof MaterializedViewDefinition);
compareMaterializedView(
materializedViewDefinition,
TableDefinition.<MaterializedViewDefinition>fromPb(materializedViewDefinition.toPb()));
}
private void compareMaterializedView(
MaterializedViewDefinition expected, MaterializedViewDefinition actual) {
assertEquals(expected.getType(), actual.getType());
assertEquals(expected.getSchema(), actual.getSchema());
assertEquals(expected.getQuery(), actual.getQuery());
assertEquals(expected.getLastRefreshTime(), actual.getLastRefreshTime());
assertEquals(expected.getEnableRefresh(), actual.getEnableRefresh());
assertEquals(expected.getRefreshIntervalMs(), actual.getRefreshIntervalMs());
assertEquals(expected.getClustering(), actual.getClustering());
assertEquals(expected.getTimePartitioning(), actual.getTimePartitioning());
assertEquals(expected.toString(), actual.toString());
assertEquals(expected.hashCode(), actual.hashCode());
assertEquals(expected, actual);
}
}