51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
"""
|
|
backend/tests/test_faq_kb_invalidation.py
|
|
|
|
用途:FAQ 与 KB 文档关联解析及失效标记单元测试。
|
|
"""
|
|
from app.services.faq_kb_invalidation import (
|
|
_faq_links_kb_document,
|
|
extract_dify_id_pairs,
|
|
merge_source_kb_document_ids,
|
|
)
|
|
from app.models import QuickQuestion
|
|
|
|
|
|
def test_extract_dify_id_pairs_from_retriever_resources():
|
|
resources = [
|
|
{'dataset_id': 'ds-1', 'document_id': 'doc-1'},
|
|
{'metadata': {'dify_dataset_id': 'ds-2', 'remote_document_id': 'doc-2'}},
|
|
]
|
|
pairs = extract_dify_id_pairs(resources)
|
|
assert ('ds-1', 'doc-1') in pairs
|
|
assert ('ds-2', 'doc-2') in pairs
|
|
|
|
|
|
def test_faq_links_kb_document_by_source_ids():
|
|
qq = QuickQuestion(
|
|
user_id=None,
|
|
agent_id=1,
|
|
title='t',
|
|
question='q',
|
|
source_kb_document_ids=[10, 20],
|
|
)
|
|
assert _faq_links_kb_document(qq, 10, None, None) is True
|
|
assert _faq_links_kb_document(qq, 99, None, None) is False
|
|
|
|
|
|
def test_faq_links_kb_document_by_retriever_resources():
|
|
qq = QuickQuestion(
|
|
user_id=None,
|
|
agent_id=1,
|
|
title='t',
|
|
question='q',
|
|
retriever_resources=[{'dataset_id': 'ds-a', 'document_id': 'remote-a'}],
|
|
)
|
|
assert _faq_links_kb_document(qq, 0, 'ds-a', 'remote-a') is True
|
|
assert _faq_links_kb_document(qq, 0, 'ds-b', 'remote-a') is False
|
|
|
|
|
|
def test_merge_source_kb_document_ids():
|
|
merged = merge_source_kb_document_ids([1], None)
|
|
assert merged == [1]
|