mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-16 13:34:10 +02:00
Implement --save plus tests for --save and --dump, refs #272
This commit is contained in:
parent
c7234cae83
commit
988d363657
2 changed files with 50 additions and 0 deletions
|
|
@ -1117,6 +1117,11 @@ def query(
|
|||
help="Named :parameters for SQL query",
|
||||
)
|
||||
@click.option("--dump", is_flag=True, help="Dump SQL for in-memory database")
|
||||
@click.option(
|
||||
"--save",
|
||||
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
|
||||
help="Save in-memory database to this file",
|
||||
)
|
||||
@load_extension_option
|
||||
def memory(
|
||||
paths,
|
||||
|
|
@ -1133,6 +1138,7 @@ def memory(
|
|||
raw,
|
||||
param,
|
||||
dump,
|
||||
save,
|
||||
load_extension,
|
||||
):
|
||||
"Execute SQL query against an in-memory database, optionally populated by imported data"
|
||||
|
|
@ -1159,6 +1165,12 @@ def memory(
|
|||
click.echo(line)
|
||||
return
|
||||
|
||||
if save:
|
||||
db2 = sqlite_utils.Database(save)
|
||||
for line in db.conn.iterdump():
|
||||
db2.execute(line)
|
||||
return
|
||||
|
||||
for alias, attach_path in attach:
|
||||
db.attach(alias, attach_path)
|
||||
_load_extensions(db, load_extension)
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import pytest
|
|||
|
||||
def test_memory_basic():
|
||||
result = CliRunner().invoke(cli.cli, ["memory", "select 1 + 1"])
|
||||
assert result.exit_code == 0
|
||||
assert result.output.strip() == '[{"1 + 1": 2}]'
|
||||
|
||||
|
||||
|
|
@ -26,7 +27,44 @@ def test_memory_csv(tmpdir, sql_from, use_stdin):
|
|||
["memory", csv_path, "select * from {}".format(sql_from), "--nl"],
|
||||
input=input,
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert (
|
||||
result.output.strip()
|
||||
== '{"id": "1", "name": "Cleo"}\n{"id": "2", "name": "Bants"}'
|
||||
)
|
||||
|
||||
|
||||
def test_memory_dump():
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
["memory", "-", "select 1", "--dump"],
|
||||
input="id,name\n1,Cleo\n2,Bants",
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert result.output.strip() == (
|
||||
"BEGIN TRANSACTION;\n"
|
||||
"CREATE TABLE [stdin] (\n"
|
||||
" [id] TEXT,\n"
|
||||
" [name] TEXT\n"
|
||||
");\n"
|
||||
"INSERT INTO \"stdin\" VALUES('1','Cleo');\n"
|
||||
"INSERT INTO \"stdin\" VALUES('2','Bants');\n"
|
||||
"CREATE VIEW t1 AS select * from [stdin];\n"
|
||||
"CREATE VIEW t AS select * from [stdin];\n"
|
||||
"COMMIT;"
|
||||
)
|
||||
|
||||
|
||||
def test_memory_save(tmpdir):
|
||||
save_to = str(tmpdir / "save.db")
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
["memory", "-", "select 1", "--save", save_to],
|
||||
input="id,name\n1,Cleo\n2,Bants",
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
db = Database(save_to)
|
||||
assert list(db["stdin"].rows) == [
|
||||
{"id": "1", "name": "Cleo"},
|
||||
{"id": "2", "name": "Bants"},
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue