2020-09-07 13:45:06 -07:00
|
|
|
from sqlite_utils import Database
|
2026-07-04 22:38:31 +00:00
|
|
|
from sqlite_utils.db import TransactionError
|
2022-10-25 13:57:43 -07:00
|
|
|
from sqlite_utils.utils import sqlite3
|
|
|
|
|
import pytest
|
2026-07-04 22:38:31 +00:00
|
|
|
import sys
|
2020-09-07 13:45:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_recursive_triggers():
|
|
|
|
|
db = Database(memory=True)
|
2020-09-07 14:56:59 -07:00
|
|
|
assert db.execute("PRAGMA recursive_triggers").fetchone()[0]
|
2020-09-07 13:45:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_recursive_triggers_off():
|
|
|
|
|
db = Database(memory=True, recursive_triggers=False)
|
2020-09-07 14:56:59 -07:00
|
|
|
assert not db.execute("PRAGMA recursive_triggers").fetchone()[0]
|
2022-02-15 17:21:25 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_memory_name():
|
|
|
|
|
db1 = Database(memory_name="shared")
|
|
|
|
|
db2 = Database(memory_name="shared")
|
|
|
|
|
db1["dogs"].insert({"name": "Cleo"})
|
|
|
|
|
assert list(db2["dogs"].rows) == [{"name": "Cleo"}]
|
2022-03-01 16:24:27 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sqlite_version():
|
|
|
|
|
db = Database(memory=True)
|
|
|
|
|
version = db.sqlite_version
|
|
|
|
|
assert isinstance(version, tuple)
|
|
|
|
|
as_string = ".".join(map(str, version))
|
|
|
|
|
actual = next(db.query("select sqlite_version() as v"))["v"]
|
|
|
|
|
assert actual == as_string
|
2022-10-25 13:57:43 -07:00
|
|
|
|
|
|
|
|
|
2026-07-04 19:05:59 +00:00
|
|
|
def test_database_context_manager(tmpdir):
|
|
|
|
|
path = str(tmpdir / "test.db")
|
|
|
|
|
with Database(path) as db:
|
|
|
|
|
db["t"].insert({"id": 1})
|
2026-07-04 23:15:01 +00:00
|
|
|
# Raw writes commit automatically too
|
2026-07-04 19:05:59 +00:00
|
|
|
db.execute("insert into t (id) values (2)")
|
2026-07-04 23:15:01 +00:00
|
|
|
# An explicitly opened transaction left uncommitted on purpose:
|
|
|
|
|
db.begin()
|
|
|
|
|
db.execute("insert into t (id) values (3)")
|
2026-07-04 19:05:59 +00:00
|
|
|
# The connection is closed...
|
|
|
|
|
with pytest.raises(sqlite3.ProgrammingError):
|
|
|
|
|
db.execute("select 1")
|
2026-07-04 23:15:01 +00:00
|
|
|
# ... and the open explicit transaction was rolled back, not committed
|
2026-07-04 19:05:59 +00:00
|
|
|
db2 = Database(path)
|
2026-07-04 23:15:01 +00:00
|
|
|
assert [r["id"] for r in db2["t"].rows] == [1, 2]
|
2026-07-04 19:05:59 +00:00
|
|
|
db2.close()
|
|
|
|
|
|
|
|
|
|
|
2022-10-25 13:57:43 -07:00
|
|
|
@pytest.mark.parametrize("memory", [True, False])
|
|
|
|
|
def test_database_close(tmpdir, memory):
|
|
|
|
|
if memory:
|
|
|
|
|
db = Database(memory=True)
|
|
|
|
|
else:
|
|
|
|
|
db = Database(str(tmpdir / "test.db"))
|
|
|
|
|
assert db.execute("select 1 + 1").fetchone()[0] == 2
|
|
|
|
|
db.close()
|
|
|
|
|
with pytest.raises(sqlite3.ProgrammingError):
|
|
|
|
|
db.execute("select 1 + 1")
|
2026-07-04 22:38:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
|
sys.version_info < (3, 12),
|
|
|
|
|
reason="sqlite3.connect(autocommit=) requires Python 3.12",
|
|
|
|
|
)
|
|
|
|
|
@pytest.mark.parametrize("autocommit", [True, False])
|
|
|
|
|
def test_autocommit_connections_are_rejected(tmpdir, autocommit):
|
|
|
|
|
# These connection modes break commit()/rollback() in ways that
|
|
|
|
|
# silently lose data, so the constructor refuses them
|
|
|
|
|
conn = sqlite3.connect(str(tmpdir / "test.db"), autocommit=autocommit)
|
|
|
|
|
with pytest.raises(TransactionError):
|
|
|
|
|
Database(conn)
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
|
sys.version_info < (3, 12),
|
|
|
|
|
reason="sqlite3.LEGACY_TRANSACTION_CONTROL requires Python 3.12",
|
|
|
|
|
)
|
|
|
|
|
def test_legacy_transaction_control_connection_is_accepted(tmpdir):
|
|
|
|
|
conn = sqlite3.connect(
|
|
|
|
|
str(tmpdir / "test.db"), autocommit=sqlite3.LEGACY_TRANSACTION_CONTROL
|
|
|
|
|
)
|
|
|
|
|
db = Database(conn)
|
|
|
|
|
db["t"].insert({"id": 1}, pk="id")
|
|
|
|
|
assert [r["id"] for r in db["t"].rows] == [1]
|
|
|
|
|
db.close()
|