-p for passing named params to query, closes #124

This commit is contained in:
Simon Willison 2020-07-26 20:53:51 -07:00
commit 814d4a7f90
3 changed files with 35 additions and 2 deletions

View file

@ -789,6 +789,27 @@ def test_query_json_binary(db_path):
]
@pytest.mark.parametrize(
"sql,params,expected",
[
("select 1 + 1 as out", {"p": "2"}, 2),
("select 1 + :p as out", {"p": "2"}, 3),
(
"select :hello as out",
{"hello": """This"has'many'quote"s"""},
"""This"has'many'quote"s""",
),
],
)
def test_query_params(db_path, sql, params, expected):
extra_args = []
for key, value in params.items():
extra_args.extend(["-p", key, value])
result = CliRunner().invoke(cli.cli, [db_path, sql] + extra_args)
assert result.exit_code == 0, str(result)
assert json.loads(result.output.strip()) == [{"out": expected}]
def test_query_json_with_json_cols(db_path):
db = Database(db_path)
with db.conn: