|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.bigquery; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | + |
| 22 | +import com.google.cloud.bigquery.BigQuery; |
| 23 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 24 | +import com.google.cloud.bigquery.Schema; |
| 25 | +import com.google.cloud.bigquery.StandardTableDefinition; |
| 26 | +import com.google.cloud.bigquery.TableId; |
| 27 | +import com.google.cloud.bigquery.TableInfo; |
| 28 | +import com.google.common.collect.ImmutableMap; |
| 29 | +import java.io.ByteArrayOutputStream; |
| 30 | +import java.io.PrintStream; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.UUID; |
| 33 | +import org.junit.After; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.BeforeClass; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +public class DeleteLabelTableIT { |
| 39 | + |
| 40 | + private String tableName; |
| 41 | + private ByteArrayOutputStream bout; |
| 42 | + private PrintStream out; |
| 43 | + |
| 44 | + private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); |
| 45 | + |
| 46 | + private static String requireEnvVar(String varName) { |
| 47 | + String value = System.getenv(varName); |
| 48 | + assertNotNull( |
| 49 | + "Environment variable " + varName + " is required to perform these tests.", |
| 50 | + System.getenv(varName)); |
| 51 | + return value; |
| 52 | + } |
| 53 | + |
| 54 | + @BeforeClass |
| 55 | + public static void checkRequirements() { |
| 56 | + requireEnvVar("BIGQUERY_DATASET_NAME"); |
| 57 | + } |
| 58 | + |
| 59 | + @Before |
| 60 | + public void setUp() { |
| 61 | + // create a temporary table |
| 62 | + tableName = "MY_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8); |
| 63 | + Map<String, String> labels = ImmutableMap.of("color", "green"); |
| 64 | + BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService(); |
| 65 | + TableId tableId = TableId.of(BIGQUERY_DATASET_NAME, tableName); |
| 66 | + StandardTableDefinition tableDefinition = StandardTableDefinition.of(Schema.of()); |
| 67 | + TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).setLabels(labels).build(); |
| 68 | + bigQuery.create(tableInfo); |
| 69 | + |
| 70 | + bout = new ByteArrayOutputStream(); |
| 71 | + out = new PrintStream(bout); |
| 72 | + System.setOut(out); |
| 73 | + } |
| 74 | + |
| 75 | + @After |
| 76 | + public void tearDown() { |
| 77 | + // delete a temporary table |
| 78 | + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); |
| 79 | + System.setOut(null); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testDeleteLabelTable() { |
| 84 | + DeleteLabelTable.deleteLabelTable(BIGQUERY_DATASET_NAME, tableName); |
| 85 | + assertThat(bout.toString()).contains("Table label deleted successfully"); |
| 86 | + } |
| 87 | +} |
0 commit comments