sqlite-utils json dogs.db "select * from dogs"

This commit is contained in:
Simon Willison 2019-01-25 18:06:29 -08:00
commit 5466c9745d
4 changed files with 158 additions and 6 deletions

View file

@ -151,3 +151,39 @@ def test_csv(db_path):
cli.cli, ["csv", db_path, "select id, name, age from dogs", "--no-headers"]
)
assert "1,Cleo,4\n2,Pancakes,2\n" == result.output
@pytest.mark.parametrize(
"args,expected",
[
(
[],
'[{"id": 1, "name": "Cleo", "age": 4},\n {"id": 2, "name": "Pancakes", "age": 2}]',
),
(
["--nl"],
'{"id": 1, "name": "Cleo", "age": 4}\n{"id": 2, "name": "Pancakes", "age": 2}',
),
(["--arrays"], '[[1, "Cleo", 4],\n [2, "Pancakes", 2]]'),
(["--arrays", "--nl"], '[1, "Cleo", 4]\n[2, "Pancakes", 2]'),
],
)
def test_json(db_path, args, expected):
db = Database(db_path)
with db.conn:
db["dogs"].insert_all(
[
{"id": 1, "age": 4, "name": "Cleo"},
{"id": 2, "age": 2, "name": "Pancakes"},
]
)
result = CliRunner().invoke(
cli.cli, ["csv", db_path, "select id, name, age from dogs"]
)
assert 0 == result.exit_code
assert "id,name,age\n1,Cleo,4\n2,Pancakes,2\n" == result.output
# Test the no-headers option:
result = CliRunner().invoke(
cli.cli, ["json", db_path, "select id, name, age from dogs"] + args
)
assert expected == result.output.strip()