1515import copy
1616import re
1717from concurrent import futures
18+ import warnings
1819
1920import mock
2021import pytest
@@ -386,13 +387,33 @@ def test_extension_load():
386387
387388@pytest .mark .usefixtures ("ipython_interactive" )
388389@pytest .mark .skipif (pandas is None , reason = "Requires `pandas`" )
389- def test_bigquery_magic_without_optional_arguments (missing_bq_storage ):
390+ @pytest .mark .skipif (
391+ bigquery_storage_v1beta1 is None , reason = "Requires `google-cloud-bigquery-storage`"
392+ )
393+ def test_bigquery_magic_without_optional_arguments (monkeypatch ):
390394 ip = IPython .get_ipython ()
391395 ip .extension_manager .load_extension ("google.cloud.bigquery" )
392- magics . context . credentials = mock .create_autospec (
396+ mock_credentials = mock .create_autospec (
393397 google .auth .credentials .Credentials , instance = True
394398 )
395399
400+ # Set up the context with monkeypatch so that it's reset for subsequent
401+ # tests.
402+ monkeypatch .setattr (magics .context , "credentials" , mock_credentials )
403+
404+ # Mock out the BigQuery Storage API.
405+ bqstorage_mock = mock .create_autospec (
406+ bigquery_storage_v1beta1 .BigQueryStorageClient
407+ )
408+ bqstorage_instance_mock = mock .create_autospec (
409+ bigquery_storage_v1beta1 .BigQueryStorageClient , instance = True
410+ )
411+ bqstorage_instance_mock .transport = mock .Mock ()
412+ bqstorage_mock .return_value = bqstorage_instance_mock
413+ bqstorage_client_patch = mock .patch (
414+ "google.cloud.bigquery_storage_v1beta1.BigQueryStorageClient" , bqstorage_mock
415+ )
416+
396417 sql = "SELECT 17 AS num"
397418 result = pandas .DataFrame ([17 ], columns = ["num" ])
398419 run_query_patch = mock .patch (
@@ -403,11 +424,11 @@ def test_bigquery_magic_without_optional_arguments(missing_bq_storage):
403424 )
404425 query_job_mock .to_dataframe .return_value = result
405426
406- # Shouldn't fail when BigQuery Storage client isn't installed.
407- with run_query_patch as run_query_mock , missing_bq_storage :
427+ with run_query_patch as run_query_mock , bqstorage_client_patch :
408428 run_query_mock .return_value = query_job_mock
409429 return_value = ip .run_cell_magic ("bigquery" , "" , sql )
410430
431+ assert bqstorage_mock .called # BQ storage client was used
411432 assert isinstance (return_value , pandas .DataFrame )
412433 assert len (return_value ) == len (result ) # verify row count
413434 assert list (return_value ) == list (result ) # verify column names
@@ -542,7 +563,6 @@ def test_bigquery_magic_with_bqstorage_from_argument(monkeypatch):
542563 # Set up the context with monkeypatch so that it's reset for subsequent
543564 # tests.
544565 monkeypatch .setattr (magics .context , "credentials" , mock_credentials )
545- monkeypatch .setattr (magics .context , "use_bqstorage_api" , False )
546566
547567 # Mock out the BigQuery Storage API.
548568 bqstorage_mock = mock .create_autospec (
@@ -566,67 +586,20 @@ def test_bigquery_magic_with_bqstorage_from_argument(monkeypatch):
566586 google .cloud .bigquery .job .QueryJob , instance = True
567587 )
568588 query_job_mock .to_dataframe .return_value = result
569- with run_query_patch as run_query_mock , bqstorage_client_patch :
589+ with run_query_patch as run_query_mock , bqstorage_client_patch , warnings .catch_warnings (
590+ record = True
591+ ) as warned :
570592 run_query_mock .return_value = query_job_mock
571593
572594 return_value = ip .run_cell_magic ("bigquery" , "--use_bqstorage_api" , sql )
573595
574- assert len (bqstorage_mock .call_args_list ) == 1
575- kwargs = bqstorage_mock .call_args_list [0 ].kwargs
576- assert kwargs .get ("credentials" ) is mock_credentials
577- client_info = kwargs .get ("client_info" )
578- assert client_info is not None
579- assert client_info .user_agent == "ipython-" + IPython .__version__
580-
581- query_job_mock .to_dataframe .assert_called_once_with (
582- bqstorage_client = bqstorage_instance_mock
583- )
584-
585- assert isinstance (return_value , pandas .DataFrame )
586-
587-
588- @pytest .mark .usefixtures ("ipython_interactive" )
589- @pytest .mark .skipif (
590- bigquery_storage_v1beta1 is None , reason = "Requires `google-cloud-bigquery-storage`"
591- )
592- def test_bigquery_magic_with_bqstorage_from_context (monkeypatch ):
593- ip = IPython .get_ipython ()
594- ip .extension_manager .load_extension ("google.cloud.bigquery" )
595- mock_credentials = mock .create_autospec (
596- google .auth .credentials .Credentials , instance = True
597- )
598-
599- # Set up the context with monkeypatch so that it's reset for subsequent
600- # tests.
601- monkeypatch .setattr (magics .context , "credentials" , mock_credentials )
602- monkeypatch .setattr (magics .context , "use_bqstorage_api" , True )
596+ # Deprecation warning should have been issued.
597+ def warning_match (warning ):
598+ message = str (warning ).lower ()
599+ return "deprecated" in message and "use_bqstorage_api" in message
603600
604- # Mock out the BigQuery Storage API.
605- bqstorage_mock = mock .create_autospec (
606- bigquery_storage_v1beta1 .BigQueryStorageClient
607- )
608- bqstorage_instance_mock = mock .create_autospec (
609- bigquery_storage_v1beta1 .BigQueryStorageClient , instance = True
610- )
611- bqstorage_instance_mock .transport = mock .Mock ()
612- bqstorage_mock .return_value = bqstorage_instance_mock
613- bqstorage_client_patch = mock .patch (
614- "google.cloud.bigquery_storage_v1beta1.BigQueryStorageClient" , bqstorage_mock
615- )
616-
617- sql = "SELECT 17 AS num"
618- result = pandas .DataFrame ([17 ], columns = ["num" ])
619- run_query_patch = mock .patch (
620- "google.cloud.bigquery.magics._run_query" , autospec = True
621- )
622- query_job_mock = mock .create_autospec (
623- google .cloud .bigquery .job .QueryJob , instance = True
624- )
625- query_job_mock .to_dataframe .return_value = result
626- with run_query_patch as run_query_mock , bqstorage_client_patch :
627- run_query_mock .return_value = query_job_mock
628-
629- return_value = ip .run_cell_magic ("bigquery" , "" , sql )
601+ expected_warnings = list (filter (warning_match , warned ))
602+ assert len (expected_warnings ) == 1
630603
631604 assert len (bqstorage_mock .call_args_list ) == 1
632605 kwargs = bqstorage_mock .call_args_list [0 ].kwargs
@@ -646,7 +619,7 @@ def test_bigquery_magic_with_bqstorage_from_context(monkeypatch):
646619@pytest .mark .skipif (
647620 bigquery_storage_v1beta1 is None , reason = "Requires `google-cloud-bigquery-storage`"
648621)
649- def test_bigquery_magic_without_bqstorage (monkeypatch ):
622+ def test_bigquery_magic_with_rest_client_requested (monkeypatch ):
650623 ip = IPython .get_ipython ()
651624 ip .extension_manager .load_extension ("google.cloud.bigquery" )
652625 mock_credentials = mock .create_autospec (
@@ -677,7 +650,7 @@ def test_bigquery_magic_without_bqstorage(monkeypatch):
677650 with run_query_patch as run_query_mock , bqstorage_client_patch :
678651 run_query_mock .return_value = query_job_mock
679652
680- return_value = ip .run_cell_magic ("bigquery" , "" , sql )
653+ return_value = ip .run_cell_magic ("bigquery" , "--use_rest_api " , sql )
681654
682655 bqstorage_mock .assert_not_called ()
683656 query_job_mock .to_dataframe .assert_called_once_with (bqstorage_client = None )
@@ -895,7 +868,7 @@ def test_bigquery_magic_w_table_id_and_bqstorage_client():
895868 with default_patch , client_patch as client_mock , bqstorage_client_patch :
896869 client_mock ().list_rows .return_value = row_iterator_mock
897870
898- ip .run_cell_magic ("bigquery" , "--use_bqstorage_api -- max_results=5" , table_id )
871+ ip .run_cell_magic ("bigquery" , "--max_results=5" , table_id )
899872 row_iterator_mock .to_dataframe .assert_called_once_with (
900873 bqstorage_client = bqstorage_instance_mock
901874 )
0 commit comments