Ability to insert base64 binary data as JSON, closes #126

This commit is contained in:
Simon Willison 2020-07-26 20:59:15 -07:00
commit 1a61a6d3d6
5 changed files with 67 additions and 3 deletions

View file

@ -554,6 +554,18 @@ def test_insert_not_null_default(db_path, tmpdir):
) == db["dogs"].schema
def test_insert_binary_base64(db_path):
result = CliRunner().invoke(
cli.cli,
["insert", db_path, "files", "-"],
input=r'{"content": {"$base64": true, "encoded": "aGVsbG8="}}',
)
assert 0 == result.exit_code, result.output
db = Database(db_path)
actual = db.execute_returning_dicts("select content from files")
assert actual == [{"content": b"hello"}]
def test_insert_newline_delimited(db_path):
result = CliRunner().invoke(
cli.cli,

22
tests/test_utils.py Normal file
View file

@ -0,0 +1,22 @@
from sqlite_utils import utils
import pytest
@pytest.mark.parametrize(
"input,expected,should_be_is",
[
({}, None, True),
({"foo": "bar"}, None, True),
(
{"content": {"$base64": True, "encoded": "aGVsbG8="}},
{"content": b"hello"},
False,
),
],
)
def test_decode_base64_values(input, expected, should_be_is):
actual = utils.decode_base64_values(input)
if should_be_is:
assert actual is input
else:
assert actual == expected