@@ -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" )
443525def 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