mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-11 11:04:10 +02:00
insert-files: support fixed value columns via -c colname:text:value
Extends the -c/--column spec so a column can be populated with a literal value for every inserted file, e.g. -c file_type:text:gif, without a separate update pass. Existing colname:coldef shorthands and compound --pk behavior are unchanged. Closes #140
This commit is contained in:
parent
6a456830ca
commit
71664b8fcf
3 changed files with 74 additions and 2 deletions
|
|
@ -117,6 +117,60 @@ def test_insert_files(silent, pk_args, expected_pks):
|
|||
assert set(db["files"].pks) == set(expected_pks)
|
||||
|
||||
|
||||
def test_insert_files_fixed_value_column():
|
||||
runner = CliRunner()
|
||||
with runner.isolated_filesystem():
|
||||
tmpdir = pathlib.Path(".")
|
||||
db_path = str(tmpdir / "files.db")
|
||||
(tmpdir / "one.gif").write_text("gif content", "utf-8")
|
||||
result = runner.invoke(
|
||||
cli.cli,
|
||||
[
|
||||
"insert-files",
|
||||
db_path,
|
||||
"files",
|
||||
str(tmpdir / "one.gif"),
|
||||
"-c",
|
||||
"path",
|
||||
"-c",
|
||||
"content",
|
||||
"-c",
|
||||
"file_type:text:gif",
|
||||
"--pk",
|
||||
"path",
|
||||
"--pk",
|
||||
"file_type",
|
||||
],
|
||||
catch_exceptions=False,
|
||||
)
|
||||
assert result.exit_code == 0, result.output
|
||||
db = Database(db_path)
|
||||
row = next(iter(db["files"].rows))
|
||||
assert row["file_type"] == "gif"
|
||||
assert set(db["files"].pks) == {"path", "file_type"}
|
||||
|
||||
|
||||
def test_insert_files_fixed_value_column_bad_type():
|
||||
runner = CliRunner()
|
||||
with runner.isolated_filesystem():
|
||||
tmpdir = pathlib.Path(".")
|
||||
db_path = str(tmpdir / "files.db")
|
||||
(tmpdir / "one.gif").write_text("gif content", "utf-8")
|
||||
result = runner.invoke(
|
||||
cli.cli,
|
||||
[
|
||||
"insert-files",
|
||||
db_path,
|
||||
"files",
|
||||
str(tmpdir / "one.gif"),
|
||||
"-c",
|
||||
"file_type:mtime:gif",
|
||||
],
|
||||
)
|
||||
assert result.exit_code == 1
|
||||
assert "colname:text:value" in result.output
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"use_text,encoding,input,expected",
|
||||
(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue