db.close() method, closes #504

This commit is contained in:
Simon Willison 2022-10-25 13:57:43 -07:00
commit 05e2bb85fc
2 changed files with 18 additions and 0 deletions

View file

@ -336,6 +336,10 @@ class Database:
self._registered_functions: set = set()
self.use_counts_table = use_counts_table
def close(self):
"Close the SQLite connection, and the underlying database file"
self.conn.close()
@contextlib.contextmanager
def tracer(self, tracer: Callable = None):
"""

View file

@ -1,4 +1,6 @@
from sqlite_utils import Database
from sqlite_utils.utils import sqlite3
import pytest
def test_recursive_triggers():
@ -25,3 +27,15 @@ def test_sqlite_version():
as_string = ".".join(map(str, version))
actual = next(db.query("select sqlite_version() as v"))["v"]
assert actual == as_string
@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")