From 5420aa47c7a819b7600cbf2cb612233f9368f9e4 Mon Sep 17 00:00:00 2001 From: Peter Lamut Date: Tue, 10 Mar 2020 13:09:40 +0000 Subject: [PATCH 1/2] fix: improve cell magic error message on missing query --- google/cloud/bigquery/magics.py | 10 +++++++++- tests/unit/test_magics.py | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/google/cloud/bigquery/magics.py b/google/cloud/bigquery/magics.py index 39608b19f..1317f97bd 100644 --- a/google/cloud/bigquery/magics.py +++ b/google/cloud/bigquery/magics.py @@ -66,7 +66,10 @@ the variable name (ex. ``$my_dict_var``). See ``In[6]`` and ``In[7]`` in the Examples section below. * ```` (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 + returned as ``pandas.DataFrame``. Returns: A :class:`pandas.DataFrame` with the query results. @@ -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): diff --git a/tests/unit/test_magics.py b/tests/unit/test_magics.py index 3f66b2c4b..fd9d1d700 100644 --- a/tests/unit/test_magics.py +++ b/tests/unit/test_magics.py @@ -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(): From a1add5e94bfc056890814bb2203838d7cc68a650 Mon Sep 17 00:00:00 2001 From: Peter Lamut Date: Tue, 17 Mar 2020 09:49:19 +0100 Subject: [PATCH 2/2] Remove possibly confusing wording from docstring --- google/cloud/bigquery/magics.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/google/cloud/bigquery/magics.py b/google/cloud/bigquery/magics.py index 1317f97bd..5872d0cfc 100644 --- a/google/cloud/bigquery/magics.py +++ b/google/cloud/bigquery/magics.py @@ -68,8 +68,7 @@ * ```` (required, cell argument): 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 - returned as ``pandas.DataFrame``. + fully-qualified table ID, and the latter's data will be fetched. Returns: A :class:`pandas.DataFrame` with the query results.