From 5c9efdafafd7ca0299ad42204edfbb8d32abea99 Mon Sep 17 00:00:00 2001 From: a <4184070+MrCurtis@users.noreply.github.com> Date: Thu, 1 Feb 2024 14:38:44 +0000 Subject: [PATCH] Better error messages --- src/fk_graph/cli.py | 15 ++++++++++++--- tests/test_cli.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/fk_graph/cli.py b/src/fk_graph/cli.py index d2f1267..40a0262 100644 --- a/src/fk_graph/cli.py +++ b/src/fk_graph/cli.py @@ -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 @@ -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 diff --git a/tests/test_cli.py b/tests/test_cli.py index a1a66b3..5ea8ee8 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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 + )