generated from john/python-template
job service tests
This commit is contained in:
@@ -7,7 +7,6 @@ Three models capture the MVP lifecycle:
|
|||||||
from datetime import UTC
|
from datetime import UTC
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
from pathlib import Path
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
@@ -29,7 +28,7 @@ class Document(SQLModel, table=True):
|
|||||||
|
|
||||||
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
filename: str
|
filename: str
|
||||||
file_path: Path
|
file_path: str
|
||||||
uploaded_at: datetime = Field(
|
uploaded_at: datetime = Field(
|
||||||
default_factory=lambda: datetime.now(UTC),
|
default_factory=lambda: datetime.now(UTC),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,20 +2,13 @@ from uuid import uuid4
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from transcription.config import Settings
|
from transcription.models import Document
|
||||||
from transcription.db.runtime import get_session_factory
|
|
||||||
from transcription.models import Job
|
from transcription.models import Job
|
||||||
|
from transcription.services.documents import DocumentService
|
||||||
from transcription.services.jobs import JobService
|
from transcription.services.jobs import JobService
|
||||||
from transcription.services.jobs import JobStatus
|
from transcription.services.jobs import JobStatus
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def job_service(default_settings: Settings) -> JobService:
|
|
||||||
"""Provide a JobService instance for testing."""
|
|
||||||
session_factory = get_session_factory(settings=default_settings)
|
|
||||||
return JobService(session_factory=session_factory)
|
|
||||||
|
|
||||||
|
|
||||||
class TestJobService:
|
class TestJobService:
|
||||||
class TestBasicCRUD:
|
class TestBasicCRUD:
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
@@ -31,6 +24,23 @@ class TestJobService:
|
|||||||
for _ in range(10):
|
for _ in range(10):
|
||||||
await job_service.create_job(job=fake_job_factory(), session=session)
|
await job_service.create_job(job=fake_job_factory(), session=session)
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_backpropagation(self, job_service: JobService, document_service: DocumentService):
|
||||||
|
"""Test that creating a job backpropagates to the related document."""
|
||||||
|
doc_id = uuid4()
|
||||||
|
document = Document(
|
||||||
|
id=doc_id,
|
||||||
|
filename="test.txt",
|
||||||
|
file_path="/path/to/test.txt",
|
||||||
|
)
|
||||||
|
await document_service.create_document(document=document)
|
||||||
|
job = Job(document_id=doc_id)
|
||||||
|
await job_service.create_job(job=job)
|
||||||
|
|
||||||
|
read_job = await job_service.read_job(job_id=job.id)
|
||||||
|
assert isinstance(read_job.document, Document)
|
||||||
|
assert read_job.document.id == document.id
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_reading_job(self, job_service: JobService):
|
async def test_reading_job(self, job_service: JobService):
|
||||||
"""Test reading a job."""
|
"""Test reading a job."""
|
||||||
@@ -67,12 +77,12 @@ class TestJobService:
|
|||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_list_jobs(self, job_service: JobService):
|
async def test_list_jobs(self, job_service: JobService):
|
||||||
"""Test listing jobs."""
|
"""Test listing jobs."""
|
||||||
|
n = 5
|
||||||
|
for _ in range(n):
|
||||||
|
await job_service.create_job(job=Job(document_id=uuid4()))
|
||||||
|
jobs = await job_service.list_jobs()
|
||||||
|
assert len(jobs) == n
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_mark_job_status(self, job_service: JobService):
|
async def test_mark_job_status(self, job_service: JobService):
|
||||||
"""Test marking a job with a new status."""
|
"""Test marking a job with a new status."""
|
||||||
|
|
||||||
class TestMultipleOperations:
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_multiple_operations(self, job_service: JobService):
|
|
||||||
"""Test multiple operations on jobs."""
|
|
||||||
|
|||||||
Reference in New Issue
Block a user