mirror of
https://github.com/langgenius/dify.git
synced 2026-03-25 13:01:06 +08:00
Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: kurokobo <kuro664@gmail.com> Co-authored-by: Hiroshi Fujita <fujita-h@users.noreply.github.com> Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: Gen Sato <52241300+halogen22@users.noreply.github.com> Co-authored-by: eux <euxuuu@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: lotsik <lotsik@mail.ru> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: gakkiyomi <gakkiyomi@aliyun.com> Co-authored-by: CN-P5 <heibai2006@gmail.com> Co-authored-by: CN-P5 <heibai2006@qq.com> Co-authored-by: Chuehnone <1897025+chuehnone@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Kevin9703 <51311316+Kevin9703@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Boris Feld <lothiraldan@gmail.com> Co-authored-by: mbo <himabo@gmail.com> Co-authored-by: mabo <mabo@aeyes.ai> Co-authored-by: Warren Chen <warren.chen830@gmail.com> Co-authored-by: KVOJJJin <jzongcode@gmail.com> Co-authored-by: JzoNgKVO <27049666+JzoNgKVO@users.noreply.github.com> Co-authored-by: jiandanfeng <chenjh3@wangsu.com> Co-authored-by: zhu-an <70234959+xhdd123321@users.noreply.github.com> Co-authored-by: zhaoqingyu.1075 <zhaoqingyu.1075@bytedance.com> Co-authored-by: 海狸大師 <86974027+yenslife@users.noreply.github.com> Co-authored-by: Xu Song <xusong.vip@gmail.com> Co-authored-by: rayshaw001 <396301947@163.com> Co-authored-by: Ding Jiatong <dingjiatong@gmail.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: JasonVV <jasonwangiii@outlook.com> Co-authored-by: le0zh <newlight@qq.com> Co-authored-by: zhuxinliang <zhuxinliang@didiglobal.com> Co-authored-by: k-zaku <zaku99@outlook.jp> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: luckylhb90 <luckylhb90@gmail.com> Co-authored-by: hobo.l <hobo.l@binance.com> Co-authored-by: jiangbo721 <365065261@qq.com> Co-authored-by: 刘江波 <jiangbo721@163.com> Co-authored-by: Shun Miyazawa <34241526+miya@users.noreply.github.com> Co-authored-by: EricPan <30651140+Egfly@users.noreply.github.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: sino <sino2322@gmail.com> Co-authored-by: Jhvcc <37662342+Jhvcc@users.noreply.github.com> Co-authored-by: lowell <lowell.hu@zkteco.in>
159 lines
5.8 KiB
Python
159 lines
5.8 KiB
Python
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from core.rag.datasource.vdb.field import Field
|
|
from core.rag.datasource.vdb.opensearch.opensearch_vector import OpenSearchConfig, OpenSearchVector
|
|
from core.rag.models.document import Document
|
|
from extensions import ext_redis
|
|
|
|
|
|
def get_example_text() -> str:
|
|
return "This is a sample text for testing purposes."
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def setup_mock_redis():
|
|
ext_redis.redis_client.get = MagicMock(return_value=None)
|
|
ext_redis.redis_client.set = MagicMock(return_value=None)
|
|
|
|
mock_redis_lock = MagicMock()
|
|
mock_redis_lock.__enter__ = MagicMock()
|
|
mock_redis_lock.__exit__ = MagicMock()
|
|
ext_redis.redis_client.lock = MagicMock(return_value=mock_redis_lock)
|
|
|
|
|
|
class TestOpenSearchVector:
|
|
def setup_method(self):
|
|
self.collection_name = "test_collection"
|
|
self.example_doc_id = "example_doc_id"
|
|
self.vector = OpenSearchVector(
|
|
collection_name=self.collection_name,
|
|
config=OpenSearchConfig(host="localhost", port=9200, user="admin", password="password", secure=False),
|
|
)
|
|
self.vector._client = MagicMock()
|
|
|
|
@pytest.mark.parametrize(
|
|
("search_response", "expected_length", "expected_doc_id"),
|
|
[
|
|
(
|
|
{
|
|
"hits": {
|
|
"total": {"value": 1},
|
|
"hits": [
|
|
{
|
|
"_source": {
|
|
"page_content": get_example_text(),
|
|
"metadata": {"document_id": "example_doc_id"},
|
|
}
|
|
}
|
|
],
|
|
}
|
|
},
|
|
1,
|
|
"example_doc_id",
|
|
),
|
|
({"hits": {"total": {"value": 0}, "hits": []}}, 0, None),
|
|
],
|
|
)
|
|
def test_search_by_full_text(self, search_response, expected_length, expected_doc_id):
|
|
self.vector._client.search.return_value = search_response
|
|
|
|
hits_by_full_text = self.vector.search_by_full_text(query=get_example_text())
|
|
assert len(hits_by_full_text) == expected_length
|
|
if expected_length > 0:
|
|
assert hits_by_full_text[0].metadata["document_id"] == expected_doc_id
|
|
|
|
def test_search_by_vector(self):
|
|
vector = [0.1] * 128
|
|
mock_response = {
|
|
"hits": {
|
|
"total": {"value": 1},
|
|
"hits": [
|
|
{
|
|
"_source": {
|
|
Field.CONTENT_KEY.value: get_example_text(),
|
|
Field.METADATA_KEY.value: {"document_id": self.example_doc_id},
|
|
},
|
|
"_score": 1.0,
|
|
}
|
|
],
|
|
}
|
|
}
|
|
self.vector._client.search.return_value = mock_response
|
|
|
|
hits_by_vector = self.vector.search_by_vector(query_vector=vector)
|
|
|
|
print("Hits by vector:", hits_by_vector)
|
|
print("Expected document ID:", self.example_doc_id)
|
|
print("Actual document ID:", hits_by_vector[0].metadata["document_id"] if hits_by_vector else "No hits")
|
|
|
|
assert len(hits_by_vector) > 0, f"Expected at least one hit, got {len(hits_by_vector)}"
|
|
assert hits_by_vector[0].metadata["document_id"] == self.example_doc_id, (
|
|
f"Expected document ID {self.example_doc_id}, got {hits_by_vector[0].metadata['document_id']}"
|
|
)
|
|
|
|
def test_get_ids_by_metadata_field(self):
|
|
mock_response = {"hits": {"total": {"value": 1}, "hits": [{"_id": "mock_id"}]}}
|
|
self.vector._client.search.return_value = mock_response
|
|
|
|
doc = Document(page_content="Test content", metadata={"document_id": self.example_doc_id})
|
|
embedding = [0.1] * 128
|
|
|
|
with patch("opensearchpy.helpers.bulk") as mock_bulk:
|
|
mock_bulk.return_value = ([], [])
|
|
self.vector.add_texts([doc], [embedding])
|
|
|
|
ids = self.vector.get_ids_by_metadata_field(key="document_id", value=self.example_doc_id)
|
|
assert len(ids) == 1
|
|
assert ids[0] == "mock_id"
|
|
|
|
def test_add_texts(self):
|
|
self.vector._client.index.return_value = {"result": "created"}
|
|
|
|
doc = Document(page_content="Test content", metadata={"document_id": self.example_doc_id})
|
|
embedding = [0.1] * 128
|
|
|
|
with patch("opensearchpy.helpers.bulk") as mock_bulk:
|
|
mock_bulk.return_value = ([], [])
|
|
self.vector.add_texts([doc], [embedding])
|
|
|
|
mock_response = {"hits": {"total": {"value": 1}, "hits": [{"_id": "mock_id"}]}}
|
|
self.vector._client.search.return_value = mock_response
|
|
|
|
ids = self.vector.get_ids_by_metadata_field(key="document_id", value=self.example_doc_id)
|
|
assert len(ids) == 1
|
|
assert ids[0] == "mock_id"
|
|
|
|
|
|
@pytest.mark.usefixtures("setup_mock_redis")
|
|
class TestOpenSearchVectorWithRedis:
|
|
def setup_method(self):
|
|
self.tester = TestOpenSearchVector()
|
|
|
|
def test_search_by_full_text(self):
|
|
self.tester.setup_method()
|
|
search_response = {
|
|
"hits": {
|
|
"total": {"value": 1},
|
|
"hits": [
|
|
{"_source": {"page_content": get_example_text(), "metadata": {"document_id": "example_doc_id"}}}
|
|
],
|
|
}
|
|
}
|
|
expected_length = 1
|
|
expected_doc_id = "example_doc_id"
|
|
self.tester.test_search_by_full_text(search_response, expected_length, expected_doc_id)
|
|
|
|
def test_get_ids_by_metadata_field(self):
|
|
self.tester.setup_method()
|
|
self.tester.test_get_ids_by_metadata_field()
|
|
|
|
def test_add_texts(self):
|
|
self.tester.setup_method()
|
|
self.tester.test_add_texts()
|
|
|
|
def test_search_by_vector(self):
|
|
self.tester.setup_method()
|
|
self.tester.test_search_by_vector()
|