Skip to content

Commit

Permalink
Plot error message (#441)
Browse files Browse the repository at this point in the history
* success case

* Plot error message

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

lint

revert

added tmp_empty

failure case
  • Loading branch information
neelasha23 authored May 5, 2023
1 parent 6c7082d commit 8ce3334
Show file tree
Hide file tree
Showing 4 changed files with 60 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)
)
31 changes: 31 additions & 0 deletions src/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sql.connection import Connection
from pathlib import Path
import pytest
from sqlalchemy.exc import OperationalError


class DictOfFloats(Mapping):
Expand Down Expand Up @@ -81,6 +82,36 @@ def test_boxplot_stats_exception(chinook_db, ip_empty):
)


def test_summary_stats(chinook_db, ip_empty, tmp_empty):
Path("data.csv").write_text(
"""\
x, y
0, 0
1, 1
2, 2
5, 7
9, 9
"""
)
ip_empty.run_cell("%sql duckdb://")
ip_empty.run_cell("%sql INSTALL 'sqlite_scanner';")
ip_empty.run_cell("%sql commit")
ip_empty.run_cell("%sql LOAD 'sqlite_scanner';")
result = plot._summary_stats(Connection.current, "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_missing_file(chinook_db, ip_empty):
ip_empty.run_cell("%sql duckdb://")
ip_empty.run_cell("%sql INSTALL 'sqlite_scanner';")
ip_empty.run_cell("%sql commit")
ip_empty.run_cell("%sql LOAD 'sqlite_scanner';")
with pytest.raises(OperationalError) as e:
plot._summary_stats(Connection.current, "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

0 comments on commit 8ce3334

Please sign in to comment.