generated from john/python-template
V4 implemented. Some tweaking left, but it is working
This commit is contained in:
@@ -6,12 +6,15 @@ from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from sqlmodel import select
|
||||
|
||||
from transcription.db.models import Document
|
||||
from transcription.db.models import Job
|
||||
from transcription.db.models import DocumentPerson
|
||||
from transcription.db.models import DocumentPersonRole
|
||||
from transcription.db.models import DocumentType
|
||||
from transcription.db.models import Person
|
||||
from transcription.db.models import PersonRole
|
||||
from transcription.db.models import Source
|
||||
from transcription.services.documents import DocumentDeleteBlockedError
|
||||
from transcription.services.documents import DocumentError
|
||||
@@ -34,6 +37,9 @@ async def test_read_document_detail_allows_missing_sources(default_session_facto
|
||||
|
||||
assert detail.id == created.id
|
||||
assert detail.sources == []
|
||||
assert detail.document_type_id is not None
|
||||
assert detail.document_type_ref is not None
|
||||
assert detail.document_type_ref.code == "letter"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -135,6 +141,12 @@ async def test_delete_document_removes_person_links(default_session_factory, tmp
|
||||
)
|
||||
)
|
||||
|
||||
links_before_delete = await service.list_document_people(document_id=document.id)
|
||||
assert len(links_before_delete) == 1
|
||||
assert links_before_delete[0].role_id is not None
|
||||
assert links_before_delete[0].role_ref is not None
|
||||
assert links_before_delete[0].role_ref.code == "author"
|
||||
|
||||
document_dir = service.settings.upload_dir / "documents" / str(document.id)
|
||||
document_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@@ -260,3 +272,52 @@ async def test_delete_person_succeeds_when_unlinked(default_session_factory):
|
||||
|
||||
with pytest.raises(DocumentError):
|
||||
await service.read_person_detail(person.id)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_document_reuses_existing_document_type_registry(default_session_factory):
|
||||
service = DocumentService(session_factory=default_session_factory)
|
||||
|
||||
async with service._session_scope() as session:
|
||||
existing = DocumentType(code="record", label="Record")
|
||||
session.add(existing)
|
||||
await session.commit()
|
||||
await session.refresh(existing)
|
||||
|
||||
created = await service.create_document(Document(id=uuid4(), name="typed-doc", document_type="record"))
|
||||
|
||||
assert created.document_type_id is not None
|
||||
assert created.document_type_id == existing.id
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_document_person_sets_role_id_from_legacy_role(default_session_factory):
|
||||
service = DocumentService(session_factory=default_session_factory)
|
||||
|
||||
document = await service.create_document(Document(id=uuid4(), name="role-sync-doc", document_type="letter"))
|
||||
person = await service.create_person(Person(full_name="Role Sync Person"))
|
||||
link = await service.create_document_person(
|
||||
DocumentPerson(
|
||||
document_id=document.id,
|
||||
person_id=person.id,
|
||||
role=DocumentPersonRole.AUTHOR,
|
||||
)
|
||||
)
|
||||
|
||||
updated = await service.update_document_person(
|
||||
DocumentPerson(
|
||||
id=link.id,
|
||||
document_id=document.id,
|
||||
person_id=person.id,
|
||||
role=DocumentPersonRole.RECIPIENT,
|
||||
role_id=None,
|
||||
)
|
||||
)
|
||||
|
||||
assert updated.role == DocumentPersonRole.RECIPIENT
|
||||
assert updated.role_id is not None
|
||||
|
||||
async with service._session_scope() as session:
|
||||
recipient_role = (await session.exec(select(PersonRole).where(PersonRole.code == "recipient"))).first()
|
||||
assert recipient_role is not None
|
||||
assert updated.role_id == recipient_role.id
|
||||
|
||||
@@ -24,6 +24,8 @@ async def test_document_service_handles_person_and_document_person_crud(default_
|
||||
document = await documents.create_document(Document(id=uuid4(), name="person-doc"))
|
||||
person = await documents.create_person(Person(full_name="Ada Lovelace"))
|
||||
|
||||
assert document.document_type_id is None
|
||||
|
||||
link = await documents.create_document_person(
|
||||
DocumentPerson(document_id=document.id, person_id=person.id, role=DocumentPersonRole.AUTHOR)
|
||||
)
|
||||
@@ -31,11 +33,13 @@ async def test_document_service_handles_person_and_document_person_crud(default_
|
||||
fetched = await documents.read_document_person(link.id)
|
||||
assert fetched.id == link.id
|
||||
assert fetched.role == DocumentPersonRole.AUTHOR
|
||||
assert fetched.role_id is not None
|
||||
|
||||
updated_link = await documents.update_document_person(
|
||||
DocumentPerson(id=link.id, document_id=document.id, person_id=person.id, role=DocumentPersonRole.RECIPIENT)
|
||||
)
|
||||
assert updated_link.role == DocumentPersonRole.RECIPIENT
|
||||
assert updated_link.role_id is not None
|
||||
|
||||
listed = await documents.list_document_people(document_id=document.id)
|
||||
assert len(listed) == 1
|
||||
|
||||
Reference in New Issue
Block a user