dhruv-pratap commented on code in PR #4706:
URL: https://github.com/apache/iceberg/pull/4706#discussion_r872496987


##########
python/tests/catalog/test_base.py:
##########
@@ -0,0 +1,444 @@
+#  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.
+
+from dataclasses import dataclass
+from typing import Dict, List, Optional, Tuple, cast
+
+import pytest
+
+from iceberg.catalog.base import Catalog
+from iceberg.exceptions import (
+    AlreadyExistsError,
+    NamespaceNotEmptyError,
+    NamespaceNotFoundError,
+    TableNotFoundError,
+)
+from iceberg.schema import Schema
+from iceberg.table.base import PartitionSpec, Table
+
+
+@dataclass(frozen=True)
+class InMemoryTable(Table):
+    """An in-memory table representation for testing purposes.
+
+    Usage:
+        table_spec = InMemoryTable(
+            namespace = ("com", "organization", "department"),
+            name = "my_table",
+            schema = Schema(),
+            location = "protocol://some/location",  // Optional
+            partition_spec = PartitionSpec(),       // Optional
+            properties = [                          // Optional
+                "key1": "value1",
+                "key2": "value2",
+            ]
+        )
+    """
+
+    namespace: Tuple[str, ...]
+    name: str
+    schema: Schema
+    location: str
+    partition_spec: PartitionSpec
+    properties: Dict[str, str]
+
+
+class InMemoryCatalog(Catalog):
+    """An in-memory catalog implementation for testing purposes."""
+
+    __tables: Dict[Tuple[Tuple[str, ...], str], InMemoryTable]
+    __namespaces: Dict[Tuple[str, ...], Dict[str, str]]
+
+    def __init__(self, name: str, properties: Dict[str, str]):
+        super().__init__(name, properties)
+        self.__tables = {}
+        self.__namespaces = {}
+
+    def create_table(
+        self,
+        *,
+        namespace: Tuple[str, ...],
+        name: str,
+        schema: Schema,
+        location: Optional[str] = None,
+        partition_spec: Optional[PartitionSpec] = None,
+        properties: Optional[Dict[str, str]] = None,
+    ) -> Table:
+
+        if (namespace, name) in self.__tables:
+            raise AlreadyExistsError(f"Table {name} already exists in 
namespace {namespace}")
+        else:
+            if namespace not in self.__namespaces:
+                self.__namespaces[namespace] = {}
+
+            table = InMemoryTable(
+                namespace=namespace,
+                name=name,
+                schema=schema if schema else None,
+                location=location if location else None,
+                partition_spec=partition_spec if partition_spec else None,
+                properties=properties if properties else {},
+            )
+            self.__tables[(namespace, name)] = table
+            return table
+
+    def table(self, namespace: Tuple[str, ...], name: str) -> Table:
+        try:
+            return self.__tables[(namespace, name)]
+        except KeyError:
+            raise TableNotFoundError(f"Table {name} not found in the catalog")
+
+    def drop_table(self, namespace: Tuple[str, ...], name: str, purge: bool = 
True) -> None:
+        try:
+            self.__tables.pop((namespace, name))
+        except KeyError:
+            raise TableNotFoundError(f"Table {name} not found in the catalog")
+
+    def rename_table(self, from_namespace: Tuple[str, ...], from_name: str, 
to_namespace: Tuple[str, ...], to_name: str) -> Table:
+        try:
+            table = self.__tables.pop((from_namespace, from_name))
+        except KeyError:
+            raise TableNotFoundError(f"Table {from_name} not found in the 
catalog")
+
+        renamed_table = InMemoryTable(
+            namespace=to_namespace,
+            name=to_name,
+            schema=table.schema,
+            location=table.location,
+            partition_spec=table.partition_spec,
+            properties=table.properties,
+        )
+        if to_namespace not in self.__namespaces:
+            self.__namespaces[to_namespace] = {}
+
+        self.__tables[(to_namespace, to_name)] = renamed_table
+        return renamed_table
+
+    def replace_table(
+        self,
+        *,
+        namespace: Tuple[str, ...],
+        name: str,
+        schema: Schema,
+        location: Optional[str] = None,
+        partition_spec: Optional[PartitionSpec] = None,
+        properties: Optional[Dict[str, str]] = None,
+    ) -> Table:
+
+        try:
+            table = self.__tables.pop((namespace, name))
+        except KeyError:
+            raise TableNotFoundError(f"Table {name} not found in the catalog")
+
+        replaced_table = InMemoryTable(
+            namespace=namespace if namespace else table.namespace,
+            name=name if name else table.name,
+            schema=schema if schema else table.schema,
+            location=location if location else table.location,
+            partition_spec=partition_spec if partition_spec else 
table.partition_spec,
+            properties={**table.properties, **properties},
+        )
+        self.__tables[(replaced_table.namespace, replaced_table.name)] = 
replaced_table
+        return replaced_table
+
+    def create_namespace(self, namespace: Tuple[str, ...], properties: 
Optional[Dict[str, str]] = None) -> None:
+        if namespace in self.__namespaces:
+            raise AlreadyExistsError(f"Namespace {namespace} already exists")
+        else:
+            self.__namespaces[namespace] = properties if properties else {}
+
+    def drop_namespace(self, namespace: Tuple[str, ...]) -> None:
+        if [table_name_tuple for table_name_tuple in self.__tables.keys() if 
namespace in table_name_tuple]:
+            raise NamespaceNotEmptyError(f"Namespace {namespace} not empty")
+        try:
+            self.__namespaces.pop(namespace)
+        except KeyError:
+            raise NamespaceNotFoundError(f"Namespace {namespace} not found in 
the catalog")
+
+    def list_tables(self, namespace: Optional[Tuple[str, ...]] = None) -> 
List[Tuple[Tuple[str, ...], str]]:
+        if namespace:
+            list_tables = [table_name_tuple for table_name_tuple in 
self.__tables.keys() if namespace in table_name_tuple]
+        else:
+            list_tables = list(self.__tables.keys())
+
+        # Casting to make mypy happy
+        return cast(List[Tuple[Tuple[str, ...], str]], list_tables)
+
+    def list_namespaces(self) -> List[Tuple[str, ...]]:
+        return list(self.__namespaces.keys())
+
+    def load_namespace_metadata(self, namespace: Tuple[str, ...]) -> Dict[str, 
str]:
+        try:
+            return self.__namespaces[namespace]
+        except KeyError:
+            raise NamespaceNotFoundError(f"Namespace {namespace} not found in 
the catalog")
+
+    def set_namespace_metadata(self, namespace: Tuple[str, ...], metadata: 
Dict[str, str]) -> None:
+        if namespace in self.__namespaces:
+            self.__namespaces[namespace] = metadata
+        else:
+            raise NamespaceNotFoundError(f"Namespace {namespace} not found in 
the catalog")
+
+
[email protected]
+def catalog() -> InMemoryCatalog:

Review Comment:
   Moved in the most recent commit.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to