1919from pathlib import Path
2020from typing import Generator , List
2121
22+ import pyarrow as pa
2223import pytest
2324from pytest import TempPathFactory
2425from pytest_lazyfixture import lazy_fixture
3536 NoSuchTableError ,
3637 TableAlreadyExistsError ,
3738)
39+ from pyiceberg .io import FSSPEC_FILE_IO , PY_IO_IMPL
40+ from pyiceberg .io .pyarrow import schema_to_pyarrow
3841from pyiceberg .schema import Schema
42+ from pyiceberg .table .snapshots import Operation
3943from pyiceberg .table .sorting import (
4044 NullOrder ,
4145 SortDirection ,
@@ -80,7 +84,7 @@ def catalog_memory(warehouse: Path) -> Generator[SqlCatalog, None, None]:
8084@pytest .fixture (scope = "module" )
8185def catalog_sqlite (warehouse : Path ) -> Generator [SqlCatalog , None , None ]:
8286 props = {
83- "uri" : "sqlite:////tmp /sql-catalog.db" ,
87+ "uri" : f "sqlite:////{ warehouse } /sql-catalog.db" ,
8488 "warehouse" : f"file://{ warehouse } " ,
8589 }
8690 catalog = SqlCatalog ("test_sql_catalog" , ** props )
@@ -92,7 +96,7 @@ def catalog_sqlite(warehouse: Path) -> Generator[SqlCatalog, None, None]:
9296@pytest .fixture (scope = "module" )
9397def catalog_sqlite_without_rowcount (warehouse : Path ) -> Generator [SqlCatalog , None , None ]:
9498 props = {
95- "uri" : "sqlite:////tmp /sql-catalog.db" ,
99+ "uri" : f "sqlite:////{ warehouse } /sql-catalog.db" ,
96100 "warehouse" : f"file://{ warehouse } " ,
97101 }
98102 catalog = SqlCatalog ("test_sql_catalog" , ** props )
@@ -102,6 +106,19 @@ def catalog_sqlite_without_rowcount(warehouse: Path) -> Generator[SqlCatalog, No
102106 catalog .destroy_tables ()
103107
104108
109+ @pytest .fixture (scope = "module" )
110+ def catalog_sqlite_fsspec (warehouse : Path ) -> Generator [SqlCatalog , None , None ]:
111+ props = {
112+ "uri" : f"sqlite:////{ warehouse } /sql-catalog.db" ,
113+ "warehouse" : f"file://{ warehouse } " ,
114+ PY_IO_IMPL : FSSPEC_FILE_IO ,
115+ }
116+ catalog = SqlCatalog ("test_sql_catalog" , ** props )
117+ catalog .create_tables ()
118+ yield catalog
119+ catalog .destroy_tables ()
120+
121+
105122def test_creation_with_no_uri () -> None :
106123 with pytest .raises (NoSuchPropertyException ):
107124 SqlCatalog ("test_ddb_catalog" , not_uri = "unused" )
@@ -722,6 +739,47 @@ def test_commit_table(catalog: SqlCatalog, table_schema_nested: Schema, random_i
722739 assert new_schema .find_field ("b" ).field_type == IntegerType ()
723740
724741
742+ @pytest .mark .parametrize (
743+ 'catalog' ,
744+ [
745+ lazy_fixture ('catalog_memory' ),
746+ lazy_fixture ('catalog_sqlite' ),
747+ lazy_fixture ('catalog_sqlite_without_rowcount' ),
748+ lazy_fixture ('catalog_sqlite_fsspec' ),
749+ ],
750+ )
751+ def test_append_table (catalog : SqlCatalog , table_schema_simple : Schema , random_identifier : Identifier ) -> None :
752+ database_name , _table_name = random_identifier
753+ catalog .create_namespace (database_name )
754+ table = catalog .create_table (random_identifier , table_schema_simple )
755+
756+ df = pa .Table .from_pydict (
757+ {
758+ "foo" : ["a" ],
759+ "bar" : [1 ],
760+ "baz" : [True ],
761+ },
762+ schema = schema_to_pyarrow (table_schema_simple ),
763+ )
764+
765+ table .append (df )
766+
767+ # new snapshot is written in APPEND mode
768+ assert len (table .metadata .snapshots ) == 1
769+ assert table .metadata .snapshots [0 ].snapshot_id == table .metadata .current_snapshot_id
770+ assert table .metadata .snapshots [0 ].parent_snapshot_id is None
771+ assert table .metadata .snapshots [0 ].sequence_number == 1
772+ assert table .metadata .snapshots [0 ].summary is not None
773+ assert table .metadata .snapshots [0 ].summary .operation == Operation .APPEND
774+ assert table .metadata .snapshots [0 ].summary ['added-data-files' ] == '1'
775+ assert table .metadata .snapshots [0 ].summary ['added-records' ] == '1'
776+ assert table .metadata .snapshots [0 ].summary ['total-data-files' ] == '1'
777+ assert table .metadata .snapshots [0 ].summary ['total-records' ] == '1'
778+
779+ # read back the data
780+ assert df == table .scan ().to_arrow ()
781+
782+
725783@pytest .mark .parametrize (
726784 'catalog' ,
727785 [
0 commit comments