Refactor database access through managed connections
This commit is contained in:
parent
f19bab6fa2
commit
3f28e46ff6
10 changed files with 1327 additions and 716 deletions
|
|
@ -9,6 +9,7 @@ from repub.model import (
|
|||
JobExecution,
|
||||
JobExecutionStatus,
|
||||
create_source,
|
||||
database,
|
||||
initialize_database,
|
||||
)
|
||||
|
||||
|
|
@ -31,15 +32,16 @@ def test_load_runs_view_humanizes_completed_execution_summary_bytes(
|
|||
cron_month="*",
|
||||
feed_url="https://example.com/completed.xml",
|
||||
)
|
||||
job = Job.get(Job.source == source)
|
||||
JobExecution.create(
|
||||
job=job,
|
||||
running_status=JobExecutionStatus.SUCCEEDED,
|
||||
ended_at=datetime(2026, 3, 30, 12, 0, tzinfo=UTC),
|
||||
requests_count=14,
|
||||
items_count=11,
|
||||
bytes_count=16_410_269,
|
||||
)
|
||||
with database.writer():
|
||||
job = Job.get(Job.source == source)
|
||||
JobExecution.create(
|
||||
job=job,
|
||||
running_status=JobExecutionStatus.SUCCEEDED,
|
||||
ended_at=datetime(2026, 3, 30, 12, 0, tzinfo=UTC),
|
||||
requests_count=14,
|
||||
items_count=11,
|
||||
bytes_count=16_410_269,
|
||||
)
|
||||
|
||||
view = load_runs_view(
|
||||
log_dir=tmp_path / "out" / "logs",
|
||||
|
|
@ -67,13 +69,14 @@ def test_load_runs_view_projects_completed_execution_duration(
|
|||
cron_month="*",
|
||||
feed_url="https://example.com/completed.xml",
|
||||
)
|
||||
job = Job.get(Job.source == source)
|
||||
JobExecution.create(
|
||||
job=job,
|
||||
running_status=JobExecutionStatus.SUCCEEDED,
|
||||
started_at=datetime(2026, 3, 30, 11, 59, 12, tzinfo=UTC),
|
||||
ended_at=datetime(2026, 3, 30, 12, 0, tzinfo=UTC),
|
||||
)
|
||||
with database.writer():
|
||||
job = Job.get(Job.source == source)
|
||||
JobExecution.create(
|
||||
job=job,
|
||||
running_status=JobExecutionStatus.SUCCEEDED,
|
||||
started_at=datetime(2026, 3, 30, 11, 59, 12, tzinfo=UTC),
|
||||
ended_at=datetime(2026, 3, 30, 12, 0, tzinfo=UTC),
|
||||
)
|
||||
|
||||
view = load_runs_view(
|
||||
log_dir=tmp_path / "out" / "logs",
|
||||
|
|
@ -101,15 +104,16 @@ def test_load_runs_view_humanizes_running_execution_summary_bytes(
|
|||
cron_month="*",
|
||||
feed_url="https://example.com/running.xml",
|
||||
)
|
||||
job = Job.get(Job.source == source)
|
||||
JobExecution.create(
|
||||
job=job,
|
||||
running_status=JobExecutionStatus.RUNNING,
|
||||
started_at=datetime(2026, 3, 30, 12, 0, tzinfo=UTC),
|
||||
requests_count=14,
|
||||
items_count=11,
|
||||
bytes_count=1_536,
|
||||
)
|
||||
with database.writer():
|
||||
job = Job.get(Job.source == source)
|
||||
JobExecution.create(
|
||||
job=job,
|
||||
running_status=JobExecutionStatus.RUNNING,
|
||||
started_at=datetime(2026, 3, 30, 12, 0, tzinfo=UTC),
|
||||
requests_count=14,
|
||||
items_count=11,
|
||||
bytes_count=1_536,
|
||||
)
|
||||
|
||||
view = load_runs_view(
|
||||
log_dir=tmp_path / "out" / "logs",
|
||||
|
|
@ -137,12 +141,13 @@ def test_load_runs_view_projects_running_execution_duration(
|
|||
cron_month="*",
|
||||
feed_url="https://example.com/running.xml",
|
||||
)
|
||||
job = Job.get(Job.source == source)
|
||||
JobExecution.create(
|
||||
job=job,
|
||||
running_status=JobExecutionStatus.RUNNING,
|
||||
started_at=datetime(2026, 3, 30, 11, 59, 12, tzinfo=UTC),
|
||||
)
|
||||
with database.writer():
|
||||
job = Job.get(Job.source == source)
|
||||
JobExecution.create(
|
||||
job=job,
|
||||
running_status=JobExecutionStatus.RUNNING,
|
||||
started_at=datetime(2026, 3, 30, 11, 59, 12, tzinfo=UTC),
|
||||
)
|
||||
|
||||
view = load_runs_view(
|
||||
log_dir=tmp_path / "out" / "logs",
|
||||
|
|
@ -184,21 +189,22 @@ def test_load_runs_view_projects_queued_executions_in_fifo_order(
|
|||
cron_month="*",
|
||||
feed_url="https://example.com/second.xml",
|
||||
)
|
||||
first_job = Job.get(Job.source == first_source)
|
||||
second_job = Job.get(Job.source == second_source)
|
||||
reference_time = datetime(2026, 3, 30, 12, 30, tzinfo=UTC)
|
||||
first_created_at = reference_time - timedelta(minutes=7)
|
||||
second_created_at = reference_time - timedelta(minutes=3)
|
||||
first_execution = JobExecution.create(
|
||||
job=first_job,
|
||||
created_at=first_created_at,
|
||||
running_status=JobExecutionStatus.PENDING,
|
||||
)
|
||||
second_execution = JobExecution.create(
|
||||
job=second_job,
|
||||
created_at=second_created_at,
|
||||
running_status=JobExecutionStatus.PENDING,
|
||||
)
|
||||
with database.writer():
|
||||
first_job = Job.get(Job.source == first_source)
|
||||
second_job = Job.get(Job.source == second_source)
|
||||
first_execution = JobExecution.create(
|
||||
job=first_job,
|
||||
created_at=first_created_at,
|
||||
running_status=JobExecutionStatus.PENDING,
|
||||
)
|
||||
second_execution = JobExecution.create(
|
||||
job=second_job,
|
||||
created_at=second_created_at,
|
||||
running_status=JobExecutionStatus.PENDING,
|
||||
)
|
||||
|
||||
view = load_runs_view(
|
||||
log_dir=tmp_path / "out" / "logs",
|
||||
|
|
@ -258,12 +264,13 @@ def test_load_runs_view_keeps_queued_jobs_in_scheduled_jobs(
|
|||
cron_month="*",
|
||||
feed_url="https://example.com/scheduled.xml",
|
||||
)
|
||||
queued_job = Job.get(Job.source == queued_source)
|
||||
Job.get(Job.source == scheduled_source)
|
||||
JobExecution.create(
|
||||
job=queued_job,
|
||||
running_status=JobExecutionStatus.PENDING,
|
||||
)
|
||||
with database.writer():
|
||||
queued_job = Job.get(Job.source == queued_source)
|
||||
Job.get(Job.source == scheduled_source)
|
||||
JobExecution.create(
|
||||
job=queued_job,
|
||||
running_status=JobExecutionStatus.PENDING,
|
||||
)
|
||||
|
||||
view = load_runs_view(
|
||||
log_dir=tmp_path / "out" / "logs",
|
||||
|
|
@ -299,17 +306,18 @@ def test_load_runs_view_running_row_targets_queued_follow_up_cancel(
|
|||
cron_month="*",
|
||||
feed_url="https://example.com/running.xml",
|
||||
)
|
||||
job = Job.get(Job.source == source)
|
||||
JobExecution.create(
|
||||
job=job,
|
||||
started_at=datetime(2026, 3, 30, 12, 0, tzinfo=UTC),
|
||||
running_status=JobExecutionStatus.RUNNING,
|
||||
)
|
||||
pending_execution = JobExecution.create(
|
||||
job=job,
|
||||
created_at=datetime(2026, 3, 30, 12, 5, tzinfo=UTC),
|
||||
running_status=JobExecutionStatus.PENDING,
|
||||
)
|
||||
with database.writer():
|
||||
job = Job.get(Job.source == source)
|
||||
JobExecution.create(
|
||||
job=job,
|
||||
started_at=datetime(2026, 3, 30, 12, 0, tzinfo=UTC),
|
||||
running_status=JobExecutionStatus.RUNNING,
|
||||
)
|
||||
pending_execution = JobExecution.create(
|
||||
job=job,
|
||||
created_at=datetime(2026, 3, 30, 12, 5, tzinfo=UTC),
|
||||
running_status=JobExecutionStatus.PENDING,
|
||||
)
|
||||
|
||||
view = load_runs_view(
|
||||
log_dir=tmp_path / "out" / "logs",
|
||||
|
|
@ -341,14 +349,15 @@ def test_load_runs_view_paginates_completed_executions_after_20_rows(
|
|||
cron_month="*",
|
||||
feed_url="https://example.com/completed.xml",
|
||||
)
|
||||
job = Job.get(Job.source == source)
|
||||
base_time = datetime(2026, 3, 30, 12, 0, tzinfo=UTC)
|
||||
for offset in range(21):
|
||||
JobExecution.create(
|
||||
job=job,
|
||||
running_status=JobExecutionStatus.SUCCEEDED,
|
||||
ended_at=base_time - timedelta(minutes=offset),
|
||||
)
|
||||
with database.writer():
|
||||
job = Job.get(Job.source == source)
|
||||
base_time = datetime(2026, 3, 30, 12, 0, tzinfo=UTC)
|
||||
for offset in range(21):
|
||||
JobExecution.create(
|
||||
job=job,
|
||||
running_status=JobExecutionStatus.SUCCEEDED,
|
||||
ended_at=base_time - timedelta(minutes=offset),
|
||||
)
|
||||
|
||||
first_page = load_runs_view(
|
||||
log_dir=tmp_path / "out" / "logs",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue