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

improve error when calling sqlcmd #804

Merged
merged 10 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [Fix] Current connection and switching connections message only displayed when `feedback>=1`
* [Fix] `--persist/--persist-replace` perform `ROLLBACK` automatically when needed
* [Fix] `ResultSet` footer (when `displaylimit` truncates results and when showing how to convert to a data frame) now appears in the `ResultSet` plain text representation (#682)
* [Fix] Improve error when calling `%sqlcmd` (#761)
* [API Change] When loading connections from a `.ini` file via `%sql --section section_name`, the section name is set as the connection alias
* [API Change] Starting connections from a `.ini` file via `%sql [section_name]` has been deprecated
* [Doc] Fixes documentation inaccuracy that said `:variable` was deprecated (we brought it back in `0.9.0`)
Expand Down
15 changes: 14 additions & 1 deletion src/sql/magic_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from sql.cmd.profile import profile
from sql.cmd.explore import explore
from sql.cmd.snippets import snippets
from sql.connection import ConnectionManager

try:
from traitlets.config.configurable import Configurable
Expand Down Expand Up @@ -51,6 +52,8 @@ def _validate_execute_inputs(self, line):
"explore",
"snippets",
]
CONNECTION_INDEPENDENT_COMMANDS = ["snippets"]
edublancas marked this conversation as resolved.
Show resolved Hide resolved
SUPPORTED_BY_BOTH_COMMANDS = ["profile"]
edublancas marked this conversation as resolved.
Show resolved Hide resolved

VALID_COMMANDS_MSG = (
f"Missing argument for %sqlcmd. "
Expand All @@ -64,8 +67,18 @@ def _validate_execute_inputs(self, line):
command, others = split[0].strip(), split[1:]

if command in AVAILABLE_SQLCMD_COMMANDS:
if command not in ["profile"]:
if (
command not in CONNECTION_INDEPENDENT_COMMANDS
and not ConnectionManager.current
):
raise exceptions.RuntimeError("No active connection")
edublancas marked this conversation as resolved.
Show resolved Hide resolved

if command not in [
*CONNECTION_INDEPENDENT_COMMANDS,
*SUPPORTED_BY_BOTH_COMMANDS,
]:
util.support_only_sql_alchemy_connection("%sqlcmd")
edublancas marked this conversation as resolved.
Show resolved Hide resolved

return self.execute(command, others)
else:
raise exceptions.UsageError(
Expand Down
48 changes: 48 additions & 0 deletions src/tests/test_magic_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,54 @@ def test_error(tmp_empty, ip, cell, error_message):
assert str(excinfo.value) == error_message


@pytest.mark.parametrize(
"command",
[
"tables",
"columns",
"test",
"profile",
"explore",
],
)
def test_sqlcmd_error_when_no_connection(ip_empty, command):
with pytest.raises(UsageError) as excinfo:
ip_empty.run_cell(f"%sqlcmd {command}")

assert excinfo.value.error_type == "RuntimeError"
assert str(excinfo.value) == "No active connection"


def test_sqlcmd_snippets_when_no_connection(ip_empty, capsys):
for key in list(store):
del store[key]
edublancas marked this conversation as resolved.
Show resolved Hide resolved

ip_empty.run_cell("%sqlcmd snippets")
captured = capsys.readouterr()
assert "No snippets stored" in captured.out


@pytest.mark.parametrize(
"query",
[
("%sqlcmd tables"),
("%sqlcmd columns --table penguins.csv"),
("%sqlcmd test --table penguins.csv --column body_mass_g --greater 2900"),
("%sqlcmd explore --table penguins.csv"),
],
)
def test_sqlcmd_not_supported_error(ip_with_connections, query, capsys):
ip_with_connections.run_cell("%sql duckdb_dbapi")
expected_error_message = (
"%sqlcmd is only supported with SQLAlchemy connections, "
"not with DBAPI connections"
)
with pytest.raises(UsageError) as excinfo:
ip_with_connections.run_cell(query)

assert expected_error_message in str(excinfo.value)


def test_tables(ip):
out = ip.run_cell("%sqlcmd tables").result._repr_html_()
assert "author" in out
Expand Down