refactor(kb): 重构kb api
This commit is contained in:
1 parent
b476b58a91
commit
d5e76dee36
30 files changed
+2713
-1691
No files matched your search
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
tests/services/kb/test_document_service.py
|
||||
|
||||
用途:document_service 文档加载、元数据校验与软删单元测试。
|
||||
"""
|
||||
import pytest
|
||||
|
||||
from app import db
|
||||
from app.core.errors import NotFoundError, ValidationError
|
||||
from app.models import KbDocument, KbDocumentVersion, KbGroup, KbJob
|
||||
from app.services.kb import document_service
|
||||
|
||||
|
||||
def test_load_document_not_found(app):
|
||||
with app.app_context():
|
||||
with pytest.raises(NotFoundError):
|
||||
document_service.load_document(99999)
|
||||
|
||||
|
||||
def test_update_metadata_rejects_duplicate_title(app):
|
||||
with app.app_context():
|
||||
group = KbGroup(name='doc-svc-dup')
|
||||
doc_a = KbDocument(group=group, title='alpha.pdf', status='uploaded')
|
||||
doc_b = KbDocument(group=group, title='beta.pdf', status='uploaded')
|
||||
db.session.add_all([group, doc_a, doc_b])
|
||||
db.session.commit()
|
||||
|
||||
with pytest.raises(ValidationError, match='同名文档'):
|
||||
document_service.update_metadata(doc_b.id, {'title': 'alpha.pdf'}, user_id=1)
|
||||
|
||||
|
||||
def test_soft_delete_document_cancels_active_jobs(app):
|
||||
with app.app_context():
|
||||
group = KbGroup(name='doc-svc-delete')
|
||||
document = KbDocument(group=group, title='doc.pdf', status='uploaded')
|
||||
version = KbDocumentVersion(
|
||||
document=document,
|
||||
version_no=1,
|
||||
status='uploaded',
|
||||
is_current=True,
|
||||
source_file_name='doc.pdf',
|
||||
source_file_path='data/kb/doc.pdf',
|
||||
)
|
||||
queued = KbJob(
|
||||
job_type='clean',
|
||||
status='queued',
|
||||
group_id=group.id,
|
||||
document_id=document.id,
|
||||
document_version_id=version.id,
|
||||
progress=0,
|
||||
)
|
||||
db.session.add_all([group, document, version, queued])
|
||||
db.session.commit()
|
||||
|
||||
assert document_service.soft_delete_document(document.id, user_id=1) is True
|
||||
db.session.commit()
|
||||
|
||||
refreshed = db.session.get(KbDocument, document.id)
|
||||
assert refreshed.is_deleted is True
|
||||
assert db.session.get(KbJob, queued.id).status == 'cancelled'
|
||||
@@ -0,0 +1,40 @@
|
||||
"""
|
||||
tests/services/kb/test_path_upload_service.py
|
||||
|
||||
用途:path_upload_service 路径校验与 payload 状态机单元测试。
|
||||
"""
|
||||
import pytest
|
||||
|
||||
from app.services.kb import path_upload_service
|
||||
|
||||
|
||||
def test_sanitize_relative_path_normal():
|
||||
assert path_upload_service.sanitize_relative_path('folder/doc.pdf', 'doc.pdf') == 'folder/doc.pdf'
|
||||
|
||||
|
||||
def test_sanitize_relative_path_rejects_traversal():
|
||||
with pytest.raises(ValueError, match='非法路径'):
|
||||
path_upload_service.sanitize_relative_path('../etc/passwd', 'passwd')
|
||||
|
||||
|
||||
def test_build_path_upload_file_item_defaults():
|
||||
item = path_upload_service.build_path_upload_file_item('a/b.txt', 'b.txt')
|
||||
assert item['relative_path'] == 'a/b.txt'
|
||||
assert item['status'] == 'queued'
|
||||
assert item['document_id'] is None
|
||||
|
||||
|
||||
def test_sync_path_upload_job_payload_counts(app):
|
||||
from app.models import KbJob
|
||||
|
||||
with app.app_context():
|
||||
job = KbJob(job_type='path_upload', status='draft', progress=0)
|
||||
files = [
|
||||
{'relative_path': 'a/1.txt', 'status': 'success', 'staged_path': '/tmp/x'},
|
||||
{'relative_path': 'a/2.txt', 'status': 'failed', 'staged_path': ''},
|
||||
]
|
||||
payload, result = path_upload_service.sync_path_upload_job_payload(job, files)
|
||||
assert payload['total_files'] == 2
|
||||
assert result['success'] == 1
|
||||
assert result['failed'] == 1
|
||||
assert payload['selected_paths'] == ['a']
|
||||
Reference in new issue
Block a user