Skip to content

Commit

Permalink
Better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCurtis committed Feb 1, 2024
1 parent 5438edd commit 5c9efda
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/fk_graph/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from argparse import ArgumentParser
from json import loads
from argparse import ArgumentParser, ArgumentTypeError
from json import JSONDecodeError, loads

from sqlalchemy import create_engine

Expand Down Expand Up @@ -47,4 +47,13 @@ def _parse_args():
return args

def _to_list(json_input):
return loads(json_input)
argument_type_error = ArgumentTypeError(
"The --only-tables argument should be a list-like JSON string."
)
try:
parsed = loads(json_input)
except JSONDecodeError:
raise argument_type_error
if not isinstance(parsed, list):
raise argument_type_error
return parsed
40 changes: 40 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,43 @@ def test_can_pass_in_list_of_tables_to_include(self):
# Because of the javascripty-nature of the app, we can
# only really inspect the status code.
self.assertEqual(response.status_code, 200)

def test_errors_if_only_tables_is_not_json(self):
command = (
"fk-graph"
" --demo"
" --table=table_a"
" --primary-key=1"
" --only-tables='not json'"
)
completed_process = run(
shlex_split(command),
capture_output=True,
text=True,
timeout=5
)
self.assertEqual(completed_process.returncode, 2)
self.assertIn(
"The --only-tables argument should be a list-like JSON string.",
completed_process.stderr
)

def test_errors_if_only_tables_is_not_listlike_json(self):
command = (
"fk-graph"
" --demo"
" --table=table_a"
" --primary-key=1"
" --only-tables='{\"not\": \"a list\"}'"
)
completed_process = run(
shlex_split(command),
capture_output=True,
text=True,
timeout=5
)
self.assertEqual(completed_process.returncode, 2)
self.assertIn(
"The --only-tables argument should be a list-like JSON string.",
completed_process.stderr
)

0 comments on commit 5c9efda

Please sign in to comment.