Better error messages for --convert, closes #363

This commit is contained in:
Simon Willison 2022-02-03 14:51:25 -08:00
commit 9dcb099905
2 changed files with 37 additions and 0 deletions

View file

@ -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)

View file

@ -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