|
60 | 60 | ) |
61 | 61 | from pyiceberg.transforms import IdentityTransform |
62 | 62 | from pyiceberg.typedef import Identifier |
63 | | -from pyiceberg.types import IntegerType, strtobool |
| 63 | +from pyiceberg.types import IntegerType, NestedField, StringType, strtobool |
64 | 64 |
|
65 | 65 | CATALOG_TABLES = [c.__tablename__ for c in SqlCatalogBaseTable.__subclasses__()] |
66 | 66 |
|
@@ -1704,3 +1704,56 @@ def test_delete_metadata_multiple(catalog: SqlCatalog, table_schema_nested: Sche |
1704 | 1704 | assert not os.path.exists(original_metadata_location[len("file://") :]) |
1705 | 1705 | assert not os.path.exists(updated_metadata_1.metadata_file[len("file://") :]) |
1706 | 1706 | assert os.path.exists(updated_metadata_2.metadata_file[len("file://") :]) |
| 1707 | + |
| 1708 | + |
| 1709 | +class TestSqlCatalogClose: |
| 1710 | + """Test SqlCatalog close functionality.""" |
| 1711 | + |
| 1712 | + def test_sql_catalog_close(self, catalog_sqlite: SqlCatalog) -> None: |
| 1713 | + """Test that SqlCatalog close method properly disposes the engine.""" |
| 1714 | + # Verify engine exists |
| 1715 | + assert hasattr(catalog_sqlite, "engine") |
| 1716 | + |
| 1717 | + # Close the catalog |
| 1718 | + catalog_sqlite.close() |
| 1719 | + |
| 1720 | + # Verify engine is disposed by checking that the engine still exists |
| 1721 | + assert hasattr(catalog_sqlite, "engine") |
| 1722 | + |
| 1723 | + def test_sql_catalog_context_manager(self, warehouse: Path) -> None: |
| 1724 | + """Test that SqlCatalog works as a context manager.""" |
| 1725 | + with SqlCatalog("test", uri="sqlite:///:memory:", warehouse=str(warehouse)) as catalog: |
| 1726 | + # Verify engine exists |
| 1727 | + assert hasattr(catalog, "engine") |
| 1728 | + |
| 1729 | + # Create a namespace and table to test functionality |
| 1730 | + catalog.create_namespace("test_db") |
| 1731 | + schema = Schema(NestedField(1, "name", StringType(), required=True)) |
| 1732 | + catalog.create_table(("test_db", "test_table"), schema) |
| 1733 | + |
| 1734 | + # Verify engine is disposed after exiting context |
| 1735 | + assert hasattr(catalog, "engine") |
| 1736 | + |
| 1737 | + def test_sql_catalog_context_manager_with_exception(self) -> None: |
| 1738 | + """Test that SqlCatalog context manager properly closes even with exceptions.""" |
| 1739 | + catalog = None |
| 1740 | + try: |
| 1741 | + with SqlCatalog("test", uri="sqlite:///:memory:") as cat: |
| 1742 | + catalog = cat |
| 1743 | + # Verify engine exists |
| 1744 | + assert hasattr(catalog, "engine") |
| 1745 | + raise ValueError("Test exception") |
| 1746 | + except ValueError: |
| 1747 | + pass |
| 1748 | + |
| 1749 | + # Verify engine is disposed even after exception |
| 1750 | + assert catalog is not None |
| 1751 | + assert hasattr(catalog, "engine") |
| 1752 | + |
| 1753 | + def test_sql_catalog_multiple_close_calls(self, catalog_sqlite: SqlCatalog) -> None: |
| 1754 | + """Test that multiple close calls on SqlCatalog are safe.""" |
| 1755 | + # First close |
| 1756 | + catalog_sqlite.close() |
| 1757 | + |
| 1758 | + # Second close should not raise an exception |
| 1759 | + catalog_sqlite.close() |
0 commit comments