forked from catherinedevlin/ipython-sql
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test Lint changelog util moved error_message sqlglot
- Loading branch information
1 parent
617333b
commit 4d31eda
Showing
5 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import sqlglot | ||
|
||
|
||
def detail(msg, query): | ||
msg = str(msg) | ||
return_msg = f"Looks like there is some syntax error " f"in your query : {query} \n" | ||
if "syntax error" in msg: | ||
try: | ||
parse = sqlglot.transpile(query) | ||
if query.upper().replace(";", "").strip() not in [ | ||
suggestion.upper() for suggestion in parse | ||
]: | ||
return return_msg + f"Did you mean : {parse} " | ||
except sqlglot.errors.ParseError as e: | ||
err = e.errors | ||
for item in err: | ||
return_msg += ( | ||
f"Syntax Error: {item['description']} at " | ||
f"Line {item['line']}, Column {item['col']}\n" | ||
) | ||
return return_msg | ||
|
||
if "fe_sendauth: no password supplied" in msg: | ||
return ( | ||
"Looks like you have run into some issues. " | ||
"Review our DB connection via URL strings guide: " | ||
"https://jupysql.ploomber.io/en/latest/connecting.html ." | ||
" Using Ubuntu? Check out this guide: " | ||
"https://help.ubuntu.com/community/PostgreSQL#fe_sendauth:_" | ||
"no_password_supplied" | ||
) | ||
|
||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import pytest | ||
|
||
from sqlalchemy.exc import OperationalError | ||
|
||
|
||
def test_syntax_error_no_suggestion(ip, capsys): | ||
ip.run_cell_magic( | ||
"sql", | ||
"", | ||
""" | ||
sqlite:// | ||
SELECT FROM author; | ||
""", | ||
) | ||
out, _ = capsys.readouterr() | ||
assert '(sqlite3.OperationalError) near "FROM": syntax error' in out | ||
assert ( | ||
"If you need help solving this issue, " | ||
"send us a message: https://ploomber.io/community" in out | ||
) | ||
|
||
|
||
def test_syntax_error_description(ip, capsys): | ||
ip.run_cell_magic( | ||
"sql", | ||
"", | ||
""" | ||
sqlite:// | ||
SELECT first_(name FROM author; | ||
""", | ||
) | ||
out, _ = capsys.readouterr() | ||
assert '(sqlite3.OperationalError) near "FROM": syntax error' in out | ||
assert "Syntax Error: Expecting ) at Line 1, Column 20" in out | ||
assert ( | ||
"If you need help solving this issue, " | ||
"send us a message: https://ploomber.io/community" in out | ||
) | ||
|
||
|
||
def test_syntax_error_suggestion(ip, capsys): | ||
ip.run_cell_magic( | ||
"sql", | ||
"", | ||
""" | ||
sqlite:// | ||
ALTER TABLE author RENAME new_author; | ||
""", | ||
) | ||
out, _ = capsys.readouterr() | ||
assert "Did you mean : ['ALTER TABLE author RENAME TO new_author']" in out | ||
assert ( | ||
"If you need help solving this issue, " | ||
"send us a message: https://ploomber.io/community" in out | ||
) | ||
|
||
|
||
def test_query_syntax_error(ip, capsys): | ||
ip.run_line_magic("config", "SqlMagic.short_errors = False") | ||
with pytest.raises(OperationalError): | ||
ip.run_cell_magic( | ||
"sql", | ||
"", | ||
""" | ||
sqlite:// | ||
SELECT FROM author; | ||
""", | ||
) | ||
out, _ = capsys.readouterr() | ||
assert ( | ||
"If you need help solving this issue, " | ||
"send us a message: https://ploomber.io/community" in out | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters