Skip to content

Commit

Permalink
Plot error message
Browse files Browse the repository at this point in the history
testcase

Fix

Fix

Fix

test fix

test fix

test fix

changelog

usageexception

test

test

error

error

error

error

error

changed to runtimeerror

quest db test

Revert test

Revert test

exception type test

Review comments

changelog

rebase

changelog

Review comments

changed to sqlite

change sequence
  • Loading branch information
neelasha23 committed May 5, 2023
1 parent 947af61 commit 8dacd02
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [Feature] Using native DuckDB `.df()` method when using `autopandas`

* [Doc] documenting `%sqlcmd tables`/`%sqlcmd columns`
* [Feature] Better error messages when function used in plotting API unsupported by DB driver (#159)

## 0.7.4 (2023-04-28)
No changes
Expand Down
14 changes: 13 additions & 1 deletion src/sql/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from jinja2 import Template

from sql.util import flatten
from sqlalchemy.exc import ProgrammingError
from sql import exceptions

try:
import matplotlib.pyplot as plt
Expand All @@ -30,6 +32,7 @@ def _summary_stats(conn, table, column, with_=None):

if not conn:
conn = sql.connection.Connection.current
driver = conn._get_curr_sqlalchemy_connection_info()["driver"]

template = Template(
"""
Expand All @@ -44,7 +47,16 @@ def _summary_stats(conn, table, column, with_=None):

query = template.render(table=table, column=column)

values = conn.execute(query, with_).fetchone()
try:
values = conn.execute(query, with_).fetchone()
except ProgrammingError as e:
print(e)
raise exceptions.RuntimeError(
f"\nEnsure that percentile_disc function is available on {driver}."
)
except Exception as e:
raise e

keys = ["q1", "med", "q3", "mean", "N"]
return {k: float(v) for k, v in zip(keys, flatten(values))}

Expand Down
15 changes: 15 additions & 0 deletions src/tests/integration/test_mssql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pyodbc
import pytest
from matplotlib import pyplot as plt
from IPython.core.error import UsageError


def test_query_count(ip_with_MSSQL, test_table_name_dict):
Expand Down Expand Up @@ -112,3 +113,17 @@ def test_sqlplot_boxplot(ip_with_MSSQL, cell):
out = ip_with_MSSQL.run_cell(cell)

assert type(out.result).__name__ in {"Axes", "AxesSubplot"}


def test_unsupported_function(ip_with_MSSQL, test_table_name_dict):
# clean current Axes
plt.cla()
out = ip_with_MSSQL.run_cell(
f"%sqlplot boxplot --table " f"{test_table_name_dict['taxi']} --column x"
)
assert isinstance(out.error_in_exec, UsageError)
assert "Ensure that percentile_disc function is available" in str(out.error_in_exec)
assert (
"If you need help solving this issue, "
"send us a message: https://ploomber.io/community" in str(out.error_in_exec)
)
29 changes: 29 additions & 0 deletions src/tests/test_magic_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@
from IPython.core.error import UsageError
import matplotlib.pyplot as plt

from sqlalchemy.exc import OperationalError

from sql.plot import _summary_stats


def test_summary_stats_success(tmp_empty, ip):
Path("data.csv").write_text(
"""\
x, y
0, 0
1, 1
2, 2
5, 7
9, 9
"""
)
ip.run_cell("%sql duckdb://")
result = _summary_stats(None, "data.csv", column="x")
expected = {"q1": 1.0, "med": 2.0, "q3": 5.0, "mean": 3.4, "N": 5.0}
assert result == expected


def test_summary_stats_failure(tmp_empty, ip):
ip.run_cell("%sql duckdb://")
with pytest.raises(OperationalError) as e:
_summary_stats(None, "data.csv", column="x")
assert 'No files found that match the pattern "data.csv"' in str(e)


@pytest.mark.parametrize(
"cell, error_type, error_message",
Expand Down Expand Up @@ -166,3 +194,4 @@ def test_sqlplot(tmp_empty, ip, cell):
# maptlotlib >= 3.7 has Axes but earlier Python
# versions are not compatible
assert type(out.result).__name__ in {"Axes", "AxesSubplot"}

0 comments on commit 8dacd02

Please sign in to comment.