mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-08-01 15:04:11 +02:00
'sqlite-utils triggers' command, closes #218
This commit is contained in:
parent
9a5c92b63e
commit
5b246d17a0
3 changed files with 114 additions and 1 deletions
|
|
@ -5,6 +5,7 @@ import json
|
|||
import os
|
||||
import pytest
|
||||
from sqlite_utils.utils import sqlite3, find_spatialite
|
||||
import textwrap
|
||||
|
||||
from .utils import collapse_whitespace
|
||||
|
||||
|
|
@ -1736,3 +1737,40 @@ def test_search(tmpdir, fts, extra_arg, expected):
|
|||
)
|
||||
assert result.exit_code == 0
|
||||
assert result.output == expected
|
||||
|
||||
|
||||
_TRIGGERS_EXPECTED = '[{"name": "blah", "table": "articles", "sql": "CREATE TRIGGER blah AFTER INSERT ON articles\\nBEGIN\\n UPDATE counter SET count = count + 1;\\nEND"}]\n'
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"extra_args,expected",
|
||||
[([], _TRIGGERS_EXPECTED), (["articles"], _TRIGGERS_EXPECTED), (["counter"], "")],
|
||||
)
|
||||
def test_triggers(tmpdir, extra_args, expected):
|
||||
db_path = str(tmpdir / "test.db")
|
||||
db = Database(db_path)
|
||||
db["articles"].insert(
|
||||
{"id": 1, "title": "Title the first"},
|
||||
pk="id",
|
||||
)
|
||||
db["counter"].insert({"count": 1})
|
||||
db.conn.execute(
|
||||
textwrap.dedent(
|
||||
"""
|
||||
CREATE TRIGGER blah AFTER INSERT ON articles
|
||||
BEGIN
|
||||
UPDATE counter SET count = count + 1;
|
||||
END
|
||||
"""
|
||||
)
|
||||
)
|
||||
args = ["triggers", db_path]
|
||||
if extra_args:
|
||||
args.extend(extra_args)
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
args,
|
||||
catch_exceptions=False,
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert result.output == expected
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue