feat: add in-memory job store

This commit is contained in:
2025-11-30 15:15:30 -05:00
parent b64c8c008d
commit 2c2864b566
3 changed files with 75 additions and 0 deletions

18
app/store.py Normal file
View File

@@ -0,0 +1,18 @@
from app.models import Job, JobStatus
class JobStore:
def __init__(self) -> None:
self._jobs: dict[str, Job] = {}
def add(self, job: Job) -> None:
self._jobs[job.id] = job
def get(self, job_id: str) -> Job | None:
return self._jobs.get(job_id)
def list_all(self) -> list[Job]:
return list(self._jobs.values())
def list_by_status(self, status: JobStatus) -> list[Job]:
return [job for job in self._jobs.values() if job.status == status]

View File

@@ -1,3 +1,4 @@
fastapi==0.123.0
uvicorn[standard]==0.38.0
pydantic==2.12.5
pytest==8.3.0

56
tests/test_store.py Normal file
View File

@@ -0,0 +1,56 @@
import pytest
from app.models import Job, JobStatus
from app.store import JobStore
def test_add_and_get_job():
store = JobStore()
job = Job(command="-i input.mp4 output.mp4")
store.add(job)
retrieved = store.get(job.id)
assert retrieved is not None
assert retrieved.id == job.id
assert retrieved.command == job.command
def test_get_nonexistent_job():
store = JobStore()
result = store.get("nonexistent")
assert result is None
def test_list_all_jobs():
store = JobStore()
job1 = Job(command="-i a.mp4 b.mp4")
job2 = Job(command="-i c.mp4 d.mp4")
store.add(job1)
store.add(job2)
jobs = store.list_all()
assert len(jobs) == 2
assert job1 in jobs
assert job2 in jobs
def test_list_jobs_by_status():
store = JobStore()
job1 = Job(command="-i a.mp4 b.mp4")
job2 = Job(command="-i c.mp4 d.mp4")
job2.status = JobStatus.RUNNING
store.add(job1)
store.add(job2)
queued = store.list_by_status(JobStatus.QUEUED)
running = store.list_by_status(JobStatus.RUNNING)
assert len(queued) == 1
assert len(running) == 1
assert queued[0].id == job1.id
assert running[0].id == job2.id