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