1- """Integration tests for Spanner Arrow support."""
1+ """Integration tests for Spanner Arrow support.
22
3- from typing import TYPE_CHECKING , Any
3+ All operations use SQLSpec interface, not raw SDK calls.
4+ """
45
56import pytest
67
78from sqlspec ._typing import PYARROW_INSTALLED
89from sqlspec .adapters .spanner import SpannerSyncConfig
910
10- if TYPE_CHECKING :
11- from google .cloud .spanner_v1 .database import Database
12-
1311pytestmark = [pytest .mark .spanner , pytest .mark .skipif (not PYARROW_INSTALLED , reason = "pyarrow not installed" )]
1412
1513
16- def test_select_to_arrow_basic (
17- spanner_config : SpannerSyncConfig , spanner_database : "Database" , test_arrow_table : str
18- ) -> None :
14+ def test_select_to_arrow_basic (spanner_config : SpannerSyncConfig , test_arrow_table : str ) -> None :
1915 """Test basic select_to_arrow functionality."""
2016 import pyarrow as pa
2117
22- database = spanner_database
23-
24- def insert_data (transaction : "Any" ) -> None :
18+ with spanner_config .provide_write_session () as session :
2519 for i , name in enumerate (["Alice" , "Bob" , "Charlie" ], start = 1 ):
26- transaction . execute_update (
20+ session . execute (
2721 f"INSERT INTO { test_arrow_table } (id, name, value) VALUES (@id, @name, @value)" ,
28- params = {"id" : i , "name" : name , "value" : i * 10 },
29- param_types = {"id" : {"code" : "INT64" }, "name" : {"code" : "STRING" }, "value" : {"code" : "INT64" }},
22+ {"id" : i , "name" : name , "value" : i * 10 },
3023 )
3124
32- database .run_in_transaction (insert_data ) # type: ignore[no-untyped-call]
33-
3425 with spanner_config .provide_session () as session :
3526 result = session .select_to_arrow (f"SELECT * FROM { test_arrow_table } ORDER BY id" )
3627
@@ -43,28 +34,19 @@ def insert_data(transaction: "Any") -> None:
4334 assert list (df ["name" ]) == ["Alice" , "Bob" , "Charlie" ]
4435 assert list (df ["value" ]) == [10 , 20 , 30 ]
4536
46- def cleanup (transaction : "Any" ) -> None :
47- transaction .execute_update (f"DELETE FROM { test_arrow_table } WHERE TRUE" )
48-
49- database .run_in_transaction (cleanup ) # type: ignore[no-untyped-call]
37+ with spanner_config .provide_write_session () as session :
38+ session .execute (f"DELETE FROM { test_arrow_table } WHERE TRUE" )
5039
5140
52- def test_select_to_arrow_with_parameters (
53- spanner_config : SpannerSyncConfig , spanner_database : "Database" , test_arrow_table : str
54- ) -> None :
41+ def test_select_to_arrow_with_parameters (spanner_config : SpannerSyncConfig , test_arrow_table : str ) -> None :
5542 """Test select_to_arrow with parameterized query."""
56- database = spanner_database
57-
58- def insert_data (transaction : "Any" ) -> None :
43+ with spanner_config .provide_write_session () as session :
5944 for i in range (1 , 6 ):
60- transaction . execute_update (
45+ session . execute (
6146 f"INSERT INTO { test_arrow_table } (id, name, value) VALUES (@id, @name, @value)" ,
62- params = {"id" : i , "name" : f"Item { i } " , "value" : i * 100 },
63- param_types = {"id" : {"code" : "INT64" }, "name" : {"code" : "STRING" }, "value" : {"code" : "INT64" }},
47+ {"id" : i , "name" : f"Item { i } " , "value" : i * 100 },
6448 )
6549
66- database .run_in_transaction (insert_data ) # type: ignore[no-untyped-call]
67-
6850 with spanner_config .provide_session () as session :
6951 result = session .select_to_arrow (
7052 f"SELECT * FROM { test_arrow_table } WHERE value > @min_value ORDER BY id" , {"min_value" : 200 }
@@ -74,10 +56,8 @@ def insert_data(transaction: "Any") -> None:
7456 df = result .to_pandas ()
7557 assert list (df ["value" ]) == [300 , 400 , 500 ]
7658
77- def cleanup (transaction : "Any" ) -> None :
78- transaction .execute_update (f"DELETE FROM { test_arrow_table } WHERE TRUE" )
79-
80- database .run_in_transaction (cleanup ) # type: ignore[no-untyped-call]
59+ with spanner_config .provide_write_session () as session :
60+ session .execute (f"DELETE FROM { test_arrow_table } WHERE TRUE" )
8161
8262
8363def test_select_to_arrow_empty_result (spanner_config : SpannerSyncConfig , test_arrow_table : str ) -> None :
@@ -89,91 +69,65 @@ def test_select_to_arrow_empty_result(spanner_config: SpannerSyncConfig, test_ar
8969 assert len (result .to_pandas ()) == 0
9070
9171
92- def test_select_to_arrow_table_format (
93- spanner_config : SpannerSyncConfig , spanner_database : "Database" , test_arrow_table : str
94- ) -> None :
72+ def test_select_to_arrow_table_format (spanner_config : SpannerSyncConfig , test_arrow_table : str ) -> None :
9573 """Test select_to_arrow with table return format (default)."""
9674 import pyarrow as pa
9775
98- database = spanner_database
99-
100- def insert_data (transaction : "Any" ) -> None :
76+ with spanner_config .provide_write_session () as session :
10177 for i in range (1 , 4 ):
102- transaction . execute_update (
78+ session . execute (
10379 f"INSERT INTO { test_arrow_table } (id, name, value) VALUES (@id, @name, @value)" ,
104- params = {"id" : i , "name" : f"Row { i } " , "value" : i },
105- param_types = {"id" : {"code" : "INT64" }, "name" : {"code" : "STRING" }, "value" : {"code" : "INT64" }},
80+ {"id" : i , "name" : f"Row { i } " , "value" : i },
10681 )
10782
108- database .run_in_transaction (insert_data ) # type: ignore[no-untyped-call]
109-
11083 with spanner_config .provide_session () as session :
11184 result = session .select_to_arrow (f"SELECT * FROM { test_arrow_table } ORDER BY id" , return_format = "table" )
11285
11386 assert isinstance (result .data , pa .Table )
11487 assert result .rows_affected == 3
11588
116- def cleanup (transaction : "Any" ) -> None :
117- transaction .execute_update (f"DELETE FROM { test_arrow_table } WHERE TRUE" )
118-
119- database .run_in_transaction (cleanup ) # type: ignore[no-untyped-call]
89+ with spanner_config .provide_write_session () as session :
90+ session .execute (f"DELETE FROM { test_arrow_table } WHERE TRUE" )
12091
12192
122- def test_select_to_arrow_batch_format (
123- spanner_config : SpannerSyncConfig , spanner_database : "Database" , test_arrow_table : str
124- ) -> None :
93+ def test_select_to_arrow_batch_format (spanner_config : SpannerSyncConfig , test_arrow_table : str ) -> None :
12594 """Test select_to_arrow with batch return format."""
12695 import pyarrow as pa
12796
128- database = spanner_database
129-
130- def insert_data (transaction : "Any" ) -> None :
97+ with spanner_config .provide_write_session () as session :
13198 for i in range (1 , 3 ):
132- transaction . execute_update (
99+ session . execute (
133100 f"INSERT INTO { test_arrow_table } (id, name, value) VALUES (@id, @name, @value)" ,
134- params = {"id" : i , "name" : f"Batch { i } " , "value" : i * 5 },
135- param_types = {"id" : {"code" : "INT64" }, "name" : {"code" : "STRING" }, "value" : {"code" : "INT64" }},
101+ {"id" : i , "name" : f"Batch { i } " , "value" : i * 5 },
136102 )
137103
138- database .run_in_transaction (insert_data ) # type: ignore[no-untyped-call]
139-
140104 with spanner_config .provide_session () as session :
141105 result = session .select_to_arrow (f"SELECT * FROM { test_arrow_table } ORDER BY id" , return_format = "batch" )
142106
143107 assert isinstance (result .data , pa .RecordBatch )
144108 assert result .rows_affected == 2
145109
146- def cleanup ( transaction : "Any" ) -> None :
147- transaction . execute_update (f"DELETE FROM { test_arrow_table } WHERE TRUE" )
110+ with spanner_config . provide_write_session () as session :
111+ session . execute (f"DELETE FROM { test_arrow_table } WHERE TRUE" )
148112
149- database .run_in_transaction (cleanup ) # type: ignore[no-untyped-call]
150113
151-
152- def test_select_to_arrow_to_polars (
153- spanner_config : SpannerSyncConfig , spanner_database : "Database" , test_arrow_table : str
154- ) -> None :
114+ def test_select_to_arrow_to_polars (spanner_config : SpannerSyncConfig , test_arrow_table : str ) -> None :
155115 """Test select_to_arrow conversion to Polars DataFrame."""
156116 pytest .importorskip ("polars" )
157- database = spanner_database
158117
159- def insert_data ( transaction : "Any" ) -> None :
118+ with spanner_config . provide_write_session () as session :
160119 for i in range (1 , 3 ):
161- transaction . execute_update (
120+ session . execute (
162121 f"INSERT INTO { test_arrow_table } (id, name, value) VALUES (@id, @name, @value)" ,
163- params = {"id" : i , "name" : f"Polars { i } " , "value" : i * 7 },
164- param_types = {"id" : {"code" : "INT64" }, "name" : {"code" : "STRING" }, "value" : {"code" : "INT64" }},
122+ {"id" : i , "name" : f"Polars { i } " , "value" : i * 7 },
165123 )
166124
167- database .run_in_transaction (insert_data ) # type: ignore[no-untyped-call]
168-
169125 with spanner_config .provide_session () as session :
170126 result = session .select_to_arrow (f"SELECT * FROM { test_arrow_table } ORDER BY id" )
171127 df = result .to_polars ()
172128
173129 assert len (df ) == 2
174130 assert df ["name" ].to_list () == ["Polars 1" , "Polars 2" ]
175131
176- def cleanup (transaction : "Any" ) -> None :
177- transaction .execute_update (f"DELETE FROM { test_arrow_table } WHERE TRUE" )
178-
179- database .run_in_transaction (cleanup ) # type: ignore[no-untyped-call]
132+ with spanner_config .provide_write_session () as session :
133+ session .execute (f"DELETE FROM { test_arrow_table } WHERE TRUE" )
0 commit comments