Skip to content

Commit cc274f7

Browse files
committed
chore: Move cell magic code into its own directory
1 parent b842fb5 commit cc274f7

File tree

4 files changed

+50
-34
lines changed

4 files changed

+50
-34
lines changed

google/cloud/bigquery/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150

151151
def load_ipython_extension(ipython):
152152
"""Called by IPython when this module is loaded as an IPython extension."""
153-
from google.cloud.bigquery.magics import _cell_magic
153+
from google.cloud.bigquery.ipython_magics.magics import _cell_magic
154154

155155
ipython.register_magic_function(
156156
_cell_magic, magic_kind="cell", magic_name="bigquery"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

tests/unit/test_magics.py

+36-33
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from google.cloud import bigquery
4444
from google.cloud.bigquery import job
4545
from google.cloud.bigquery import table
46-
from google.cloud.bigquery import magics
46+
from google.cloud.bigquery.ipython_magics import magics
4747
from tests.unit.helpers import make_connection
4848
from test_utils.imports import maybe_fail_import
4949

@@ -256,7 +256,7 @@ def test__run_query():
256256
]
257257

258258
client_patch = mock.patch(
259-
"google.cloud.bigquery.magics.bigquery.Client", autospec=True
259+
"google.cloud.bigquery.ipython_magics.magics.bigquery.Client", autospec=True
260260
)
261261
with client_patch as client_mock, io.capture_output() as captured:
262262
client_mock().query(sql).result.side_effect = responses
@@ -284,7 +284,7 @@ def test__run_query_dry_run_without_errors_is_silent():
284284
sql = "SELECT 17"
285285

286286
client_patch = mock.patch(
287-
"google.cloud.bigquery.magics.bigquery.Client", autospec=True
287+
"google.cloud.bigquery.ipython_magics.magics.bigquery.Client", autospec=True
288288
)
289289

290290
job_config = job.QueryJobConfig()
@@ -350,7 +350,7 @@ def test__create_dataset_if_necessary_exists():
350350
dataset_reference = bigquery.dataset.DatasetReference(project, dataset_id)
351351
dataset = bigquery.Dataset(dataset_reference)
352352
client_patch = mock.patch(
353-
"google.cloud.bigquery.magics.bigquery.Client", autospec=True
353+
"google.cloud.bigquery.ipython_magics.magics.bigquery.Client", autospec=True
354354
)
355355
with client_patch as client_mock:
356356
client = client_mock()
@@ -364,7 +364,7 @@ def test__create_dataset_if_necessary_not_exist():
364364
project = "project_id"
365365
dataset_id = "dataset_id"
366366
client_patch = mock.patch(
367-
"google.cloud.bigquery.magics.bigquery.Client", autospec=True
367+
"google.cloud.bigquery.ipython_magics.magics.bigquery.Client", autospec=True
368368
)
369369
with client_patch as client_mock:
370370
client = client_mock()
@@ -382,7 +382,7 @@ def test_extension_load():
382382

383383
# verify that the magic is registered and has the correct source
384384
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"
386386

387387

388388
@pytest.mark.usefixtures("ipython_interactive")
@@ -415,7 +415,7 @@ def test_bigquery_magic_without_optional_arguments(monkeypatch):
415415
sql = "SELECT 17 AS num"
416416
result = pandas.DataFrame([17], columns=["num"])
417417
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
419419
)
420420
query_job_mock = mock.create_autospec(
421421
google.cloud.bigquery.job.QueryJob, instance=True
@@ -445,7 +445,7 @@ def test_bigquery_magic_default_connection_user_agent():
445445
"google.auth.default", return_value=(credentials_mock, "general-project")
446446
)
447447
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
449449
)
450450
conn_patch = mock.patch("google.cloud.bigquery.client.Connection", autospec=True)
451451

@@ -466,7 +466,7 @@ def test_bigquery_magic_with_legacy_sql():
466466
)
467467

468468
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
470470
)
471471
with run_query_patch as run_query_mock:
472472
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():
489489
assert "df" not in ip.user_ns
490490

491491
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
493493
)
494494
query_job_mock = mock.create_autospec(
495495
google.cloud.bigquery.job.QueryJob, instance=True
@@ -516,10 +516,11 @@ def test_bigquery_magic_does_not_clear_display_in_verbose_mode():
516516
)
517517

518518
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,
520521
)
521522
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
523524
)
524525
with clear_patch as clear_mock, run_query_patch:
525526
ip.run_cell_magic("bigquery", "--verbose", "SELECT 17 as num")
@@ -536,10 +537,11 @@ def test_bigquery_magic_clears_display_in_verbose_mode():
536537
)
537538

538539
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,
540542
)
541543
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
543545
)
544546
with clear_patch as clear_mock, run_query_patch:
545547
ip.run_cell_magic("bigquery", "", "SELECT 17 as num")
@@ -576,7 +578,7 @@ def test_bigquery_magic_with_bqstorage_from_argument(monkeypatch):
576578
sql = "SELECT 17 AS num"
577579
result = pandas.DataFrame([17], columns=["num"])
578580
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
580582
)
581583
query_job_mock = mock.create_autospec(
582584
google.cloud.bigquery.job.QueryJob, instance=True
@@ -635,7 +637,7 @@ def test_bigquery_magic_with_rest_client_requested(monkeypatch):
635637
sql = "SELECT 17 AS num"
636638
result = pandas.DataFrame([17], columns=["num"])
637639
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
639641
)
640642
query_job_mock = mock.create_autospec(
641643
google.cloud.bigquery.job.QueryJob, instance=True
@@ -719,7 +721,7 @@ def test_bigquery_magic_w_max_results_query_job_results_fails():
719721
"google.cloud.bigquery.client.Client.query", autospec=True
720722
)
721723
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,
723725
)
724726

725727
sql = "SELECT 17 AS num"
@@ -751,7 +753,7 @@ def test_bigquery_magic_w_table_id_invalid():
751753
)
752754

753755
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",
755757
autospec=True,
756758
side_effect=exceptions.BadRequest("Not a valid table ID"),
757759
)
@@ -809,7 +811,7 @@ def test_bigquery_magic_w_table_id_and_destination_var():
809811
)
810812

811813
client_patch = mock.patch(
812-
"google.cloud.bigquery.magics.bigquery.Client", autospec=True
814+
"google.cloud.bigquery.ipython_magics.magics.bigquery.Client", autospec=True
813815
)
814816

815817
table_id = "bigquery-public-data.samples.shakespeare"
@@ -849,7 +851,7 @@ def test_bigquery_magic_w_table_id_and_bqstorage_client():
849851
)
850852

851853
client_patch = mock.patch(
852-
"google.cloud.bigquery.magics.bigquery.Client", autospec=True
854+
"google.cloud.bigquery.ipython_magics.magics.bigquery.Client", autospec=True
853855
)
854856

855857
bqstorage_mock = mock.create_autospec(bigquery_storage_v1.BigQueryReadClient)
@@ -882,7 +884,7 @@ def test_bigquery_magic_dryrun_option_sets_job_config():
882884
)
883885

884886
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
886888
)
887889

888890
sql = "SELECT 17 AS num"
@@ -905,7 +907,7 @@ def test_bigquery_magic_dryrun_option_returns_query_job():
905907
google.cloud.bigquery.job.QueryJob, instance=True
906908
)
907909
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
909911
)
910912

911913
sql = "SELECT 17 AS num"
@@ -927,7 +929,7 @@ def test_bigquery_magic_dryrun_option_variable_error_message():
927929
)
928930

929931
run_query_patch = mock.patch(
930-
"google.cloud.bigquery.magics._run_query",
932+
"google.cloud.bigquery.ipython_magics.magics._run_query",
931933
autospec=True,
932934
side_effect=exceptions.BadRequest("Syntax error in SQL query"),
933935
)
@@ -954,7 +956,7 @@ def test_bigquery_magic_dryrun_option_saves_query_job_to_variable():
954956
google.cloud.bigquery.job.QueryJob, instance=True
955957
)
956958
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
958960
)
959961

960962
sql = "SELECT 17 AS num"
@@ -1151,7 +1153,7 @@ def test_bigquery_magic_with_project():
11511153
"google.auth.default", return_value=(credentials_mock, "general-project")
11521154
)
11531155
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
11551157
)
11561158
with run_query_patch as run_query_mock, default_patch:
11571159
ip.run_cell_magic("bigquery", "--project=specific-project", "SELECT 17 as num")
@@ -1176,7 +1178,7 @@ def test_bigquery_magic_with_string_params():
11761178
assert "params_string_df" not in ip.user_ns
11771179

11781180
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
11801182
)
11811183
query_job_mock = mock.create_autospec(
11821184
google.cloud.bigquery.job.QueryJob, instance=True
@@ -1209,7 +1211,7 @@ def test_bigquery_magic_with_dict_params():
12091211
assert "params_dict_df" not in ip.user_ns
12101212

12111213
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
12131215
)
12141216
query_job_mock = mock.create_autospec(
12151217
google.cloud.bigquery.job.QueryJob, instance=True
@@ -1259,7 +1261,7 @@ def test_bigquery_magic_omits_tracebacks_from_error_message():
12591261
)
12601262

12611263
run_query_patch = mock.patch(
1262-
"google.cloud.bigquery.magics._run_query",
1264+
"google.cloud.bigquery.ipython_magics.magics._run_query",
12631265
autospec=True,
12641266
side_effect=exceptions.BadRequest("Syntax error in SQL query"),
12651267
)
@@ -1287,7 +1289,7 @@ def test_bigquery_magic_w_destination_table_invalid_format():
12871289
)
12881290

12891291
client_patch = mock.patch(
1290-
"google.cloud.bigquery.magics.bigquery.Client", autospec=True
1292+
"google.cloud.bigquery.ipython_magics.magics.bigquery.Client", autospec=True
12911293
)
12921294

12931295
with client_patch, default_patch, pytest.raises(ValueError) as exc_context:
@@ -1310,11 +1312,12 @@ def test_bigquery_magic_w_destination_table():
13101312
)
13111313

13121314
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,
13141317
)
13151318

13161319
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
13181321
)
13191322

13201323
with create_dataset_if_necessary_patch, run_query_patch as run_query_mock:
@@ -1341,12 +1344,12 @@ def test_bigquery_magic_create_dataset_fails():
13411344
)
13421345

13431346
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",
13451348
autospec=True,
13461349
side_effect=OSError,
13471350
)
13481351
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,
13501353
)
13511354

13521355
with pytest.raises(

0 commit comments

Comments
 (0)