From 9dcb099905c4a2246e3487be3289642161991864 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Thu, 3 Feb 2022 14:51:25 -0800 Subject: [PATCH] Better error messages for --convert, closes #363 --- sqlite_utils/cli.py | 11 +++++++++++ tests/test_cli_insert.py | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 771d432..8b1b5a4 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -1009,6 +1009,9 @@ def insert_upsert_implementation( if upsert: extra_kwargs["upsert"] = upsert + # docs should all be dictionaries + docs = (verify_is_dict(doc) for doc in docs) + # Apply {"$base64": true, ...} decoding, if needed docs = (decode_base64_values(doc) for doc in docs) @@ -2760,6 +2763,14 @@ def json_binary(value): raise TypeError +def verify_is_dict(doc): + if not isinstance(doc, dict): + raise click.ClickException( + "Rows must all be dictionaries, got: {}".format(repr(doc)[:1000]) + ) + return doc + + def _load_extensions(db, load_extension): if load_extension: db.conn.enable_load_extension(True) diff --git a/tests/test_cli_insert.py b/tests/test_cli_insert.py index 0d6b482..45ad362 100644 --- a/tests/test_cli_insert.py +++ b/tests/test_cli_insert.py @@ -472,6 +472,32 @@ def test_insert_convert_row_modifying_in_place(db_path): assert rows == [{"name": "Azi", "is_chicken": 1}] +@pytest.mark.parametrize( + "options,expected_error", + ( + ( + ["--text", "--convert", "1"], + "Error: --convert must return dict or iterator\n", + ), + (["--convert", "1"], "Error: Rows must all be dictionaries, got: 1\n"), + ), +) +def test_insert_convert_error_messages(db_path, options, expected_error): + result = CliRunner().invoke( + cli.cli, + [ + "insert", + db_path, + "rows", + "-", + ] + + options, + input='{"name": "Azi"}', + ) + assert result.exit_code == 1 + assert result.output == expected_error + + def test_insert_streaming_batch_size_1(db_path): # https://github.com/simonw/sqlite-utils/issues/364 # Streaming with --batch-size 1 should commit on each record