sqlite-utils duplicate command, closes #454, refs #449

Also made it so .duplicate() raises new NoTable exception rather than raising
an AssertionError if the source table does not exist.
This commit is contained in:
Simon Willison 2022-07-15 14:45:14 -07:00
commit c710ade644
7 changed files with 80 additions and 3 deletions

View file

@ -2129,3 +2129,26 @@ def test_analyze(tmpdir, options, expected):
result = CliRunner().invoke(cli.cli, ["analyze", db_path] + options)
assert result.exit_code == 0
assert list(db["sqlite_stat1"].rows) == expected
def test_duplicate_table(tmpdir):
db_path = str(tmpdir / "test.db")
db = Database(db_path)
db["one"].insert({"id": 1, "name": "Cleo"}, pk="id")
# First try a non-existent table
result_error = CliRunner().invoke(
cli.cli,
["duplicate", db_path, "missing", "two"],
catch_exceptions=False,
)
assert result_error.exit_code == 1
assert result_error.output == 'Error: Table "missing" does not exist\n'
# Now try for a table that exists
result = CliRunner().invoke(
cli.cli,
["duplicate", db_path, "one", "two"],
catch_exceptions=False,
)
assert result.exit_code == 0
assert db["one"].columns_dict == db["two"].columns_dict
assert list(db["one"].rows) == list(db["two"].rows)

View file

@ -1,3 +1,4 @@
from sqlite_utils.db import NoTable
import datetime
import pytest
@ -38,5 +39,5 @@ def test_duplicate(fresh_db):
def test_duplicate_fails_if_table_does_not_exist(fresh_db):
with pytest.raises(AssertionError):
with pytest.raises(NoTable):
fresh_db["not_a_table"].duplicate("duplicated")