Skip to content

Commit 6182cf4

Browse files
authored
fix: improve cell magic error message on missing query (#58)
* fix: improve cell magic error message on missing query * Remove possibly confusing wording from docstring
1 parent 9ada7a5 commit 6182cf4

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

google/cloud/bigquery/magics.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@
6666
the variable name (ex. ``$my_dict_var``). See ``In[6]`` and ``In[7]``
6767
in the Examples section below.
6868
* ``<query>`` (required, cell argument):
69-
SQL query to run.
69+
SQL query to run. If the query does not contain any whitespace (aside
70+
from leading and trailing whitespace), it is assumed to represent a
71+
fully-qualified table ID, and the latter's data will be fetched.
7072
7173
Returns:
7274
A :class:`pandas.DataFrame` with the query results.
@@ -506,6 +508,11 @@ def _cell_magic(line, query):
506508

507509
query = query.strip()
508510

511+
if not query:
512+
error = ValueError("Query is missing.")
513+
_handle_error(error, args.destination_var)
514+
return
515+
509516
# Any query that does not contain whitespace (aside from leading and trailing whitespace)
510517
# is assumed to be a table id
511518
if not re.search(r"\s", query):

tests/unit/test_magics.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,22 @@ def test_bigquery_magic_w_table_id_invalid():
800800
assert "Traceback (most recent call last)" not in output
801801

802802

803+
def test_bigquery_magic_w_missing_query():
804+
ip = IPython.get_ipython()
805+
ip.extension_manager.load_extension("google.cloud.bigquery")
806+
magics.context._project = None
807+
808+
cell_body = " \n \n \t\t \n "
809+
810+
with io.capture_output() as captured_io:
811+
ip.run_cell_magic("bigquery", "df", cell_body)
812+
813+
output = captured_io.stderr
814+
assert "Could not save output to variable" in output
815+
assert "Query is missing" in output
816+
assert "Traceback (most recent call last)" not in output
817+
818+
803819
@pytest.mark.usefixtures("ipython_interactive")
804820
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
805821
def test_bigquery_magic_w_table_id_and_destination_var():

0 commit comments

Comments
 (0)