|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 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.google.cloud.examples.bigquery.snippets; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertFalse; |
| 21 | +import static org.junit.Assert.assertNull; |
| 22 | +import static org.junit.Assert.assertTrue; |
| 23 | + |
| 24 | +import com.google.cloud.Page; |
| 25 | +import com.google.cloud.bigquery.BigQuery; |
| 26 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 27 | +import com.google.cloud.bigquery.Dataset; |
| 28 | +import com.google.cloud.bigquery.Dataset.Builder; |
| 29 | +import com.google.cloud.bigquery.DatasetInfo; |
| 30 | +import com.google.cloud.bigquery.Field; |
| 31 | +import com.google.cloud.bigquery.StandardTableDefinition; |
| 32 | +import com.google.cloud.bigquery.Table; |
| 33 | + |
| 34 | +import org.junit.After; |
| 35 | +import org.junit.Assert; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.BeforeClass; |
| 38 | +import org.junit.Test; |
| 39 | + |
| 40 | +import java.util.Iterator; |
| 41 | + |
| 42 | +public class ITDatasetSnippets { |
| 43 | + private static final String datasetId = "dataset_snippets_integration_test"; |
| 44 | + private static final String nonExistandDatasetId = "non_existant_dataset"; |
| 45 | + private static final String friendlyName = "some_friendly_name"; |
| 46 | + |
| 47 | + private static BigQuery bigquery; |
| 48 | + private static Dataset dataset; |
| 49 | + private static DatasetSnippets datasetSnippets; |
| 50 | + |
| 51 | + @BeforeClass |
| 52 | + public static void beforeClass() { |
| 53 | + bigquery = BigQueryOptions.defaultInstance().service(); |
| 54 | + } |
| 55 | + |
| 56 | + @Before |
| 57 | + public void before() { |
| 58 | + dataset = bigquery.create(DatasetInfo.builder(datasetId).build()); |
| 59 | + datasetSnippets = new DatasetSnippets(dataset); |
| 60 | + } |
| 61 | + |
| 62 | + @After |
| 63 | + public void after() { |
| 64 | + bigquery.delete(datasetId, BigQuery.DatasetDeleteOption.deleteContents()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testDoesDataExistReturnsFalseWhenDatasetDoesntExist() { |
| 69 | + Dataset nonExistantDataset = bigquery.create(DatasetInfo.builder(nonExistandDatasetId).build()); |
| 70 | + DatasetSnippets datasetSnippetsWithNonExistantDataset = new DatasetSnippets(nonExistantDataset); |
| 71 | + bigquery.delete(nonExistandDatasetId, BigQuery.DatasetDeleteOption.deleteContents()); |
| 72 | + assertFalse(datasetSnippetsWithNonExistantDataset.doesDatasetExist()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testDoesDataExistReturnsTrueWhenDatasetExists() { |
| 77 | + assertTrue(datasetSnippets.doesDatasetExist()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testReloadDatasetReturnsNullWhenDatasetDoesntExist() { |
| 82 | + Dataset nonExistantDataset = bigquery.create(DatasetInfo.builder(nonExistandDatasetId).build()); |
| 83 | + DatasetSnippets datasetSnippetsWithNonExistantDataset = new DatasetSnippets(nonExistantDataset); |
| 84 | + bigquery.delete(nonExistandDatasetId, BigQuery.DatasetDeleteOption.deleteContents()); |
| 85 | + assertNull(datasetSnippetsWithNonExistantDataset.reloadDataset()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testReloadDatasetReturnsTheReloadedDatasetWhenDatasetExists() { |
| 90 | + assertNull(dataset.friendlyName()); |
| 91 | + |
| 92 | + Builder builder = dataset.toBuilder(); |
| 93 | + builder.friendlyName(friendlyName); |
| 94 | + builder.build().update(); |
| 95 | + |
| 96 | + Dataset reloadedDataset = datasetSnippets.reloadDataset(); |
| 97 | + assertEquals(friendlyName, reloadedDataset.friendlyName()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testUpdateDatasetReturnsTheUpdatedDatasetWhenDatasetExists() { |
| 102 | + assertNull(dataset.friendlyName()); |
| 103 | + |
| 104 | + Dataset updatedDataset = datasetSnippets.updateDataset(friendlyName); |
| 105 | + assertEquals(friendlyName, updatedDataset.friendlyName()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testDeleteDatasetReturnsFalseWhenDatasetDoesntExist() { |
| 110 | + Dataset nonExistantDataset = bigquery.create(DatasetInfo.builder(nonExistandDatasetId).build()); |
| 111 | + DatasetSnippets datasetSnippetsWithNonExistantDataset = new DatasetSnippets(nonExistantDataset); |
| 112 | + bigquery.delete(nonExistandDatasetId, BigQuery.DatasetDeleteOption.deleteContents()); |
| 113 | + assertFalse(datasetSnippetsWithNonExistantDataset.deleteDataset()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testDeleteDatasetReturnsTrueWhenDatasetExists() { |
| 118 | + assertTrue(datasetSnippets.deleteDataset()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testListTablesWhenEmpty() { |
| 123 | + Page<Table> tables = datasetSnippets.listDataset(); |
| 124 | + assertFalse(tables.iterateAll().hasNext()); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testListTablesWhenNotEmpty() { |
| 129 | + String expectedTableName = "test_table"; |
| 130 | + |
| 131 | + dataset.create(expectedTableName, StandardTableDefinition.builder().build()); |
| 132 | + Page<Table> tables = datasetSnippets.listDataset(); |
| 133 | + Iterator<Table> iterator = tables.iterateAll(); |
| 134 | + assertTrue(iterator.hasNext()); |
| 135 | + |
| 136 | + Table actualTable = iterator.next(); |
| 137 | + assertTrue(actualTable.tableId().table().equals(expectedTableName)); |
| 138 | + assertFalse(iterator.hasNext()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testGetTable() { |
| 143 | + String expectedTableName = "test_table"; |
| 144 | + |
| 145 | + dataset.create(expectedTableName, StandardTableDefinition.builder().build()); |
| 146 | + Table actualTable = datasetSnippets.getTable(expectedTableName); |
| 147 | + |
| 148 | + Assert.assertNotNull(actualTable); |
| 149 | + Assert.assertEquals(expectedTableName, actualTable.tableId().table()); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testCreateTable() { |
| 154 | + String expectedTableName = "test_table"; |
| 155 | + |
| 156 | + Table actualTable = datasetSnippets.createTable(expectedTableName); |
| 157 | + Assert.assertNotNull(actualTable); |
| 158 | + Assert.assertEquals(expectedTableName, actualTable.tableId().table()); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testCreateTableWithSchema() { |
| 163 | + String expectedTableName = "test_table"; |
| 164 | + String expectedFieldName = "test_field"; |
| 165 | + |
| 166 | + Table actualTable = datasetSnippets.createTable(expectedTableName, expectedFieldName); |
| 167 | + Assert.assertNotNull(actualTable); |
| 168 | + Assert.assertEquals(expectedTableName, actualTable.tableId().table()); |
| 169 | + Assert.assertEquals(1, actualTable.definition().schema().fields().size()); |
| 170 | + |
| 171 | + Field actualField = actualTable.definition().schema().fields().get(0); |
| 172 | + Assert.assertEquals(expectedFieldName, actualField.name()); |
| 173 | + } |
| 174 | +} |
0 commit comments