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

Validating CLI arguments #561

Merged
merged 25 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ae2f54d
Validating CLI arguments
tl3119 May 31, 2023
2be9e6a
Merge branch 'master' of https://github.com/tl3119/jupysql
tl3119 May 31, 2023
38be8a0
Validating CLI arguments
tl3119 Jun 1, 2023
cd093c2
Merge pull request #1 from ploomber/master
tl3119 Jun 1, 2023
a52c5e6
remove rec_arguments array and add tests for line magic and cell magic
tl3119 Jun 1, 2023
acc2f06
Merge branch 'master' of https://github.com/tl3119/jupysql
tl3119 Jun 1, 2023
f06d7cc
Merge pull request #2 from ploomber/master
tl3119 Jun 1, 2023
08a540f
Update docs to use --alias
tl3119 Jun 2, 2023
3fca85d
Merge branch 'master' of https://github.com/tl3119/jupysql
tl3119 Jun 2, 2023
beec9e2
Remove pinned pydata version
tl3119 Jun 5, 2023
16f794f
Validating CLI arguments update
tl3119 Jun 5, 2023
3055c77
Update PR
tl3119 Jun 5, 2023
79efad6
Update PR, fix irrelevant issues
tl3119 Jun 6, 2023
7200eef
Update magic logic and test
tl3119 Jun 6, 2023
90ed427
Update test_magic.py
tl3119 Jun 7, 2023
219a47c
update PR
tl3119 Jun 7, 2023
553f49f
update PR
tl3119 Jun 7, 2023
43b35c3
Merge pull request #3 from ploomber/master
tl3119 Jun 7, 2023
7203acf
fix check only for cell magic
tl3119 Jun 7, 2023
468f365
Merge branch 'master' of https://github.com/tl3119/jupysql
tl3119 Jun 7, 2023
a410d91
Merge pull request #5 from ploomber/master
tl3119 Jun 7, 2023
68310ea
pass multiple arguments to pytest parameterized
tl3119 Jun 7, 2023
e5d4e9b
Merge branch 'master' of https://github.com/tl3119/jupysql
tl3119 Jun 7, 2023
cb8b636
Update PR
tl3119 Jun 7, 2023
42e4f24
Merge pull request #6 from ploomber/master
tl3119 Jun 7, 2023
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
35 changes: 35 additions & 0 deletions src/sql/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@


SUPPORT_INTERACTIVE_WIDGETS = ["Checkbox", "Text", "IntSlider", ""]
recognized_arguments = [
"--connections",
"--close",
"--creator",
"--section",
"--persist",
"--persist-replace",
"--no-index",
"--append",
"--connection_arguments",
"--file",
"--save",
"--with",
"--no-execute",
"--alias",
"--interact",
tl3119 marked this conversation as resolved.
Show resolved Hide resolved
]


@magics_class
Expand Down Expand Up @@ -311,6 +328,24 @@ def interactive_execute_wrapper(**kwargs):

# %%sql {line}
# {cell}

# Split the line into tokens separated by whitespace
tokens = line.split()
arguments = []

# Iterate through the tokens to separate arguments and SQL code
# If the token starts with "--", it is an argument
# Otherwise, it is part of the SQL code
for token in tokens:
if token.startswith("--"):
arguments.append(token)

for check_argument in arguments:
if check_argument not in recognized_arguments:
raise exceptions.UsageError(
"Unrecognized argument(s): {}".format(check_argument)
)

if local_ns is None:
local_ns = {}

Expand Down
5 changes: 5 additions & 0 deletions src/tests/test_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ def test_persist_no_index(ip):
assert persisted == [(1, "foo"), (2, "bar")]


def test_unrecognized_arguments(ip):
result = ip.run_cell("%sql --stuff sqlite://")
tl3119 marked this conversation as resolved.
Show resolved Hide resolved
assert "Unrecognized argument(s)" in str(result.error_in_exec)

tl3119 marked this conversation as resolved.
Show resolved Hide resolved

def test_persist_invalid_identifier(ip):
result = ip.run_cell("%sql --persist sqlite:// not an identifier")
assert "not a valid identifier" in str(result.error_in_exec)
Expand Down