mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-28 20:04:32 +02:00
JSON output no longer escapes non-ASCII characters, new --ascii option (#777)
Closes #625 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JaHan1NhaTRAxJ9LQtSLf9
This commit is contained in:
parent
d516e58543
commit
815b6a7d3d
6 changed files with 121 additions and 6 deletions
|
|
@ -1047,6 +1047,34 @@ def test_query_json_with_json_cols(db_path):
|
|||
assert expected == result_rows.output.strip()
|
||||
|
||||
|
||||
def test_query_json_unicode_not_escaped_by_default(db_path):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["text"].insert({"id": 1, "text": "Japanese 日本語"}, pk="id")
|
||||
result = CliRunner().invoke(cli.cli, [db_path, "select id, text from text"])
|
||||
assert result.exit_code == 0
|
||||
assert result.output.strip() == '[{"id": 1, "text": "Japanese 日本語"}]'
|
||||
# Same for --nl
|
||||
result = CliRunner().invoke(cli.cli, [db_path, "select id, text from text", "--nl"])
|
||||
assert result.exit_code == 0
|
||||
assert result.output.strip() == '{"id": 1, "text": "Japanese 日本語"}'
|
||||
|
||||
|
||||
@pytest.mark.parametrize("command", ["query", "rows"])
|
||||
def test_query_json_ascii_option(db_path, command):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["text"].insert({"id": 1, "text": "Japanese 日本語"}, pk="id")
|
||||
if command == "query":
|
||||
args = [db_path, "select id, text from text", "--ascii"]
|
||||
else:
|
||||
args = ["rows", db_path, "text", "--ascii"]
|
||||
result = CliRunner().invoke(cli.cli, args)
|
||||
assert result.exit_code == 0
|
||||
expected = '[{"id": 1, "text": "Japanese ' + "\\u65e5\\u672c\\u8a9e" + '"}]'
|
||||
assert result.output.strip() == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"content,is_binary",
|
||||
[(b"\x00\x0fbinary", True), ("this is text", False), (1, False), (1.5, False)],
|
||||
|
|
|
|||
|
|
@ -215,6 +215,25 @@ def test_convert_multi_dryrun(test_db_and_path):
|
|||
)
|
||||
|
||||
|
||||
def test_convert_multi_dryrun_unicode_not_escaped(test_db_and_path):
|
||||
db_path = test_db_and_path[1]
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
[
|
||||
"convert",
|
||||
db_path,
|
||||
"example",
|
||||
"dt",
|
||||
"{'text': 'Japanese 日本語'}",
|
||||
"--dry-run",
|
||||
"--multi",
|
||||
],
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
# Preview should match what jsonify_if_needed() would actually store
|
||||
assert '{"text": "Japanese 日本語"}' in result.output
|
||||
|
||||
|
||||
@pytest.mark.parametrize("drop", (True, False))
|
||||
def test_convert_output_column(test_db_and_path, drop):
|
||||
db, db_path = test_db_and_path
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue