talatuyarer commented on code in PR #12808:
URL: https://github.com/apache/iceberg/pull/12808#discussion_r2067928742


##########
bigquery/src/test/java/org/apache/iceberg/gcp/bigquery/TestBigQueryTableOperations.java:
##########
@@ -0,0 +1,291 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.iceberg.gcp.bigquery;
+
+import static 
org.apache.iceberg.gcp.bigquery.BigQueryMetastoreCatalog.PROJECT_ID;
+import static org.apache.iceberg.types.Types.NestedField.required;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.api.services.bigquery.model.Dataset;
+import com.google.api.services.bigquery.model.DatasetReference;
+import com.google.api.services.bigquery.model.ExternalCatalogDatasetOptions;
+import com.google.api.services.bigquery.model.ExternalCatalogTableOptions;
+import com.google.api.services.bigquery.model.StorageDescriptor;
+import com.google.api.services.bigquery.model.Table;
+import com.google.api.services.bigquery.model.TableReference;
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Optional;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.filefilter.TrueFileFilter;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.types.Types;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.ArgumentCaptor;
+
+public class TestBigQueryTableOperations {
+
+  @TempDir private File tempFolder;
+  public static final String METADATA_LOCATION_PROP = "metadata_location";
+  private static final String GCP_PROJECT = "my-project";
+  private static final String GCP_REGION = "us";
+  private static final String NS = "db";
+  private static final String TABLE = "tbl";
+  private static final TableIdentifier IDENTIFIER = TableIdentifier.of(NS, 
TABLE);
+
+  private static final TableReference TABLE_REFERENCE =
+      new 
TableReference().setProjectId(GCP_PROJECT).setDatasetId(NS).setTableId(TABLE);
+
+  private static final Schema SCHEMA =
+      new Schema(
+          required(1, "id", Types.IntegerType.get(), "unique ID"),
+          required(2, "data", Types.StringType.get()));
+
+  private final BigQueryMetastoreClient client = 
mock(BigQueryMetastoreClient.class);
+
+  private BigQueryMetastoreCatalog catalog;
+  private BigQueryTableOperations tableOps;
+
+  @BeforeEach
+  public void before() {
+    this.catalog = new BigQueryMetastoreCatalog();
+    this.catalog.setConf(new Configuration());
+    String warehouseLocation = 
tempFolder.toPath().resolve("hive-warehouse").toString();
+
+    catalog.initialize(
+        "CATALOG_ID",
+        ImmutableMap.of(
+            PROJECT_ID,
+            GCP_PROJECT,
+            CatalogProperties.WAREHOUSE_LOCATION,
+            warehouseLocation,
+            CatalogProperties.FILE_IO_IMPL,
+            "org.apache.iceberg.hadoop.HadoopFileIO"),
+        GCP_PROJECT,
+        GCP_REGION,
+        client);
+    this.tableOps = (BigQueryTableOperations) catalog.newTableOps(IDENTIFIER);
+  }
+
+  @Test
+  public void fetchLatestMetadataFromBigQuery() throws Exception {
+    Table createdTable = createTestTable();
+    reset(client);
+    when(client.load(TABLE_REFERENCE)).thenReturn(createdTable);
+
+    tableOps.refresh();
+    assertThat(
+            createdTable
+                .getExternalCatalogTableOptions()
+                .getParameters()
+                .getOrDefault(METADATA_LOCATION_PROP, ""))
+        .isEqualTo(tableOps.currentMetadataLocation());
+
+    reset(client);
+    when(client.load(TABLE_REFERENCE))
+        .thenThrow(new NoSuchTableException("error message getTable"));
+    // Refresh fails when table is not found but metadata already presents.
+    assertThatThrownBy(() -> tableOps.refresh())
+        .isInstanceOf(NoSuchTableException.class)
+        .hasMessageContaining("error message getTable");
+  }
+
+  @Test
+  public void loadNonIcebergTableFails() {
+    when(client.load(TABLE_REFERENCE)).thenReturn(new 
Table().setTableReference(TABLE_REFERENCE));
+
+    assertThatThrownBy(() -> tableOps.refresh())
+        .isInstanceOf(ValidationException.class)
+        .hasMessageContaining("metadata location not found");
+  }
+
+  @Test
+  public void loadNoOpWhenMetadataAndTableNotFound() {
+    when(client.load(TABLE_REFERENCE))
+        .thenThrow(new NoSuchTableException("error message getTable"));
+    // Table not found won't cause errors when the metadata is null.
+    assertThat(tableOps.currentMetadataLocation()).isNull();

Review Comment:
   Yes I added. While applying changes Looks like I deleted but i did not added 
:) 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to