generated from john/python-template
Delete Document & Delete Source buttons now delete the underlying files
This commit is contained in:
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
from datetime import UTC
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
@@ -87,8 +88,9 @@ async def test_delete_document_blocks_when_dependencies_exist(default_session_fa
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_document_succeeds_when_unlinked(default_session_factory):
|
||||
async def test_delete_document_succeeds_when_unlinked(default_session_factory, tmp_path):
|
||||
service = DocumentService(session_factory=default_session_factory)
|
||||
service.settings.upload_dir = tmp_path
|
||||
|
||||
document = await service.create_document(
|
||||
Document(
|
||||
@@ -98,12 +100,45 @@ async def test_delete_document_succeeds_when_unlinked(default_session_factory):
|
||||
)
|
||||
)
|
||||
|
||||
document_dir = service.settings.upload_dir / "documents" / str(document.id)
|
||||
document_dir.mkdir(parents=True, exist_ok=True)
|
||||
(document_dir / "leftover.txt").write_text("orphan", encoding="utf-8")
|
||||
|
||||
await service.delete_document(document)
|
||||
|
||||
assert not document_dir.exists()
|
||||
|
||||
with pytest.raises(DocumentError):
|
||||
await service.read_document_detail(document.id)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_document_removes_populated_storage_tree(default_session_factory, tmp_path):
|
||||
service = DocumentService(session_factory=default_session_factory)
|
||||
service.settings.upload_dir = tmp_path
|
||||
|
||||
document = await service.create_document(
|
||||
Document(
|
||||
id=uuid4(),
|
||||
name="tree-delete",
|
||||
document_type="memo",
|
||||
)
|
||||
)
|
||||
|
||||
document_dir = service.settings.upload_dir / "documents" / str(document.id)
|
||||
(document_dir / "page-1.jpg").parent.mkdir(parents=True, exist_ok=True)
|
||||
(document_dir / "page-1.jpg").write_bytes(b"one")
|
||||
(document_dir / "page-2.jpg").write_bytes(b"two")
|
||||
(document_dir / "nested" / "manifest.json").parent.mkdir(parents=True, exist_ok=True)
|
||||
(document_dir / "nested" / "manifest.json").write_text('{"ok": true}', encoding="utf-8")
|
||||
|
||||
assert document_dir.exists()
|
||||
|
||||
await service.delete_document(document)
|
||||
|
||||
assert not document_dir.exists()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_read_person_detail_loads_document_links(default_session_factory):
|
||||
service = DocumentService(session_factory=default_session_factory)
|
||||
|
||||
Reference in New Issue
Block a user