Skip to content

Commit ba7532f

Browse files
committed
test: fix magics tests in prerelease by mocking context
1 parent 93b6f8d commit ba7532f

1 file changed

Lines changed: 22 additions & 10 deletions

File tree

packages/google-cloud-bigquery/tests/unit/test_magics.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@
4545
pandas = pytest.importorskip("pandas")
4646

4747

48+
@pytest.fixture()
49+
def use_local_magics_context(monkeypatch):
50+
if magics is not None:
51+
local_context = magics.Context()
52+
local_context._project = "unit-test-project"
53+
mock_credentials = mock.create_autospec(
54+
google.auth.credentials.Credentials, instance=True
55+
)
56+
local_context._credentials = mock_credentials
57+
monkeypatch.setattr(magics, "context", local_context)
58+
59+
4860
@pytest.fixture(scope="session")
4961
def ipython():
5062
config = tools.default_config()
@@ -523,7 +535,7 @@ def test_bigquery_magic_default_connection_user_agent(monkeypatch):
523535

524536

525537
@pytest.mark.usefixtures("ipython_interactive")
526-
def test_bigquery_magic_with_legacy_sql(monkeypatch):
538+
def test_bigquery_magic_with_legacy_sql(monkeypatch, use_local_magics_context):
527539
ip = IPython.get_ipython()
528540
monkeypatch.setattr(bigquery, "bigquery_magics", None)
529541
bigquery.load_ipython_extension(ip)
@@ -543,7 +555,7 @@ def test_bigquery_magic_with_legacy_sql(monkeypatch):
543555

544556
@pytest.mark.usefixtures("ipython_interactive")
545557
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
546-
def test_bigquery_magic_with_result_saved_to_variable(ipython_ns_cleanup, monkeypatch):
558+
def test_bigquery_magic_with_result_saved_to_variable(ipython_ns_cleanup, monkeypatch, use_local_magics_context):
547559
ip = IPython.get_ipython()
548560
monkeypatch.setattr(bigquery, "bigquery_magics", None)
549561
bigquery.load_ipython_extension(ip)
@@ -577,7 +589,7 @@ def test_bigquery_magic_with_result_saved_to_variable(ipython_ns_cleanup, monkey
577589

578590

579591
@pytest.mark.usefixtures("ipython_interactive")
580-
def test_bigquery_magic_does_not_clear_display_in_verbose_mode(monkeypatch):
592+
def test_bigquery_magic_does_not_clear_display_in_verbose_mode(monkeypatch, use_local_magics_context):
581593
ip = IPython.get_ipython()
582594
monkeypatch.setattr(bigquery, "bigquery_magics", None)
583595
bigquery.load_ipython_extension(ip)
@@ -599,7 +611,7 @@ def test_bigquery_magic_does_not_clear_display_in_verbose_mode(monkeypatch):
599611

600612

601613
@pytest.mark.usefixtures("ipython_interactive")
602-
def test_bigquery_magic_clears_display_in_non_verbose_mode(monkeypatch):
614+
def test_bigquery_magic_clears_display_in_non_verbose_mode(monkeypatch, use_local_magics_context):
603615
ip = IPython.get_ipython()
604616
monkeypatch.setattr(bigquery, "bigquery_magics", None)
605617
bigquery.load_ipython_extension(ip)
@@ -624,7 +636,7 @@ def test_bigquery_magic_clears_display_in_non_verbose_mode(monkeypatch):
624636
@pytest.mark.skipif(
625637
bigquery_storage is None, reason="Requires `google-cloud-bigquery-storage`"
626638
)
627-
def test_bigquery_magic_with_bqstorage_from_argument(monkeypatch):
639+
def test_bigquery_magic_with_bqstorage_from_argument(monkeypatch, use_local_magics_context):
628640
ip = IPython.get_ipython()
629641
monkeypatch.setattr(bigquery, "bigquery_magics", None)
630642
bigquery.load_ipython_extension(ip)
@@ -691,7 +703,7 @@ def warning_match(warning):
691703
@pytest.mark.skipif(
692704
bigquery_storage is None, reason="Requires `google-cloud-bigquery-storage`"
693705
)
694-
def test_bigquery_magic_with_rest_client_requested(monkeypatch):
706+
def test_bigquery_magic_with_rest_client_requested(monkeypatch, use_local_magics_context):
695707
pandas = pytest.importorskip("pandas")
696708

697709
ip = IPython.get_ipython()
@@ -1437,7 +1449,7 @@ def test_bigquery_magic_with_project(monkeypatch):
14371449

14381450

14391451
@pytest.mark.usefixtures("ipython_interactive")
1440-
def test_bigquery_magic_with_bigquery_api_endpoint(ipython_ns_cleanup, monkeypatch):
1452+
def test_bigquery_magic_with_bigquery_api_endpoint(ipython_ns_cleanup, monkeypatch, use_local_magics_context):
14411453
ip = IPython.get_ipython()
14421454
monkeypatch.setattr(bigquery, "bigquery_magics", None)
14431455
bigquery.load_ipython_extension(ip)
@@ -1460,7 +1472,7 @@ def test_bigquery_magic_with_bigquery_api_endpoint(ipython_ns_cleanup, monkeypat
14601472

14611473

14621474
@pytest.mark.usefixtures("ipython_interactive")
1463-
def test_bigquery_magic_with_bigquery_api_endpoint_context_dict(monkeypatch):
1475+
def test_bigquery_magic_with_bigquery_api_endpoint_context_dict(monkeypatch, use_local_magics_context):
14641476
ip = IPython.get_ipython()
14651477
monkeypatch.setattr(bigquery, "bigquery_magics", None)
14661478
bigquery.load_ipython_extension(ip)
@@ -1484,7 +1496,7 @@ def test_bigquery_magic_with_bigquery_api_endpoint_context_dict(monkeypatch):
14841496

14851497

14861498
@pytest.mark.usefixtures("ipython_interactive")
1487-
def test_bigquery_magic_with_bqstorage_api_endpoint(ipython_ns_cleanup, monkeypatch):
1499+
def test_bigquery_magic_with_bqstorage_api_endpoint(ipython_ns_cleanup, monkeypatch, use_local_magics_context):
14881500
ip = IPython.get_ipython()
14891501
monkeypatch.setattr(bigquery, "bigquery_magics", None)
14901502
bigquery.load_ipython_extension(ip)
@@ -1507,7 +1519,7 @@ def test_bigquery_magic_with_bqstorage_api_endpoint(ipython_ns_cleanup, monkeypa
15071519

15081520

15091521
@pytest.mark.usefixtures("ipython_interactive")
1510-
def test_bigquery_magic_with_bqstorage_api_endpoint_context_dict(monkeypatch):
1522+
def test_bigquery_magic_with_bqstorage_api_endpoint_context_dict(monkeypatch, use_local_magics_context):
15111523
ip = IPython.get_ipython()
15121524
monkeypatch.setattr(bigquery, "bigquery_magics", None)
15131525
bigquery.load_ipython_extension(ip)

0 commit comments

Comments
 (0)