Skip to content

Commit e140b64

Browse files
Add_maximum_bytes_billed_to_magics and test cases
1 parent 7e8feaf commit e140b64

2 files changed

Lines changed: 122 additions & 3 deletions

File tree

bigquery/google/cloud/bigquery/magics.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def __init__(self):
160160
self._credentials = None
161161
self._project = None
162162
self._use_bqstorage_api = None
163-
self._maximum_bytes_billed = None
163+
self._maximum_bytes_billed = 0
164164

165165
@property
166166
def credentials(self):
@@ -260,7 +260,7 @@ def maximum_bytes_billed(self, value):
260260
job_config = bigquery.job.QueryJobConfig()
261261
job_config.maximum_bytes_billed = self._maximum_bytes_billed
262262

263-
except:
263+
except Exception:
264264
raise ValueError("value is not a valid integer.")
265265

266266

@@ -317,6 +317,13 @@ def _run_query(client, query, job_config=None):
317317
default=None,
318318
help=("Project to use for executing this query. Defaults to the context project."),
319319
)
320+
@magic_arguments.argument(
321+
"--maximum_bytes_billed",
322+
default=None,
323+
help=(
324+
"maximum_bytes_billed to use for executing this query. Defaults to the context maximum_bytes_billed."
325+
),
326+
)
320327
@magic_arguments.argument(
321328
"--use_legacy_sql",
322329
action="store_true",
@@ -396,7 +403,17 @@ def _cell_magic(line, query):
396403
job_config = bigquery.job.QueryJobConfig()
397404
job_config.query_parameters = params
398405
job_config.use_legacy_sql = args.use_legacy_sql
399-
job_config.maximum_bytes_billed = context.maximum_bytes_billed
406+
407+
if args.maximum_bytes_billed == "None":
408+
job_config.maximum_bytes_billed = 0
409+
elif args.maximum_bytes_billed is not None:
410+
try:
411+
value = int(args.maximum_bytes_billed)
412+
job_config.maximum_bytes_billed = value
413+
except Exception:
414+
raise ValueError("value is not a valid integer.")
415+
else:
416+
job_config.maximum_bytes_billed = context.maximum_bytes_billed
400417
query_job = _run_query(client, query, job_config)
401418

402419
if not args.verbose:

bigquery/tests/unit/test_magics.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,88 @@ def test_bigquery_magic_without_bqstorage(monkeypatch):
439439
assert isinstance(return_value, pandas.DataFrame)
440440

441441

442+
@pytest.mark.usefixtures("ipython_interactive")
443+
def test_maximum_bytes_billed_w_int_magic():
444+
ip = IPython.get_ipython()
445+
ip.extension_manager.load_extension("google.cloud.bigquery")
446+
magics.context._project = None
447+
448+
bqstorage_mock = mock.create_autospec(
449+
bigquery_storage_v1beta1.BigQueryStorageClient
450+
)
451+
452+
credentials_mock = mock.create_autospec(
453+
google.auth.credentials.Credentials, instance=True
454+
)
455+
default_patch = mock.patch(
456+
"google.auth.default", return_value=(credentials_mock, "general-project")
457+
)
458+
run_query_patch = mock.patch(
459+
"google.cloud.bigquery.magics._run_query", autospec=True
460+
)
461+
462+
sql = "SELECT 17 AS num"
463+
result = pandas.DataFrame([17], columns=["num"])
464+
query_job_mock = mock.create_autospec(
465+
google.cloud.bigquery.job.QueryJob, instance=True
466+
)
467+
query_job_mock.to_dataframe.return_value = result
468+
with run_query_patch as run_query_mock, default_patch:
469+
run_query_mock.return_value = query_job_mock
470+
return_value = ip.run_cell_magic("bigquery", "--maximum_bytes_billed=123456789", sql)
471+
472+
bqstorage_mock.assert_not_called()
473+
query_job_mock.to_dataframe.assert_called_once_with(bqstorage_client=None)
474+
assert isinstance(return_value, pandas.DataFrame)
475+
476+
477+
@pytest.mark.usefixtures("ipython_interactive")
478+
def test_maximum_bytes_billed_w_string_params():
479+
ip = IPython.get_ipython()
480+
ip.extension_manager.load_extension("google.cloud.bigquery")
481+
magics.context._project = None
482+
483+
sql = "SELECT 17 AS num"
484+
485+
with pytest.raises(ValueError):
486+
ip.run_cell_magic("bigquery", "--maximum_bytes_billed=abc", sql)
487+
488+
489+
@pytest.mark.usefixtures("ipython_interactive")
490+
def test_maximum_bytes_billed_w_none__magic():
491+
ip = IPython.get_ipython()
492+
ip.extension_manager.load_extension("google.cloud.bigquery")
493+
magics.context._project = None
494+
495+
bqstorage_mock = mock.create_autospec(
496+
bigquery_storage_v1beta1.BigQueryStorageClient
497+
)
498+
499+
credentials_mock = mock.create_autospec(
500+
google.auth.credentials.Credentials, instance=True
501+
)
502+
default_patch = mock.patch(
503+
"google.auth.default", return_value=(credentials_mock, "general-project")
504+
)
505+
run_query_patch = mock.patch(
506+
"google.cloud.bigquery.magics._run_query", autospec=True
507+
)
508+
509+
sql = "SELECT 17 AS num"
510+
result = pandas.DataFrame([17], columns=["num"])
511+
query_job_mock = mock.create_autospec(
512+
google.cloud.bigquery.job.QueryJob, instance=True
513+
)
514+
query_job_mock.to_dataframe.return_value = result
515+
with run_query_patch as run_query_mock, default_patch:
516+
run_query_mock.return_value = query_job_mock
517+
return_value = ip.run_cell_magic("bigquery", "--maximum_bytes_billed=None", sql)
518+
519+
bqstorage_mock.assert_not_called()
520+
query_job_mock.to_dataframe.assert_called_once_with(bqstorage_client=None)
521+
assert isinstance(return_value, pandas.DataFrame)
522+
523+
442524
@pytest.mark.usefixtures("ipython_interactive")
443525
def test_bigquery_magic_with_project():
444526
ip = IPython.get_ipython()
@@ -543,3 +625,23 @@ def test_bigquery_magic_with_improperly_formatted_params():
543625

544626
with pytest.raises(SyntaxError):
545627
ip.run_cell_magic("bigquery", "--params {17}", sql)
628+
629+
630+
def test_maximum_bytes_billed_set_value():
631+
"""When Application Default Credentials are set, the context credentials
632+
will be created the first time it is called
633+
"""
634+
635+
from google.cloud.bigquery import QueryJobConfig
636+
job_config = QueryJobConfig()
637+
magics.context.maximum_bytes_billed = 1234567489
638+
assert job_config.maximum_bytes_billed == magics.context.maximum_bytes_billed
639+
640+
641+
def test_maximum_bytes_billed_set_string():
642+
"""When Application Default Credentials are set, the context credentials
643+
will be created the first time it is called
644+
"""
645+
with pytest.raises(ValueError):
646+
magics.context.maximum_bytes_billed = "abc"
647+

0 commit comments

Comments
 (0)