forked from mtusi2026/mtusi2026lab4
55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
"""Проверка компенсации объекта при ошибке commit метаданных."""
|
|
import asyncio
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
|
|
from app.domain.models import User
|
|
from app.services.attachments import AttachmentService
|
|
|
|
|
|
class FailingRepository:
|
|
async def chat_exists_for_member(self, chat_id, user_id):
|
|
return True
|
|
|
|
async def create(self, **kwargs):
|
|
raise RuntimeError('injected database commit failure')
|
|
|
|
|
|
class RecordingStorage:
|
|
def __init__(self):
|
|
self.put_key = None
|
|
self.removed_key = None
|
|
|
|
async def put(self, key, source, length, content_type):
|
|
self.put_key = key
|
|
|
|
async def remove(self, key):
|
|
self.removed_key = key
|
|
|
|
|
|
def test_uploaded_object_is_removed_when_metadata_commit_fails():
|
|
storage = RecordingStorage()
|
|
service = AttachmentService(FailingRepository(), storage, 10 * 1024 * 1024, 40_000_000)
|
|
actor = User(uuid4(), 'tester', 'Tester', datetime.now(timezone.utc))
|
|
content = (Path(__file__).parent / 'fixtures/sample.png').read_bytes()
|
|
|
|
async def chunks():
|
|
yield content
|
|
|
|
with pytest.raises(RuntimeError, match='injected database commit failure'):
|
|
asyncio.run(
|
|
service.upload(
|
|
actor,
|
|
uuid4(),
|
|
'sample.png',
|
|
'image/png',
|
|
chunks(),
|
|
)
|
|
)
|
|
|
|
assert storage.put_key is not None
|
|
assert storage.removed_key == storage.put_key
|