22
33import pandas as pd # noqa: TID253
44import pytest
5+ from pytest_mock .plugin import MockerFixture
56from sqlglot import expressions as exp
67from sqlglot import parse_one
7-
88from sqlmesh .core .engine_adapter import DuckDBEngineAdapter , EngineAdapter
99from tests .core .engine_adapter import to_sql_calls
1010
@@ -77,9 +77,12 @@ def test_set_current_catalog(make_mocked_engine_adapter: t.Callable, duck_conn):
7777 ]
7878
7979
80- def test_temporary_table (make_mocked_engine_adapter : t .Callable , duck_conn ):
80+ def test_temporary_table (make_mocked_engine_adapter : t .Callable , mocker : MockerFixture ):
8181 adapter = make_mocked_engine_adapter (DuckDBEngineAdapter )
8282
83+ mocker .patch .object (adapter , "get_current_catalog" , return_value = "test_catalog" )
84+ mocker .patch .object (adapter , "fetchone" , return_value = ("test_catalog" ,))
85+
8386 adapter .create_table (
8487 "test_table" ,
8588 {"a" : exp .DataType .build ("INT" ), "b" : exp .DataType .build ("INT" )},
@@ -103,3 +106,33 @@ def test_drop_catalog(make_mocked_engine_adapter: t.Callable) -> None:
103106 adapter .drop_catalog (exp .to_identifier ("foo" ))
104107
105108 assert to_sql_calls (adapter ) == ['DETACH DATABASE IF EXISTS "foo"' ]
109+
110+
111+ def test_ducklake_partitioning (adapter : EngineAdapter , duck_conn , tmp_path ):
112+ catalog = "a_ducklake_db"
113+
114+ duck_conn .install_extension ("ducklake" )
115+ duck_conn .load_extension ("ducklake" )
116+ duck_conn .execute (
117+ f"ATTACH 'ducklake:{ catalog } .ducklake' AS { catalog } (DATA_PATH '{ tmp_path } ');"
118+ )
119+
120+ # no partitions on catalog creation
121+ partition_info = duck_conn .execute (
122+ f"SELECT * FROM __ducklake_metadata_{ catalog } .main.ducklake_partition_info"
123+ ).fetchdf ()
124+ assert partition_info .empty
125+
126+ adapter .set_current_catalog (catalog )
127+ adapter .create_schema ("test_schema" )
128+ adapter .create_table (
129+ "test_schema.test_table" ,
130+ {"a" : exp .DataType .build ("INT" ), "b" : exp .DataType .build ("INT" )},
131+ partitioned_by = [exp .to_column ("a" ), exp .to_column ("b" )],
132+ )
133+
134+ # 1 partition after table creation
135+ partition_info = duck_conn .execute (
136+ f"SELECT * FROM __ducklake_metadata_{ catalog } .main.ducklake_partition_info"
137+ ).fetchdf ()
138+ assert partition_info .shape [0 ] == 1
0 commit comments