Skip to content

Commit 629d34b

Browse files
HemangChothanitswast
authored andcommitted
Add_maximum_bytes_billed_to_magics and test cases
1 parent 7b843d0 commit 629d34b

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
@@ -161,7 +161,7 @@ def __init__(self):
161161
self._project = None
162162
self._connection = None
163163
self._use_bqstorage_api = None
164-
self._maximum_bytes_billed = None
164+
self._maximum_bytes_billed = 0
165165

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

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

267267

@@ -318,6 +318,13 @@ def _run_query(client, query, job_config=None):
318318
default=None,
319319
help=("Project to use for executing this query. Defaults to the context project."),
320320
)
321+
@magic_arguments.argument(
322+
"--maximum_bytes_billed",
323+
default=None,
324+
help=(
325+
"maximum_bytes_billed to use for executing this query. Defaults to the context maximum_bytes_billed."
326+
),
327+
)
321328
@magic_arguments.argument(
322329
"--use_legacy_sql",
323330
action="store_true",
@@ -399,7 +406,17 @@ def _cell_magic(line, query):
399406
job_config = bigquery.job.QueryJobConfig()
400407
job_config.query_parameters = params
401408
job_config.use_legacy_sql = args.use_legacy_sql
402-
job_config.maximum_bytes_billed = context.maximum_bytes_billed
409+
410+
if args.maximum_bytes_billed == "None":
411+
job_config.maximum_bytes_billed = 0
412+
elif args.maximum_bytes_billed is not None:
413+
try:
414+
value = int(args.maximum_bytes_billed)
415+
job_config.maximum_bytes_billed = value
416+
except Exception:
417+
raise ValueError("value is not a valid integer.")
418+
else:
419+
job_config.maximum_bytes_billed = context.maximum_bytes_billed
403420
query_job = _run_query(client, query, job_config)
404421

405422
if not args.verbose:

bigquery/tests/unit/test_magics.py

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

550550

551+
@pytest.mark.usefixtures("ipython_interactive")
552+
def test_maximum_bytes_billed_w_int_magic():
553+
ip = IPython.get_ipython()
554+
ip.extension_manager.load_extension("google.cloud.bigquery")
555+
magics.context._project = None
556+
557+
bqstorage_mock = mock.create_autospec(
558+
bigquery_storage_v1beta1.BigQueryStorageClient
559+
)
560+
561+
credentials_mock = mock.create_autospec(
562+
google.auth.credentials.Credentials, instance=True
563+
)
564+
default_patch = mock.patch(
565+
"google.auth.default", return_value=(credentials_mock, "general-project")
566+
)
567+
run_query_patch = mock.patch(
568+
"google.cloud.bigquery.magics._run_query", autospec=True
569+
)
570+
571+
sql = "SELECT 17 AS num"
572+
result = pandas.DataFrame([17], columns=["num"])
573+
query_job_mock = mock.create_autospec(
574+
google.cloud.bigquery.job.QueryJob, instance=True
575+
)
576+
query_job_mock.to_dataframe.return_value = result
577+
with run_query_patch as run_query_mock, default_patch:
578+
run_query_mock.return_value = query_job_mock
579+
return_value = ip.run_cell_magic("bigquery", "--maximum_bytes_billed=123456789", sql)
580+
581+
bqstorage_mock.assert_not_called()
582+
query_job_mock.to_dataframe.assert_called_once_with(bqstorage_client=None)
583+
assert isinstance(return_value, pandas.DataFrame)
584+
585+
586+
@pytest.mark.usefixtures("ipython_interactive")
587+
def test_maximum_bytes_billed_w_string_params():
588+
ip = IPython.get_ipython()
589+
ip.extension_manager.load_extension("google.cloud.bigquery")
590+
magics.context._project = None
591+
592+
sql = "SELECT 17 AS num"
593+
594+
with pytest.raises(ValueError):
595+
ip.run_cell_magic("bigquery", "--maximum_bytes_billed=abc", sql)
596+
597+
598+
@pytest.mark.usefixtures("ipython_interactive")
599+
def test_maximum_bytes_billed_w_none__magic():
600+
ip = IPython.get_ipython()
601+
ip.extension_manager.load_extension("google.cloud.bigquery")
602+
magics.context._project = None
603+
604+
bqstorage_mock = mock.create_autospec(
605+
bigquery_storage_v1beta1.BigQueryStorageClient
606+
)
607+
608+
credentials_mock = mock.create_autospec(
609+
google.auth.credentials.Credentials, instance=True
610+
)
611+
default_patch = mock.patch(
612+
"google.auth.default", return_value=(credentials_mock, "general-project")
613+
)
614+
run_query_patch = mock.patch(
615+
"google.cloud.bigquery.magics._run_query", autospec=True
616+
)
617+
618+
sql = "SELECT 17 AS num"
619+
result = pandas.DataFrame([17], columns=["num"])
620+
query_job_mock = mock.create_autospec(
621+
google.cloud.bigquery.job.QueryJob, instance=True
622+
)
623+
query_job_mock.to_dataframe.return_value = result
624+
with run_query_patch as run_query_mock, default_patch:
625+
run_query_mock.return_value = query_job_mock
626+
return_value = ip.run_cell_magic("bigquery", "--maximum_bytes_billed=None", sql)
627+
628+
bqstorage_mock.assert_not_called()
629+
query_job_mock.to_dataframe.assert_called_once_with(bqstorage_client=None)
630+
assert isinstance(return_value, pandas.DataFrame)
631+
632+
551633
@pytest.mark.usefixtures("ipython_interactive")
552634
def test_bigquery_magic_with_project():
553635
ip = IPython.get_ipython()
@@ -652,3 +734,23 @@ def test_bigquery_magic_with_improperly_formatted_params():
652734

653735
with pytest.raises(SyntaxError):
654736
ip.run_cell_magic("bigquery", "--params {17}", sql)
737+
738+
739+
def test_maximum_bytes_billed_set_value():
740+
"""When Application Default Credentials are set, the context credentials
741+
will be created the first time it is called
742+
"""
743+
744+
from google.cloud.bigquery import QueryJobConfig
745+
job_config = QueryJobConfig()
746+
magics.context.maximum_bytes_billed = 1234567489
747+
assert job_config.maximum_bytes_billed == magics.context.maximum_bytes_billed
748+
749+
750+
def test_maximum_bytes_billed_set_string():
751+
"""When Application Default Credentials are set, the context credentials
752+
will be created the first time it is called
753+
"""
754+
with pytest.raises(ValueError):
755+
magics.context.maximum_bytes_billed = "abc"
756+

0 commit comments

Comments
 (0)