@@ -623,7 +623,7 @@ def warning_match(warning):
623
623
assert client_info .user_agent == "ipython-" + IPython .__version__
624
624
625
625
query_job_mock .to_dataframe .assert_called_once_with (
626
- bqstorage_client = bqstorage_instance_mock
626
+ bqstorage_client = bqstorage_instance_mock , progress_bar_type = "tqdm"
627
627
)
628
628
629
629
assert isinstance (return_value , pandas .DataFrame )
@@ -665,7 +665,9 @@ def test_bigquery_magic_with_rest_client_requested(monkeypatch):
665
665
return_value = ip .run_cell_magic ("bigquery" , "--use_rest_api" , sql )
666
666
667
667
bqstorage_mock .assert_not_called ()
668
- query_job_mock .to_dataframe .assert_called_once_with (bqstorage_client = None )
668
+ query_job_mock .to_dataframe .assert_called_once_with (
669
+ bqstorage_client = None , progress_bar_type = "tqdm"
670
+ )
669
671
670
672
assert isinstance (return_value , pandas .DataFrame )
671
673
@@ -1167,6 +1169,71 @@ def test_bigquery_magic_w_maximum_bytes_billed_w_context_setter():
1167
1169
assert sent_config ["maximumBytesBilled" ] == "10203"
1168
1170
1169
1171
1172
+ @pytest .mark .usefixtures ("ipython_interactive" )
1173
+ @pytest .mark .skipif (pandas is None , reason = "Requires `pandas`" )
1174
+ def test_bigquery_magic_w_progress_bar_type_w_context_setter (monkeypatch ):
1175
+ ip = IPython .get_ipython ()
1176
+ ip .extension_manager .load_extension ("google.cloud.bigquery" )
1177
+ magics .context ._project = None
1178
+
1179
+ magics .context .progress_bar_type = "tqdm_gui"
1180
+
1181
+ mock_credentials = mock .create_autospec (
1182
+ google .auth .credentials .Credentials , instance = True
1183
+ )
1184
+
1185
+ # Set up the context with monkeypatch so that it's reset for subsequent
1186
+ # tests.
1187
+ monkeypatch .setattr (magics .context , "_credentials" , mock_credentials )
1188
+
1189
+ # Mock out the BigQuery Storage API.
1190
+ bqstorage_mock = mock .create_autospec (bigquery_storage .BigQueryReadClient )
1191
+ bqstorage_client_patch = mock .patch (
1192
+ "google.cloud.bigquery_storage.BigQueryReadClient" , bqstorage_mock
1193
+ )
1194
+
1195
+ sql = "SELECT 17 AS num"
1196
+ result = pandas .DataFrame ([17 ], columns = ["num" ])
1197
+ run_query_patch = mock .patch (
1198
+ "google.cloud.bigquery.magics.magics._run_query" , autospec = True
1199
+ )
1200
+ query_job_mock = mock .create_autospec (
1201
+ google .cloud .bigquery .job .QueryJob , instance = True
1202
+ )
1203
+ query_job_mock .to_dataframe .return_value = result
1204
+ with run_query_patch as run_query_mock , bqstorage_client_patch :
1205
+ run_query_mock .return_value = query_job_mock
1206
+
1207
+ return_value = ip .run_cell_magic ("bigquery" , "--use_rest_api" , sql )
1208
+
1209
+ bqstorage_mock .assert_not_called ()
1210
+ query_job_mock .to_dataframe .assert_called_once_with (
1211
+ bqstorage_client = None , progress_bar_type = magics .context .progress_bar_type
1212
+ )
1213
+
1214
+ assert isinstance (return_value , pandas .DataFrame )
1215
+
1216
+
1217
+ @pytest .mark .usefixtures ("ipython_interactive" )
1218
+ def test_bigquery_magic_with_progress_bar_type ():
1219
+ ip = IPython .get_ipython ()
1220
+ ip .extension_manager .load_extension ("google.cloud.bigquery" )
1221
+ magics .context .progress_bar_type = None
1222
+
1223
+ run_query_patch = mock .patch (
1224
+ "google.cloud.bigquery.magics.magics._run_query" , autospec = True
1225
+ )
1226
+ with run_query_patch as run_query_mock :
1227
+ ip .run_cell_magic (
1228
+ "bigquery" , "--progress_bar_type=tqdm_gui" , "SELECT 17 as num"
1229
+ )
1230
+
1231
+ progress_bar_used = run_query_mock .mock_calls [1 ][2 ]["progress_bar_type" ]
1232
+ assert progress_bar_used == "tqdm_gui"
1233
+ # context progress bar type should not change
1234
+ assert magics .context .progress_bar_type is None
1235
+
1236
+
1170
1237
@pytest .mark .usefixtures ("ipython_interactive" )
1171
1238
def test_bigquery_magic_with_project ():
1172
1239
ip = IPython .get_ipython ()
0 commit comments