@@ -428,3 +428,52 @@ def test_to_dataframe_mid_stream_failure(mut, class_under_test, mock_gapic_clien
428428
429429 with pytest .raises (RuntimeError , match = "Stream format changed mid-stream" ):
430430 it .to_dataframe ()
431+
432+
433+ def test_to_arrow_delegates_to_pandas_gbq_when_installed (mut ):
434+ mock_parser = mock .Mock ()
435+ mock_message = mock .Mock ()
436+ page = mut .ReadRowsPage (mock_parser , mock_message )
437+ expected_batch = pyarrow .RecordBatch .from_arrays (
438+ [pyarrow .array ([100 ])], names = ["id" ]
439+ )
440+
441+ mock_arrow_module = mock .Mock ()
442+ mock_arrow_module .from_read_rows_response .return_value = expected_batch
443+
444+ mock_pandas_gbq = mock .Mock ()
445+ mock_pandas_gbq .arrow = mock_arrow_module
446+
447+ with mock .patch .dict (
448+ "sys.modules" ,
449+ {"pandas_gbq" : mock_pandas_gbq , "pandas_gbq.arrow" : mock_arrow_module },
450+ ):
451+ with pytest .warns (
452+ PendingDeprecationWarning ,
453+ match = "google-cloud-bigquery-storage is deprecated" ,
454+ ):
455+ actual_batch = page .to_arrow ()
456+
457+ assert actual_batch == expected_batch
458+ mock_arrow_module .from_read_rows_response .assert_called_once_with (
459+ mock_message , arrow_schema = mock_parser ._schema
460+ )
461+
462+
463+ def test_to_arrow_falls_back_when_pandas_gbq_uninstalled (mut ):
464+ mock_parser = mock .Mock ()
465+ mock_message = mock .Mock ()
466+ expected_batch = pyarrow .RecordBatch .from_arrays (
467+ [pyarrow .array ([200 ])], names = ["id" ]
468+ )
469+ mock_parser .to_arrow .return_value = expected_batch
470+ with mock .patch .dict ("sys.modules" , {"pandas_gbq" : None , "pandas_gbq.arrow" : None }):
471+ page = mut .ReadRowsPage (mock_parser , mock_message )
472+ with pytest .warns (
473+ PendingDeprecationWarning ,
474+ match = "google-cloud-bigquery-storage is deprecated" ,
475+ ):
476+ actual_batch = page .to_arrow ()
477+
478+ assert actual_batch == expected_batch
479+ mock_parser .to_arrow .assert_called_once_with (mock_message )
0 commit comments