43
43
from google .cloud import bigquery
44
44
from google .cloud .bigquery import job
45
45
from google .cloud .bigquery import table
46
- from google .cloud .bigquery import magics
46
+ from google .cloud .bigquery . ipython_magics import magics
47
47
from tests .unit .helpers import make_connection
48
48
from test_utils .imports import maybe_fail_import
49
49
@@ -256,7 +256,7 @@ def test__run_query():
256
256
]
257
257
258
258
client_patch = mock .patch (
259
- "google.cloud.bigquery.magics.bigquery.Client" , autospec = True
259
+ "google.cloud.bigquery.ipython_magics. magics.bigquery.Client" , autospec = True
260
260
)
261
261
with client_patch as client_mock , io .capture_output () as captured :
262
262
client_mock ().query (sql ).result .side_effect = responses
@@ -284,7 +284,7 @@ def test__run_query_dry_run_without_errors_is_silent():
284
284
sql = "SELECT 17"
285
285
286
286
client_patch = mock .patch (
287
- "google.cloud.bigquery.magics.bigquery.Client" , autospec = True
287
+ "google.cloud.bigquery.ipython_magics. magics.bigquery.Client" , autospec = True
288
288
)
289
289
290
290
job_config = job .QueryJobConfig ()
@@ -350,7 +350,7 @@ def test__create_dataset_if_necessary_exists():
350
350
dataset_reference = bigquery .dataset .DatasetReference (project , dataset_id )
351
351
dataset = bigquery .Dataset (dataset_reference )
352
352
client_patch = mock .patch (
353
- "google.cloud.bigquery.magics.bigquery.Client" , autospec = True
353
+ "google.cloud.bigquery.ipython_magics. magics.bigquery.Client" , autospec = True
354
354
)
355
355
with client_patch as client_mock :
356
356
client = client_mock ()
@@ -364,7 +364,7 @@ def test__create_dataset_if_necessary_not_exist():
364
364
project = "project_id"
365
365
dataset_id = "dataset_id"
366
366
client_patch = mock .patch (
367
- "google.cloud.bigquery.magics.bigquery.Client" , autospec = True
367
+ "google.cloud.bigquery.ipython_magics. magics.bigquery.Client" , autospec = True
368
368
)
369
369
with client_patch as client_mock :
370
370
client = client_mock ()
@@ -382,7 +382,7 @@ def test_extension_load():
382
382
383
383
# verify that the magic is registered and has the correct source
384
384
magic = ip .magics_manager .magics ["cell" ].get ("bigquery" )
385
- assert magic .__module__ == "google.cloud.bigquery.magics"
385
+ assert magic .__module__ == "google.cloud.bigquery.ipython_magics. magics"
386
386
387
387
388
388
@pytest .mark .usefixtures ("ipython_interactive" )
@@ -415,7 +415,7 @@ def test_bigquery_magic_without_optional_arguments(monkeypatch):
415
415
sql = "SELECT 17 AS num"
416
416
result = pandas .DataFrame ([17 ], columns = ["num" ])
417
417
run_query_patch = mock .patch (
418
- "google.cloud.bigquery.magics._run_query" , autospec = True
418
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
419
419
)
420
420
query_job_mock = mock .create_autospec (
421
421
google .cloud .bigquery .job .QueryJob , instance = True
@@ -445,7 +445,7 @@ def test_bigquery_magic_default_connection_user_agent():
445
445
"google.auth.default" , return_value = (credentials_mock , "general-project" )
446
446
)
447
447
run_query_patch = mock .patch (
448
- "google.cloud.bigquery.magics._run_query" , autospec = True
448
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
449
449
)
450
450
conn_patch = mock .patch ("google.cloud.bigquery.client.Connection" , autospec = True )
451
451
@@ -466,7 +466,7 @@ def test_bigquery_magic_with_legacy_sql():
466
466
)
467
467
468
468
run_query_patch = mock .patch (
469
- "google.cloud.bigquery.magics._run_query" , autospec = True
469
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
470
470
)
471
471
with run_query_patch as run_query_mock :
472
472
ip .run_cell_magic ("bigquery" , "--use_legacy_sql" , "SELECT 17 AS num" )
@@ -489,7 +489,7 @@ def test_bigquery_magic_with_result_saved_to_variable():
489
489
assert "df" not in ip .user_ns
490
490
491
491
run_query_patch = mock .patch (
492
- "google.cloud.bigquery.magics._run_query" , autospec = True
492
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
493
493
)
494
494
query_job_mock = mock .create_autospec (
495
495
google .cloud .bigquery .job .QueryJob , instance = True
@@ -516,10 +516,11 @@ def test_bigquery_magic_does_not_clear_display_in_verbose_mode():
516
516
)
517
517
518
518
clear_patch = mock .patch (
519
- "google.cloud.bigquery.magics.display.clear_output" , autospec = True
519
+ "google.cloud.bigquery.ipython_magics.magics.display.clear_output" ,
520
+ autospec = True ,
520
521
)
521
522
run_query_patch = mock .patch (
522
- "google.cloud.bigquery.magics._run_query" , autospec = True
523
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
523
524
)
524
525
with clear_patch as clear_mock , run_query_patch :
525
526
ip .run_cell_magic ("bigquery" , "--verbose" , "SELECT 17 as num" )
@@ -536,10 +537,11 @@ def test_bigquery_magic_clears_display_in_verbose_mode():
536
537
)
537
538
538
539
clear_patch = mock .patch (
539
- "google.cloud.bigquery.magics.display.clear_output" , autospec = True
540
+ "google.cloud.bigquery.ipython_magics.magics.display.clear_output" ,
541
+ autospec = True ,
540
542
)
541
543
run_query_patch = mock .patch (
542
- "google.cloud.bigquery.magics._run_query" , autospec = True
544
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
543
545
)
544
546
with clear_patch as clear_mock , run_query_patch :
545
547
ip .run_cell_magic ("bigquery" , "" , "SELECT 17 as num" )
@@ -576,7 +578,7 @@ def test_bigquery_magic_with_bqstorage_from_argument(monkeypatch):
576
578
sql = "SELECT 17 AS num"
577
579
result = pandas .DataFrame ([17 ], columns = ["num" ])
578
580
run_query_patch = mock .patch (
579
- "google.cloud.bigquery.magics._run_query" , autospec = True
581
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
580
582
)
581
583
query_job_mock = mock .create_autospec (
582
584
google .cloud .bigquery .job .QueryJob , instance = True
@@ -635,7 +637,7 @@ def test_bigquery_magic_with_rest_client_requested(monkeypatch):
635
637
sql = "SELECT 17 AS num"
636
638
result = pandas .DataFrame ([17 ], columns = ["num" ])
637
639
run_query_patch = mock .patch (
638
- "google.cloud.bigquery.magics._run_query" , autospec = True
640
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
639
641
)
640
642
query_job_mock = mock .create_autospec (
641
643
google .cloud .bigquery .job .QueryJob , instance = True
@@ -719,7 +721,7 @@ def test_bigquery_magic_w_max_results_query_job_results_fails():
719
721
"google.cloud.bigquery.client.Client.query" , autospec = True
720
722
)
721
723
close_transports_patch = mock .patch (
722
- "google.cloud.bigquery.magics._close_transports" , autospec = True ,
724
+ "google.cloud.bigquery.ipython_magics. magics._close_transports" , autospec = True ,
723
725
)
724
726
725
727
sql = "SELECT 17 AS num"
@@ -751,7 +753,7 @@ def test_bigquery_magic_w_table_id_invalid():
751
753
)
752
754
753
755
list_rows_patch = mock .patch (
754
- "google.cloud.bigquery.magics.bigquery.Client.list_rows" ,
756
+ "google.cloud.bigquery.ipython_magics. magics.bigquery.Client.list_rows" ,
755
757
autospec = True ,
756
758
side_effect = exceptions .BadRequest ("Not a valid table ID" ),
757
759
)
@@ -809,7 +811,7 @@ def test_bigquery_magic_w_table_id_and_destination_var():
809
811
)
810
812
811
813
client_patch = mock .patch (
812
- "google.cloud.bigquery.magics.bigquery.Client" , autospec = True
814
+ "google.cloud.bigquery.ipython_magics. magics.bigquery.Client" , autospec = True
813
815
)
814
816
815
817
table_id = "bigquery-public-data.samples.shakespeare"
@@ -849,7 +851,7 @@ def test_bigquery_magic_w_table_id_and_bqstorage_client():
849
851
)
850
852
851
853
client_patch = mock .patch (
852
- "google.cloud.bigquery.magics.bigquery.Client" , autospec = True
854
+ "google.cloud.bigquery.ipython_magics. magics.bigquery.Client" , autospec = True
853
855
)
854
856
855
857
bqstorage_mock = mock .create_autospec (bigquery_storage_v1 .BigQueryReadClient )
@@ -882,7 +884,7 @@ def test_bigquery_magic_dryrun_option_sets_job_config():
882
884
)
883
885
884
886
run_query_patch = mock .patch (
885
- "google.cloud.bigquery.magics._run_query" , autospec = True
887
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
886
888
)
887
889
888
890
sql = "SELECT 17 AS num"
@@ -905,7 +907,7 @@ def test_bigquery_magic_dryrun_option_returns_query_job():
905
907
google .cloud .bigquery .job .QueryJob , instance = True
906
908
)
907
909
run_query_patch = mock .patch (
908
- "google.cloud.bigquery.magics._run_query" , autospec = True
910
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
909
911
)
910
912
911
913
sql = "SELECT 17 AS num"
@@ -927,7 +929,7 @@ def test_bigquery_magic_dryrun_option_variable_error_message():
927
929
)
928
930
929
931
run_query_patch = mock .patch (
930
- "google.cloud.bigquery.magics._run_query" ,
932
+ "google.cloud.bigquery.ipython_magics. magics._run_query" ,
931
933
autospec = True ,
932
934
side_effect = exceptions .BadRequest ("Syntax error in SQL query" ),
933
935
)
@@ -954,7 +956,7 @@ def test_bigquery_magic_dryrun_option_saves_query_job_to_variable():
954
956
google .cloud .bigquery .job .QueryJob , instance = True
955
957
)
956
958
run_query_patch = mock .patch (
957
- "google.cloud.bigquery.magics._run_query" , autospec = True
959
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
958
960
)
959
961
960
962
sql = "SELECT 17 AS num"
@@ -1151,7 +1153,7 @@ def test_bigquery_magic_with_project():
1151
1153
"google.auth.default" , return_value = (credentials_mock , "general-project" )
1152
1154
)
1153
1155
run_query_patch = mock .patch (
1154
- "google.cloud.bigquery.magics._run_query" , autospec = True
1156
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
1155
1157
)
1156
1158
with run_query_patch as run_query_mock , default_patch :
1157
1159
ip .run_cell_magic ("bigquery" , "--project=specific-project" , "SELECT 17 as num" )
@@ -1176,7 +1178,7 @@ def test_bigquery_magic_with_string_params():
1176
1178
assert "params_string_df" not in ip .user_ns
1177
1179
1178
1180
run_query_patch = mock .patch (
1179
- "google.cloud.bigquery.magics._run_query" , autospec = True
1181
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
1180
1182
)
1181
1183
query_job_mock = mock .create_autospec (
1182
1184
google .cloud .bigquery .job .QueryJob , instance = True
@@ -1209,7 +1211,7 @@ def test_bigquery_magic_with_dict_params():
1209
1211
assert "params_dict_df" not in ip .user_ns
1210
1212
1211
1213
run_query_patch = mock .patch (
1212
- "google.cloud.bigquery.magics._run_query" , autospec = True
1214
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
1213
1215
)
1214
1216
query_job_mock = mock .create_autospec (
1215
1217
google .cloud .bigquery .job .QueryJob , instance = True
@@ -1259,7 +1261,7 @@ def test_bigquery_magic_omits_tracebacks_from_error_message():
1259
1261
)
1260
1262
1261
1263
run_query_patch = mock .patch (
1262
- "google.cloud.bigquery.magics._run_query" ,
1264
+ "google.cloud.bigquery.ipython_magics. magics._run_query" ,
1263
1265
autospec = True ,
1264
1266
side_effect = exceptions .BadRequest ("Syntax error in SQL query" ),
1265
1267
)
@@ -1287,7 +1289,7 @@ def test_bigquery_magic_w_destination_table_invalid_format():
1287
1289
)
1288
1290
1289
1291
client_patch = mock .patch (
1290
- "google.cloud.bigquery.magics.bigquery.Client" , autospec = True
1292
+ "google.cloud.bigquery.ipython_magics. magics.bigquery.Client" , autospec = True
1291
1293
)
1292
1294
1293
1295
with client_patch , default_patch , pytest .raises (ValueError ) as exc_context :
@@ -1310,11 +1312,12 @@ def test_bigquery_magic_w_destination_table():
1310
1312
)
1311
1313
1312
1314
create_dataset_if_necessary_patch = mock .patch (
1313
- "google.cloud.bigquery.magics._create_dataset_if_necessary" , autospec = True
1315
+ "google.cloud.bigquery.ipython_magics.magics._create_dataset_if_necessary" ,
1316
+ autospec = True ,
1314
1317
)
1315
1318
1316
1319
run_query_patch = mock .patch (
1317
- "google.cloud.bigquery.magics._run_query" , autospec = True
1320
+ "google.cloud.bigquery.ipython_magics. magics._run_query" , autospec = True
1318
1321
)
1319
1322
1320
1323
with create_dataset_if_necessary_patch , run_query_patch as run_query_mock :
@@ -1341,12 +1344,12 @@ def test_bigquery_magic_create_dataset_fails():
1341
1344
)
1342
1345
1343
1346
create_dataset_if_necessary_patch = mock .patch (
1344
- "google.cloud.bigquery.magics._create_dataset_if_necessary" ,
1347
+ "google.cloud.bigquery.ipython_magics. magics._create_dataset_if_necessary" ,
1345
1348
autospec = True ,
1346
1349
side_effect = OSError ,
1347
1350
)
1348
1351
close_transports_patch = mock .patch (
1349
- "google.cloud.bigquery.magics._close_transports" , autospec = True ,
1352
+ "google.cloud.bigquery.ipython_magics. magics._close_transports" , autospec = True ,
1350
1353
)
1351
1354
1352
1355
with pytest .raises (
0 commit comments