import pytest from django.contrib.auth import get_user_model from django.db import IntegrityError, transaction from review_agent.models import ( Conversation, ExportedSummaryFile, FileAttachment, FileSummaryBatch, FileSummaryBatchAttachment, FileSummaryItem, ) pytestmark = pytest.mark.django_db def create_user(username="u1"): return get_user_model().objects.create_user(username=username, password="pass") def test_attachment_versions_are_unique_per_conversation_and_name(): user = create_user() conversation = Conversation.objects.create(user=user, title="会话") first = FileAttachment.objects.create( conversation=conversation, user=user, original_name="资料.docx", version_no=1, is_active=False, storage_path="media/a.docx", file_size=10, ) second = FileAttachment.objects.create( conversation=conversation, user=user, original_name="资料.docx", version_no=2, storage_path="media/b.docx", file_size=12, ) assert first.version_no == 1 assert second.version_no == 2 with pytest.raises(IntegrityError), transaction.atomic(): FileAttachment.objects.create( conversation=conversation, user=user, original_name="资料.docx", version_no=2, storage_path="media/c.docx", file_size=14, ) def test_batch_attachment_and_item_unique_constraints(): user = create_user() conversation = Conversation.objects.create(user=user, title="会话") attachment = FileAttachment.objects.create( conversation=conversation, user=user, original_name="资料.docx", storage_path="media/a.docx", file_size=10, ) batch = FileSummaryBatch.objects.create( conversation=conversation, user=user, batch_no="FS-001", ) FileSummaryBatchAttachment.objects.create(batch=batch, attachment=attachment) with pytest.raises(IntegrityError), transaction.atomic(): FileSummaryBatchAttachment.objects.create(batch=batch, attachment=attachment) FileSummaryItem.objects.create( batch=batch, file_index=1, file_name="资料.docx", file_type="docx", relative_path="资料.docx", storage_path="media/a.docx", ) with pytest.raises(IntegrityError), transaction.atomic(): FileSummaryItem.objects.create( batch=batch, file_index=2, file_name="资料.docx", file_type="docx", relative_path="资料.docx", storage_path="media/a.docx", ) def test_exported_file_traces_to_user_and_conversation(): user = create_user() conversation = Conversation.objects.create(user=user, title="会话") batch = FileSummaryBatch.objects.create( conversation=conversation, user=user, batch_no="FS-002", ) exported = ExportedSummaryFile.objects.create( batch=batch, export_type=ExportedSummaryFile.ExportType.MARKDOWN, file_name="summary.md", storage_path="media/summary.md", ) assert exported.batch.user == user assert exported.batch.conversation == conversation