66
77import copy
88import datetime
9+ import re
910from unittest import mock
11+ import warnings
1012
1113import google .api_core .exceptions
1214import google .cloud .bigquery
15+ import google .cloud .bigquery .table
1316import numpy
1417import packaging .version
1518import pandas
1619from pandas import DataFrame
1720import pytest
1821
1922from pandas_gbq import gbq
23+ import pandas_gbq .constants
24+ import pandas_gbq .exceptions
2025import pandas_gbq .features
2126from pandas_gbq .features import FEATURES
2227
@@ -147,6 +152,62 @@ def test__transform_read_gbq_configuration_makes_copy(original, expected):
147152 assert did_change == should_change
148153
149154
155+ def test_GbqConnector_download_results_warns_for_large_tables (default_bigquery_client ):
156+ gbq ._test_google_api_imports ()
157+ connector = _make_connector ()
158+ rows_iter = mock .create_autospec (
159+ google .cloud .bigquery .table .RowIterator , instance = True
160+ )
161+ table = google .cloud .bigquery .Table .from_api_repr (
162+ {
163+ "tableReference" : {
164+ "projectId" : "my-proj" ,
165+ "datasetId" : "my-dset" ,
166+ "tableId" : "my_tbl" ,
167+ },
168+ "numBytes" : 2 * pandas_gbq .constants .BYTES_IN_GIB ,
169+ },
170+ )
171+ rows_iter ._table = table
172+ default_bigquery_client .get_table .reset_mock (side_effect = True )
173+ default_bigquery_client .get_table .return_value = table
174+
175+ with pytest .warns (
176+ pandas_gbq .exceptions .LargeResultsWarning ,
177+ match = re .escape ("Your results are 2.0 GiB. Consider using BigQuery DataFrames" ),
178+ ):
179+ connector ._download_results (rows_iter )
180+
181+
182+ def test_GbqConnector_download_results_doesnt_warn_for_small_tables (
183+ default_bigquery_client ,
184+ ):
185+ gbq ._test_google_api_imports ()
186+ connector = _make_connector ()
187+ rows_iter = mock .create_autospec (
188+ google .cloud .bigquery .table .RowIterator , instance = True
189+ )
190+ table = google .cloud .bigquery .Table .from_api_repr (
191+ {
192+ "tableReference" : {
193+ "projectId" : "my-proj" ,
194+ "datasetId" : "my-dset" ,
195+ "tableId" : "my_tbl" ,
196+ },
197+ "numBytes" : 999 * pandas_gbq .constants .BYTES_IN_MIB ,
198+ },
199+ )
200+ rows_iter ._table = table
201+ default_bigquery_client .get_table .reset_mock (side_effect = True )
202+ default_bigquery_client .get_table .return_value = table
203+
204+ with warnings .catch_warnings ():
205+ warnings .simplefilter (
206+ "error" , category = pandas_gbq .exceptions .LargeResultsWarning
207+ )
208+ connector ._download_results (rows_iter )
209+
210+
150211def test_GbqConnector_get_client_w_new_bq (mock_bigquery_client ):
151212 gbq ._test_google_api_imports ()
152213 pytest .importorskip ("google.api_core.client_info" )
@@ -191,16 +252,13 @@ def test_to_gbq_with_chunksize_warns_deprecation(
191252 api_method , warning_message , warning_type
192253):
193254 with pytest .warns (warning_type , match = warning_message ):
194- try :
195- gbq .to_gbq (
196- DataFrame ([[1 ]]),
197- "dataset.tablename" ,
198- project_id = "my-project" ,
199- api_method = api_method ,
200- chunksize = 100 ,
201- )
202- except gbq .TableCreationError :
203- pass
255+ gbq .to_gbq (
256+ DataFrame ([[1 ]]),
257+ "dataset.tablename" ,
258+ project_id = "my-project" ,
259+ api_method = api_method ,
260+ chunksize = 100 ,
261+ )
204262
205263
206264@pytest .mark .parametrize (["verbose" ], [(True ,), (False ,)])
@@ -211,15 +269,12 @@ def test_to_gbq_with_verbose_new_pandas_warns_deprecation(monkeypatch, verbose):
211269 mock .PropertyMock (return_value = True ),
212270 )
213271 with pytest .warns (FutureWarning , match = "verbose is deprecated" ):
214- try :
215- gbq .to_gbq (
216- DataFrame ([[1 ]]),
217- "dataset.tablename" ,
218- project_id = "my-project" ,
219- verbose = verbose ,
220- )
221- except gbq .TableCreationError :
222- pass
272+ gbq .to_gbq (
273+ DataFrame ([[1 ]]),
274+ "dataset.tablename" ,
275+ project_id = "my-project" ,
276+ verbose = verbose ,
277+ )
223278
224279
225280def test_to_gbq_with_private_key_raises_notimplementederror ():
@@ -233,11 +288,7 @@ def test_to_gbq_with_private_key_raises_notimplementederror():
233288
234289
235290def test_to_gbq_doesnt_run_query (mock_bigquery_client ):
236- try :
237- gbq .to_gbq (DataFrame ([[1 ]]), "dataset.tablename" , project_id = "my-project" )
238- except gbq .TableCreationError :
239- pass
240-
291+ gbq .to_gbq (DataFrame ([[1 ]]), "dataset.tablename" , project_id = "my-project" )
241292 mock_bigquery_client .query .assert_not_called ()
242293
243294
0 commit comments