Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve cell magic error message on missing query #58

Merged
merged 5 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion google/cloud/bigquery/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@
the variable name (ex. ``$my_dict_var``). See ``In[6]`` and ``In[7]``
in the Examples section below.
* ``<query>`` (required, cell argument):
SQL query to run.
SQL query to run. If the query does not contain any whitespace (aside
from leading and trailing whitespace), it is assumed to represent a
fully-qualified table ID, and the latter's data will be fetched and
plamut marked this conversation as resolved.
Show resolved Hide resolved
returned as ``pandas.DataFrame``.

Returns:
A :class:`pandas.DataFrame` with the query results.
Expand Down Expand Up @@ -506,6 +509,11 @@ def _cell_magic(line, query):

query = query.strip()

if not query:
error = ValueError("Query is missing.")
_handle_error(error, args.destination_var)
return

# Any query that does not contain whitespace (aside from leading and trailing whitespace)
# is assumed to be a table id
if not re.search(r"\s", query):
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,22 @@ def test_bigquery_magic_w_table_id_invalid():
assert "Traceback (most recent call last)" not in output


def test_bigquery_magic_w_missing_query():
ip = IPython.get_ipython()
ip.extension_manager.load_extension("google.cloud.bigquery")
magics.context._project = None

cell_body = " \n \n \t\t \n "

with io.capture_output() as captured_io:
ip.run_cell_magic("bigquery", "df", cell_body)

output = captured_io.stderr
assert "Could not save output to variable" in output
assert "Query is missing" in output
assert "Traceback (most recent call last)" not in output


@pytest.mark.usefixtures("ipython_interactive")
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
def test_bigquery_magic_w_table_id_and_destination_var():
Expand Down