Refactor database access through managed connections

This commit is contained in:
Abel Luck 2026-03-31 17:30:07 +02:00
parent f19bab6fa2
commit 3f28e46ff6
10 changed files with 1327 additions and 716 deletions

View file

@ -112,8 +112,7 @@ def test_initialize_database_configures_sqlite_pragmas(tmp_path: Path) -> None:
initialize_database(db_path)
database.connect(reuse_if_open=True)
try:
with database.reader_conn():
pragma_values = {
"cache_size": database.execute_sql("PRAGMA cache_size").fetchone()[0],
"page_size": database.execute_sql("PRAGMA page_size").fetchone()[0],
@ -132,8 +131,6 @@ def test_initialize_database_configures_sqlite_pragmas(tmp_path: Path) -> None:
"foreign_keys": 1,
"busy_timeout": 5000,
}
finally:
database.close()
def test_initialize_database_creates_scheduler_and_execution_indexes(
@ -208,34 +205,35 @@ def test_initialize_database_creates_run_queue_indexes(tmp_path: Path) -> None:
def test_job_table_allows_exactly_one_job_per_source(tmp_path: Path) -> None:
initialize_database(tmp_path / "jobs.db")
source = Source.create(
name="Guardian feed mirror",
slug="guardian-feed",
source_type="feed",
)
Job.create(
source=source,
enabled=True,
spider_arguments="",
cron_minute="15",
cron_hour="*",
cron_day_of_month="*",
cron_day_of_week="*",
cron_month="*",
)
with pytest.raises(IntegrityError):
with database.writer():
source = Source.create(
name="Guardian feed mirror",
slug="guardian-feed",
source_type="feed",
)
Job.create(
source=source,
enabled=True,
spider_arguments="language=en",
cron_minute="30",
spider_arguments="",
cron_minute="15",
cron_hour="*",
cron_day_of_month="*",
cron_day_of_week="*",
cron_month="*",
)
with pytest.raises(IntegrityError):
Job.create(
source=source,
enabled=True,
spider_arguments="language=en",
cron_minute="30",
cron_hour="*",
cron_day_of_month="*",
cron_day_of_week="*",
cron_month="*",
)
def test_load_max_concurrent_jobs_defaults_to_one(tmp_path: Path) -> None:
initialize_database(tmp_path / "settings-defaults.db")
@ -248,7 +246,8 @@ def test_save_setting_persists_json_value(tmp_path: Path) -> None:
save_setting("max_concurrent_jobs", 4)
row = AppSetting.get(AppSetting.key == "max_concurrent_jobs")
with database.reader():
row = AppSetting.get(AppSetting.key == "max_concurrent_jobs")
assert row.value == "4"
assert load_max_concurrent_jobs() == 4