Skip to content

Commit

Permalink
feat: add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Praful Makani committed Mar 19, 2020
1 parent b9de4aa commit bade2c8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
Expand Up @@ -134,19 +134,21 @@ public static MaterializedViewDefinition of(String query) {

static MaterializedViewDefinition fromPb(Table tablePb) {
Builder builder = newBuilder().table(tablePb);
com.google.api.services.bigquery.model.MaterializedViewDefinition materializedViewDefinition =
tablePb.getMaterializedView();
if (materializedViewDefinition.getQuery() != null) {
builder.setQuery(materializedViewDefinition.getQuery());
}
if (materializedViewDefinition.getLastRefreshTime() != null) {
builder.setLastRefreshTime(materializedViewDefinition.getLastRefreshTime());
}
if (materializedViewDefinition.getEnableRefresh() != null) {
builder.setEnableRefresh(materializedViewDefinition.getEnableRefresh());
}
if (materializedViewDefinition.getRefreshIntervalMs() != null) {
builder.setRefreshIntervalMs(materializedViewDefinition.getRefreshIntervalMs());
if (tablePb.getMaterializedView() != null) {
com.google.api.services.bigquery.model.MaterializedViewDefinition materializedViewDefinition =
tablePb.getMaterializedView();
if (materializedViewDefinition.getQuery() != null) {
builder.setQuery(materializedViewDefinition.getQuery());
}
if (materializedViewDefinition.getLastRefreshTime() != null) {
builder.setLastRefreshTime(materializedViewDefinition.getLastRefreshTime());
}
if (materializedViewDefinition.getEnableRefresh() != null) {
builder.setEnableRefresh(materializedViewDefinition.getEnableRefresh());
}
if (materializedViewDefinition.getRefreshIntervalMs() != null) {
builder.setRefreshIntervalMs(materializedViewDefinition.getRefreshIntervalMs());
}
}
return builder.build();
}
Expand Down
Expand Up @@ -33,6 +33,7 @@
import com.google.cloud.Date;
import com.google.cloud.RetryOption;
import com.google.cloud.bigquery.Acl;
import com.google.cloud.ServiceOptions;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
import com.google.cloud.bigquery.BigQuery.DatasetField;
Expand Down Expand Up @@ -65,6 +66,7 @@
import com.google.cloud.bigquery.JobStatistics.LoadStatistics;
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.LoadJobConfiguration;
import com.google.cloud.bigquery.MaterializedViewDefinition;
import com.google.cloud.bigquery.Model;
import com.google.cloud.bigquery.ModelId;
import com.google.cloud.bigquery.ModelInfo;
Expand Down Expand Up @@ -134,6 +136,7 @@ public class ITBigQueryTest {
private static final String OTHER_DATASET = RemoteBigQueryHelper.generateDatasetName();
private static final String MODEL_DATASET = RemoteBigQueryHelper.generateDatasetName();
private static final String ROUTINE_DATASET = RemoteBigQueryHelper.generateDatasetName();
private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId();
private static final Map<String, String> LABELS =
ImmutableMap.of(
"example-label1", "example-value1",
Expand Down Expand Up @@ -219,6 +222,17 @@ public class ITBigQueryTest {
Field.newBuilder("BooleanField", LegacySQLTypeName.BOOLEAN)
.setMode(Field.Mode.NULLABLE)
.build());
private static final Schema VIEW_SCHEMA =
Schema.of(
Field.newBuilder("TimestampField", LegacySQLTypeName.TIMESTAMP)
.setMode(Field.Mode.NULLABLE)
.build(),
Field.newBuilder("StringField", LegacySQLTypeName.STRING)
.setMode(Field.Mode.NULLABLE)
.build(),
Field.newBuilder("BooleanField", LegacySQLTypeName.BOOLEAN)
.setMode(Field.Mode.NULLABLE)
.build());
private static final RangePartitioning.Range RANGE =
RangePartitioning.Range.newBuilder().setStart(1L).setInterval(2L).setEnd(20L).build();
private static final RangePartitioning RANGE_PARTITIONING =
Expand Down Expand Up @@ -640,18 +654,7 @@ public void testCreateViewTable() throws InterruptedException {
assertNotNull(remoteTable);
assertEquals(createdTable.getTableId(), remoteTable.getTableId());
assertTrue(remoteTable.getDefinition() instanceof ViewDefinition);
Schema expectedSchema =
Schema.of(
Field.newBuilder("TimestampField", LegacySQLTypeName.TIMESTAMP)
.setMode(Field.Mode.NULLABLE)
.build(),
Field.newBuilder("StringField", LegacySQLTypeName.STRING)
.setMode(Field.Mode.NULLABLE)
.build(),
Field.newBuilder("BooleanField", LegacySQLTypeName.BOOLEAN)
.setMode(Field.Mode.NULLABLE)
.build());
assertEquals(expectedSchema, remoteTable.getDefinition().getSchema());
assertEquals(VIEW_SCHEMA, remoteTable.getDefinition().getSchema());
QueryJobConfiguration config =
QueryJobConfiguration.newBuilder("SELECT * FROM " + tableName)
.setDefaultDataset(DatasetId.of(DATASET))
Expand All @@ -678,6 +681,30 @@ public void testCreateViewTable() throws InterruptedException {
assertTrue(remoteTable.delete());
}

@Test
public void testCreateMaterializedViewTable() {
String tableName = "test_materialized_view_table";
TableId tableId = TableId.of(DATASET, tableName);
MaterializedViewDefinition viewDefinition =
MaterializedViewDefinition.newBuilder(
String.format(
"SELECT MAX(TimestampField) AS TimestampField,StringField, MAX(BooleanField) AS BooleanField FROM %s.%s.%s GROUP BY StringField",
PROJECT_ID, DATASET, TABLE_ID.getTable()))
.build();
TableInfo tableInfo = TableInfo.of(tableId, viewDefinition);
Table createdTable = bigquery.create(tableInfo);
assertNotNull(createdTable);
assertEquals(DATASET, createdTable.getTableId().getDataset());
assertEquals(tableName, createdTable.getTableId().getTable());
Table remoteTable = bigquery.getTable(DATASET, tableName);
assertNotNull(remoteTable);
assertEquals(createdTable.getTableId(), remoteTable.getTableId());
assertEquals(createdTable.getTableId(), remoteTable.getTableId());
assertTrue(remoteTable.getDefinition() instanceof MaterializedViewDefinition);
assertEquals(VIEW_SCHEMA, remoteTable.getDefinition().getSchema());
assertTrue(remoteTable.delete());
}

@Test
public void testListTables() {
String tableName = "test_list_tables";
Expand Down

0 comments on commit bade2c8

Please sign in to comment.