-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_pandas_options.py
More file actions
376 lines (320 loc) · 12.5 KB
/
test_pandas_options.py
File metadata and controls
376 lines (320 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import re
import warnings
from unittest import mock
import google.api_core.exceptions
import pandas.testing
import pytest
import bigframes.exceptions
import bigframes.pandas as bpd
@pytest.mark.parametrize(
("read_method", "query_prefix"),
[
(bpd.read_gbq, None),
(bpd.read_gbq, "SELECT COUNT(1) FROM "),
(bpd.read_gbq_table, None),
(bpd.read_gbq_query, "SELECT COUNT(1) FROM "),
],
ids=[
"read_gbq-on-table-name",
"read_gbq-on-sql",
"read_gbq_table-on-table-name",
"read_gbq_query-on-sql",
],
)
def test_read_gbq_start_sets_session_location(
test_data_tables_tokyo,
dataset_id_permanent_tokyo,
tokyo_location,
test_data_tables,
dataset_id_permanent,
read_method,
query_prefix,
reset_default_session_and_location,
):
# Form query as a table name or a SQL depending on the test scenario
query_tokyo = test_data_tables_tokyo["scalars"]
query = test_data_tables["scalars"]
if query_prefix:
query_tokyo = f"{query_prefix} {query_tokyo}"
query = f"{query_prefix} {query}"
# Initially there is no location set in the bigquery options
assert not bpd.options.bigquery.location
# Starting user journey with read_gbq* should work for a table in any
# location, in this case tokyo.
with warnings.catch_warnings():
# Since the query refers to a specific location, no warning should be
# raised.
warnings.simplefilter("error", bigframes.exceptions.DefaultLocationWarning)
df = read_method(query_tokyo)
assert df is not None
# Now bigquery options location should be set to tokyo
assert bpd.options.bigquery.location == tokyo_location
# Now read_gbq* from another location should fail
with pytest.raises(
(google.api_core.exceptions.NotFound, ValueError),
match=dataset_id_permanent,
):
read_method(query)
# Close the global session to start over.
# Note: This is a thread-local operation because of the
# reset_default_session_and_location fixture above.
bpd.close_session()
# There should still be the previous location set in the bigquery options
assert bpd.options.bigquery.location == tokyo_location
# Reset the location to be able to query another location
bpd.options.bigquery.location = None
assert not bpd.options.bigquery.location
# Starting over the user journey with read_gbq* should work for a table
# in another location, in this case US
df = read_method(query)
assert df is not None
# Now bigquery options location should be set to US
assert bpd.options.bigquery.location == "US"
# Now read_gbq* from another location should fail
with pytest.raises(
(google.api_core.exceptions.NotFound, ValueError),
match=dataset_id_permanent_tokyo,
):
read_method(query_tokyo)
@pytest.mark.parametrize(
("read_method", "query_prefix"),
[
(bpd.read_gbq, None),
(bpd.read_gbq, "SELECT COUNT(1) FROM "),
(bpd.read_gbq_table, None),
(bpd.read_gbq_query, "SELECT COUNT(1) FROM "),
],
ids=[
"read_gbq-on-table-name",
"read_gbq-on-sql",
"read_gbq_table-on-table-name",
"read_gbq_query-on-sql",
],
)
def test_read_gbq_after_session_start_must_comply_with_default_location(
scalars_pandas_df_index,
test_data_tables,
test_data_tables_tokyo,
dataset_id_permanent_tokyo,
read_method,
query_prefix,
reset_default_session_and_location,
):
# Form query as a table name or a SQL depending on the test scenario
query_tokyo = test_data_tables_tokyo["scalars"]
query = test_data_tables["scalars"]
if query_prefix:
query_tokyo = f"{query_prefix} {query_tokyo}"
query = f"{query_prefix} {query}"
# Initially there is no location set in the bigquery options
assert not bpd.options.bigquery.location
# Starting user journey with anything other than read_gbq*, such as
# read_pandas would bind the session to default location US
with pytest.warns(
bigframes.exceptions.DefaultLocationWarning,
match=re.escape("using location US for the session"),
):
df = bpd.read_pandas(scalars_pandas_df_index)
assert df is not None
# Doing read_gbq* from a table in another location should fail
with pytest.raises(
(google.api_core.exceptions.NotFound, ValueError),
match=dataset_id_permanent_tokyo,
):
read_method(query_tokyo)
# read_gbq* from a table in the default location should work
df = read_method(query)
assert df is not None
@pytest.mark.parametrize(
("read_method", "query_prefix"),
[
(bpd.read_gbq, None),
(bpd.read_gbq, "SELECT COUNT(1) FROM "),
(bpd.read_gbq_table, None),
(bpd.read_gbq_query, "SELECT COUNT(1) FROM "),
],
ids=[
"read_gbq-on-table-name",
"read_gbq-on-sql",
"read_gbq_table-on-table-name",
"read_gbq_query-on-sql",
],
)
def test_read_gbq_must_comply_with_set_location_US(
test_data_tables,
test_data_tables_tokyo,
dataset_id_permanent_tokyo,
read_method,
query_prefix,
reset_default_session_and_location,
):
# Form query as a table name or a SQL depending on the test scenario
query_tokyo = test_data_tables_tokyo["scalars"]
query = test_data_tables["scalars"]
if query_prefix:
query_tokyo = f"{query_prefix} {query_tokyo}"
query = f"{query_prefix} {query}"
# Initially there is no location set in the bigquery options
assert not bpd.options.bigquery.location
# Explicitly set location
bpd.options.bigquery.location = "US"
assert bpd.options.bigquery.location == "US"
# Starting user journey with read_gbq* from another location should fail
with pytest.raises(
(google.api_core.exceptions.NotFound, ValueError),
match=dataset_id_permanent_tokyo,
):
read_method(query_tokyo)
# Starting user journey with read_gbq* should work for a table in the same
# location, in this case tokyo
df = read_method(query)
assert df is not None
@pytest.mark.parametrize(
("read_method", "query_prefix"),
[
(bpd.read_gbq, None),
(bpd.read_gbq, "SELECT COUNT(1) FROM "),
(bpd.read_gbq_table, None),
(bpd.read_gbq_query, "SELECT COUNT(1) FROM "),
],
ids=[
"read_gbq-on-table-name",
"read_gbq-on-sql",
"read_gbq_table-on-table-name",
"read_gbq_query-on-sql",
],
)
def test_read_gbq_must_comply_with_set_location_non_US(
tokyo_location,
test_data_tables,
test_data_tables_tokyo,
dataset_id_permanent,
read_method,
query_prefix,
reset_default_session_and_location,
):
# Form query as a table name or a SQL depending on the test scenario
query_tokyo = test_data_tables_tokyo["scalars"]
query = test_data_tables["scalars"]
if query_prefix:
query_tokyo = f"{query_prefix} {query_tokyo}"
query = f"{query_prefix} {query}"
# Initially there is no location set in the bigquery options
assert not bpd.options.bigquery.location
# Explicitly set location
bpd.options.bigquery.location = tokyo_location
assert bpd.options.bigquery.location == tokyo_location
# Starting user journey with read_gbq* from another location should fail
with pytest.raises(
(google.api_core.exceptions.NotFound, ValueError),
match=dataset_id_permanent,
):
read_method(query)
# Starting user journey with read_gbq* should work for a table in the same
# location, in this case tokyo
df = read_method(query_tokyo)
assert df is not None
def test_credentials_need_reauthentication(
monkeypatch, reset_default_session_and_location
):
# Use a simple test query to verify that default session works to interact
# with BQ.
test_query = "SELECT 1"
# Confirm that default session works as usual
df = bpd.read_gbq(test_query)
assert df is not None
# Call get_global_session() *after* read_gbq so that our location detection
# has a chance to work.
session = bpd.get_global_session()
assert session.bqclient._http.credentials.valid
# We look at the thread-local session because of the
# reset_default_session_and_location fixture and that this test mutates
# state that might otherwise be used by tests running in parallel.
current_session = (
bigframes.core.global_session._global_session_state.thread_local_session
)
assert current_session is not None
# Force a temp table to be created, so there is something to cleanup.
current_session._anon_dataset_manager.create_temp_table(schema=())
with monkeypatch.context() as m:
# Simulate expired credentials to trigger the credential refresh flow
m.setattr(
session.bqclient._http.credentials, "expiry", datetime.datetime.utcnow()
)
assert not session.bqclient._http.credentials.valid
# Simulate an exception during the credential refresh flow
m.setattr(
session.bqclient._http.credentials,
"refresh",
mock.Mock(side_effect=google.auth.exceptions.RefreshError()),
)
# Confirm that session is unusable to run any jobs
with pytest.raises(google.auth.exceptions.RefreshError):
query_job = session.bqclient.query(test_query)
query_job.result() # blocks until finished
# Confirm that as a result bigframes.pandas interface is unusable
with pytest.raises(google.auth.exceptions.RefreshError):
bpd.read_gbq(test_query)
with warnings.catch_warnings(record=True) as warned:
bpd.close_session() # CleanupFailedWarning: can't clean up
# The test forces a failure during cleanup and asserts that one or more warning is generated
# when/if multiple temp tables might have been left over.
assert len(warned) >= 1
assert warned[0].category == bigframes.exceptions.CleanupFailedWarning
assert (
bigframes.core.global_session._global_session_state.thread_local_session
is None
)
# Now verify that use is able to start over
df = bpd.read_gbq(test_query)
assert df is not None
def test_max_rows_normal_execution_within_limit(
scalars_df_index, scalars_pandas_df_index
):
"""Test queries execute normally when the number of rows is within the limit."""
with bpd.option_context("compute.maximum_result_rows", 10):
df = scalars_df_index.head(10)
result = df.to_pandas()
expected = scalars_pandas_df_index.head(10)
pandas.testing.assert_frame_equal(result, expected)
with (
bpd.option_context("compute.maximum_result_rows", 10),
bpd.option_context("display.repr_mode", "head"),
):
df = scalars_df_index.head(10)
assert repr(df) is not None
# We should be able to get away with only a single row for shape.
with bpd.option_context("compute.maximum_result_rows", 1):
shape = scalars_df_index.shape
assert shape == scalars_pandas_df_index.shape
# 0 is not recommended, as it would stop aggregations and many other
# necessary operations, but we shouldn't need even 1 row for to_gbq().
with bpd.option_context("compute.maximum_result_rows", 0):
destination = scalars_df_index.to_gbq()
assert destination is not None
def test_max_rows_exceeds_limit(scalars_df_index):
"""Test to_pandas() raises MaximumRowsDownloadedExceeded when the limit is exceeded."""
with (
bpd.option_context("compute.maximum_result_rows", 5),
pytest.raises(bigframes.exceptions.MaximumResultRowsExceeded, match="5"),
):
scalars_df_index.to_pandas()
with (
bpd.option_context("compute.maximum_result_rows", 5),
pytest.raises(bigframes.exceptions.MaximumResultRowsExceeded, match="5"),
):
next(iter(scalars_df_index.to_pandas_batches()))