6
6
7
7
import copy
8
8
import datetime
9
+ import re
9
10
from unittest import mock
11
+ import warnings
10
12
11
13
import google .api_core .exceptions
12
14
import google .cloud .bigquery
15
+ import google .cloud .bigquery .table
13
16
import numpy
14
17
import packaging .version
15
18
import pandas
16
19
from pandas import DataFrame
17
20
import pytest
18
21
19
22
from pandas_gbq import gbq
23
+ import pandas_gbq .constants
24
+ import pandas_gbq .exceptions
20
25
import pandas_gbq .features
21
26
from pandas_gbq .features import FEATURES
22
27
@@ -147,6 +152,62 @@ def test__transform_read_gbq_configuration_makes_copy(original, expected):
147
152
assert did_change == should_change
148
153
149
154
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
+
150
211
def test_GbqConnector_get_client_w_new_bq (mock_bigquery_client ):
151
212
gbq ._test_google_api_imports ()
152
213
pytest .importorskip ("google.api_core.client_info" )
@@ -191,16 +252,13 @@ def test_to_gbq_with_chunksize_warns_deprecation(
191
252
api_method , warning_message , warning_type
192
253
):
193
254
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
+ )
204
262
205
263
206
264
@pytest .mark .parametrize (["verbose" ], [(True ,), (False ,)])
@@ -211,15 +269,12 @@ def test_to_gbq_with_verbose_new_pandas_warns_deprecation(monkeypatch, verbose):
211
269
mock .PropertyMock (return_value = True ),
212
270
)
213
271
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
+ )
223
278
224
279
225
280
def test_to_gbq_with_private_key_raises_notimplementederror ():
@@ -233,11 +288,7 @@ def test_to_gbq_with_private_key_raises_notimplementederror():
233
288
234
289
235
290
def 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" )
241
292
mock_bigquery_client .query .assert_not_called ()
242
293
243
294
0 commit comments