mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-09 10:04:10 +02:00
Database as a context manager, fixed many pytest warnings
* Database can now work as a context manager * Claude Code helped fix a ton of .close() warnings https://gistpreview.github.io/?730f0c5dc38528a1dd0615f330bd5481 * New autouse fixture to help with test warnings Refs https://github.com/simonw/sqlite-utils/issues/692#issuecomment-3644371889 * Fix all remaining resource warnings https://gistpreview.github.io/?0bb8e869b82f6ff0db647de755182502 Closes #692
This commit is contained in:
parent
066d0f3e8d
commit
fd5b09f64b
12 changed files with 231 additions and 66 deletions
|
|
@ -14,6 +14,26 @@ def pytest_configure(config):
|
|||
sys._called_from_test = True
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def close_all_databases():
|
||||
"""Automatically close all Database objects created during a test."""
|
||||
databases = []
|
||||
original_init = Database.__init__
|
||||
|
||||
def tracking_init(self, *args, **kwargs):
|
||||
original_init(self, *args, **kwargs)
|
||||
databases.append(self)
|
||||
|
||||
Database.__init__ = tracking_init
|
||||
yield
|
||||
Database.__init__ = original_init
|
||||
for db in databases:
|
||||
try:
|
||||
db.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fresh_db():
|
||||
return Database(memory=True)
|
||||
|
|
@ -38,4 +58,5 @@ def db_path(tmpdir):
|
|||
path = str(tmpdir / "test.db")
|
||||
db = sqlite3.connect(path)
|
||||
db.executescript(CREATE_TABLES)
|
||||
db.close()
|
||||
return path
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue