|
| 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 junit.framework.TestCase.assertNotNull; |
| 21 | + |
| 22 | +import com.google.cloud.bigquery.Field; |
| 23 | +import com.google.cloud.bigquery.InsertAllRequest; |
| 24 | +import com.google.cloud.bigquery.LegacySQLTypeName; |
| 25 | +import com.google.cloud.bigquery.Schema; |
| 26 | +import java.io.ByteArrayOutputStream; |
| 27 | +import java.io.PrintStream; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 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 TableInsertRowsWithoutRowIdsIT { |
| 39 | + |
| 40 | + private String tableName; |
| 41 | + private ByteArrayOutputStream bout; |
| 42 | + private PrintStream out; |
| 43 | + |
| 44 | + private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME"); |
| 45 | + |
| 46 | + private static void requireEnvVar(String varName) { |
| 47 | + assertNotNull( |
| 48 | + "Environment variable " + varName + " is required to perform these tests.", |
| 49 | + System.getenv(varName)); |
| 50 | + } |
| 51 | + |
| 52 | + @BeforeClass |
| 53 | + public static void checkRequirements() { |
| 54 | + requireEnvVar("BIGQUERY_DATASET_NAME"); |
| 55 | + } |
| 56 | + |
| 57 | + @Before |
| 58 | + public void setUp() { |
| 59 | + bout = new ByteArrayOutputStream(); |
| 60 | + out = new PrintStream(bout); |
| 61 | + System.setOut(out); |
| 62 | + |
| 63 | + tableName = "INSERT_ROW_WITHOUT_ROW_ID_TEST" + UUID.randomUUID().toString().substring(0, 8); |
| 64 | + Schema schema = |
| 65 | + Schema.of( |
| 66 | + Field.of("stringField", LegacySQLTypeName.STRING), |
| 67 | + Field.of("numericField", LegacySQLTypeName.NUMERIC)); |
| 68 | + |
| 69 | + // Create table in dataset for testing |
| 70 | + CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema); |
| 71 | + |
| 72 | + bout = new ByteArrayOutputStream(); |
| 73 | + out = new PrintStream(bout); |
| 74 | + System.setOut(out); |
| 75 | + } |
| 76 | + |
| 77 | + @After |
| 78 | + public void tearDown() { |
| 79 | + // Clean up |
| 80 | + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); |
| 81 | + System.setOut(null); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testTableInsertRowsWithoutRowIds() { |
| 86 | + // Create rows to insert |
| 87 | + Map<String, Object> rowContent1 = new HashMap<>(); |
| 88 | + rowContent1.put("stringField", "Phred Phlyntstone"); |
| 89 | + rowContent1.put("numericField", 32); |
| 90 | + Map<String, Object> rowContent2 = new HashMap<>(); |
| 91 | + rowContent2.put("stringField", "Wylma Phlyntstone"); |
| 92 | + rowContent2.put("numericField", 29); |
| 93 | + List<InsertAllRequest.RowToInsert> rowContent = new ArrayList<>(); |
| 94 | + rowContent.add(InsertAllRequest.RowToInsert.of(rowContent1)); |
| 95 | + rowContent.add(InsertAllRequest.RowToInsert.of(rowContent2)); |
| 96 | + TableInsertRowsWithoutRowIds.tableInsertRowsWithoutRowIds( |
| 97 | + BIGQUERY_DATASET_NAME, tableName, rowContent); |
| 98 | + assertThat(bout.toString()).contains("Rows successfully inserted into table without row ids"); |
| 99 | + } |
| 100 | +} |
0 commit comments