feat(documents): 增强上传反馈与状态展示
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.urls import reverse
|
||||
|
||||
from apps.documents.forms import DocumentUploadForm
|
||||
from apps.documents.models import UploadedDocument
|
||||
from apps.documents.services import extract_text
|
||||
|
||||
@@ -20,6 +21,19 @@ def test_upload_txt_document_creates_uploaded_record(client, db):
|
||||
assert document.scenario_id == "knowledge_qa"
|
||||
|
||||
|
||||
def test_upload_redirect_shows_success_message(client, db):
|
||||
file = SimpleUploadedFile("notice.txt", "hello".encode("utf-8"), content_type="text/plain")
|
||||
|
||||
response = client.post(
|
||||
reverse("documents:upload"),
|
||||
{"scenario_id": "knowledge_qa", "file": file},
|
||||
follow=True,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert "文件已上传,可继续执行入库" in response.content.decode("utf-8")
|
||||
|
||||
|
||||
def test_upload_accepts_pdf_and_docx_documents(client, db):
|
||||
for filename, payload in [
|
||||
("policy.pdf", b"%PDF-1.4\nplain policy text"),
|
||||
@@ -80,3 +94,37 @@ def test_extract_text_supports_pdf_and_docx_plain_text_fallback(db):
|
||||
|
||||
assert "Safety policy" in extract_text(pdf_document)
|
||||
assert "Contract clause review" in extract_text(docx_document)
|
||||
|
||||
|
||||
def test_document_upload_form_builds_scenario_choices():
|
||||
form = DocumentUploadForm()
|
||||
|
||||
choice_values = [value for value, _label in form.fields["scenario_id"].choices]
|
||||
|
||||
assert "knowledge_qa" in choice_values
|
||||
assert "quality_analysis" in choice_values
|
||||
|
||||
|
||||
def test_index_failure_message_is_visible_on_document_list(client, db, monkeypatch):
|
||||
document = UploadedDocument.objects.create(
|
||||
scenario_id="knowledge_qa",
|
||||
original_name="broken.md",
|
||||
file_type="md",
|
||||
size=5,
|
||||
status="uploaded",
|
||||
)
|
||||
|
||||
def fake_index_document(target_document):
|
||||
target_document.status = UploadedDocument.STATUS_FAILED
|
||||
target_document.error_message = "模拟入库失败"
|
||||
target_document.save(update_fields=["status", "error_message", "updated_at"])
|
||||
return target_document
|
||||
|
||||
monkeypatch.setattr("apps.documents.views.index_document", fake_index_document)
|
||||
|
||||
response = client.post(reverse("documents:index", args=[document.id]), follow=True)
|
||||
|
||||
content = response.content.decode("utf-8")
|
||||
assert response.status_code == 200
|
||||
assert "文档入库失败,请检查错误原因后重试" in content
|
||||
assert "模拟入库失败" in content
|
||||
|
||||
Reference in New Issue
Block a user