Skip to content

Commit

Permalink
feat: Add an is_registered global value to indicate whether the exten…
Browse files Browse the repository at this point in the history
…sion has been loaded (#83)

* feat: Add an is_registered global value to indicate whether the extension has been loaded

* fix format

* unregister magics after each test
  • Loading branch information
sycai authored Jan 13, 2025
1 parent cbb3e44 commit 0bc4473
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions bigquery_magics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
context = bigquery_magics.config.context
__version__ = bigquery_magics.version.__version__

# Whether the magics has already been reigstered by some other packages.
is_registered = False


def load_ipython_extension(ipython):
"""Called by IPython when this module is loaded as an IPython extension."""
Expand All @@ -29,11 +32,21 @@ def load_ipython_extension(ipython):
)
ipython.register_magic_function(_cell_magic, magic_kind="cell", magic_name="bqsql")

global is_registered
is_registered = True


def unload_ipython_extension(ipython):
global is_registered
is_registered = False


__all__ = (
# For backwards compatibility we need to make the context available in
# the path google.cloud.bigquery.magics.context.
"context",
"__version__",
"load_ipython_extension",
"unload_ipython_extension",
"is_registered",
)
15 changes: 15 additions & 0 deletions tests/unit/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def ipython_interactive(request, ipython):
with ipython.builtin_trap:
yield ipython

ipython.get_ipython().extension_manager.unload_extension("bigquery_magics")


@pytest.fixture()
def ipython_ns_cleanup():
Expand Down Expand Up @@ -2138,3 +2140,16 @@ def test_bigquery_magic_bigframes_with_dry_run__should_fail():

with bf_patch, pytest.raises(ValueError):
ip.run_cell_magic("bigquery", "--dry_run", sql)


@pytest.mark.usefixtures("ipython_interactive")
def test_test_bigquery_magic__extension_not_loaded__is_registered_set_to_false():
assert bigquery_magics.is_registered is False


@pytest.mark.usefixtures("ipython_interactive")
def test_test_bigquery_magic__extension_loaded__is_registered_set_to_true():
ip = IPython.get_ipython()
ip.extension_manager.load_extension("bigquery_magics")

assert bigquery_magics.is_registered is True

0 comments on commit 0bc4473

Please sign in to comment.