16c42787
tangwang
feat: implement r...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
"""
pytest配置文件
提供测试夹具和共享配置
"""
import os
import sys
import pytest
import tempfile
from typing import Dict, Any, Generator
from unittest.mock import Mock, MagicMock
# 添加项目根目录到Python路径
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)
|
33839b37
tangwang
属性值参与搜索:
|
18
|
from config import SearchConfig, QueryConfig, IndexConfig, SPUConfig, RankingConfig, FunctionScoreConfig, RerankConfig
|
16c42787
tangwang
feat: implement r...
|
19
20
21
22
23
24
25
|
from utils.es_client import ESClient
from search import Searcher
from query import QueryParser
from context import RequestContext, create_request_context
@pytest.fixture
|
16c42787
tangwang
feat: implement r...
|
26
27
28
29
|
def sample_index_config() -> IndexConfig:
"""样例索引配置"""
return IndexConfig(
name="default",
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
30
|
label="默认索引",
|
33839b37
tangwang
属性值参与搜索:
|
31
32
|
fields=["title_zh", "brief_zh", "tags"],
boost=1.0
|
16c42787
tangwang
feat: implement r...
|
33
34
35
36
|
)
@pytest.fixture
|
9cb7528e
tangwang
店匠体系数据的搜索:mock da...
|
37
38
|
def sample_search_config(sample_index_config) -> SearchConfig:
"""样例搜索配置"""
|
16c42787
tangwang
feat: implement r...
|
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
query_config = QueryConfig(
enable_query_rewrite=True,
enable_translation=True,
enable_text_embedding=True,
supported_languages=["zh", "en"]
)
spu_config = SPUConfig(
enabled=True,
spu_field="spu_id",
inner_hits_size=3
)
ranking_config = RankingConfig(
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
53
54
|
expression="static_bm25() + text_embedding_relevance() * 0.2",
description="Test ranking"
|
16c42787
tangwang
feat: implement r...
|
55
56
|
)
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
57
58
59
|
function_score_config = FunctionScoreConfig()
rerank_config = RerankConfig()
|
9cb7528e
tangwang
店匠体系数据的搜索:mock da...
|
60
|
return SearchConfig(
|
16c42787
tangwang
feat: implement r...
|
61
|
es_index_name="test_products",
|
33839b37
tangwang
属性值参与搜索:
|
62
63
64
65
66
67
68
|
field_boosts={
"tenant_id": 1.0,
"title_zh": 3.0,
"brief_zh": 1.5,
"tags": 1.0,
"category_path_zh": 1.5,
},
|
16c42787
tangwang
feat: implement r...
|
69
|
indexes=[sample_index_config],
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
70
|
query_config=query_config,
|
16c42787
tangwang
feat: implement r...
|
71
|
ranking=ranking_config,
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
72
73
74
|
function_score=function_score_config,
rerank=rerank_config,
spu_config=spu_config
|
16c42787
tangwang
feat: implement r...
|
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
)
@pytest.fixture
def mock_es_client() -> Mock:
"""模拟ES客户端"""
mock_client = Mock(spec=ESClient)
# 模拟搜索响应
mock_response = {
"hits": {
"total": {"value": 10},
"max_score": 2.5,
"hits": [
{
"_id": "1",
"_score": 2.5,
"_source": {
|
33839b37
tangwang
属性值参与搜索:
|
93
94
95
96
|
"title_zh": "红色连衣裙",
"vendor_zh": "测试品牌",
"min_price": 299.0,
"category_id": "1"
|
16c42787
tangwang
feat: implement r...
|
97
98
99
100
101
102
|
}
},
{
"_id": "2",
"_score": 2.2,
"_source": {
|
33839b37
tangwang
属性值参与搜索:
|
103
104
105
106
|
"title_zh": "蓝色连衣裙",
"vendor_zh": "测试品牌",
"min_price": 399.0,
"category_id": "1"
|
16c42787
tangwang
feat: implement r...
|
107
108
109
110
111
112
113
114
115
116
117
118
|
}
}
]
},
"took": 15
}
mock_client.search.return_value = mock_response
return mock_client
@pytest.fixture
|
9cb7528e
tangwang
店匠体系数据的搜索:mock da...
|
119
|
def test_searcher(sample_search_config, mock_es_client) -> Searcher:
|
16c42787
tangwang
feat: implement r...
|
120
121
|
"""测试用Searcher实例"""
return Searcher(
|
9f96d6f3
tangwang
短query不用语义搜索
|
122
123
|
es_client=mock_es_client,
config=sample_search_config
|
16c42787
tangwang
feat: implement r...
|
124
125
126
127
|
)
@pytest.fixture
|
9cb7528e
tangwang
店匠体系数据的搜索:mock da...
|
128
|
def test_query_parser(sample_search_config) -> QueryParser:
|
16c42787
tangwang
feat: implement r...
|
129
|
"""测试用QueryParser实例"""
|
9cb7528e
tangwang
店匠体系数据的搜索:mock da...
|
130
|
return QueryParser(sample_search_config)
|
16c42787
tangwang
feat: implement r...
|
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
@pytest.fixture
def test_request_context() -> RequestContext:
"""测试用RequestContext实例"""
return create_request_context("test-req-001", "test-user")
@pytest.fixture
def sample_search_results() -> Dict[str, Any]:
"""样例搜索结果"""
return {
"query": "红色连衣裙",
"expected_total": 2,
"expected_products": [
|
33839b37
tangwang
属性值参与搜索:
|
146
147
|
{"title_zh": "红色连衣裙", "min_price": 299.0},
{"title_zh": "蓝色连衣裙", "min_price": 399.0}
|
16c42787
tangwang
feat: implement r...
|
148
149
150
151
152
153
154
155
156
157
158
|
]
}
@pytest.fixture
def temp_config_file() -> Generator[str, None, None]:
"""临时配置文件"""
import tempfile
import yaml
config_data = {
|
16c42787
tangwang
feat: implement r...
|
159
|
"es_index_name": "test_products",
|
33839b37
tangwang
属性值参与搜索:
|
160
161
162
163
164
|
"field_boosts": {
"title_zh": 3.0,
"brief_zh": 1.5,
"tags": 1.0,
"category_path_zh": 1.5
|
16c42787
tangwang
feat: implement r...
|
165
|
},
|
16c42787
tangwang
feat: implement r...
|
166
167
168
|
"indexes": [
{
"name": "default",
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
169
|
"label": "默认索引",
|
33839b37
tangwang
属性值参与搜索:
|
170
171
|
"fields": ["title_zh", "brief_zh", "tags"],
"boost": 1.0
|
16c42787
tangwang
feat: implement r...
|
172
173
|
}
],
|
33839b37
tangwang
属性值参与搜索:
|
174
175
176
177
178
179
180
|
"query_config": {
"supported_languages": ["zh", "en"],
"default_language": "zh",
"enable_translation": True,
"enable_text_embedding": True,
"enable_query_rewrite": True
},
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
181
|
"spu_config": {
|
16c42787
tangwang
feat: implement r...
|
182
183
184
185
186
|
"enabled": True,
"spu_field": "spu_id",
"inner_hits_size": 3
},
"ranking": {
|
33839b37
tangwang
属性值参与搜索:
|
187
|
"expression": "bm25() + 0.2*text_embedding_relevance()",
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
188
189
190
191
192
193
194
195
196
197
198
|
"description": "Test ranking"
},
"function_score": {
"score_mode": "sum",
"boost_mode": "multiply",
"functions": []
},
"rerank": {
"enabled": False,
"expression": "",
"description": ""
|
16c42787
tangwang
feat: implement r...
|
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
}
}
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
yaml.dump(config_data, f)
temp_file = f.name
yield temp_file
# 清理
os.unlink(temp_file)
@pytest.fixture
def mock_env_variables(monkeypatch):
"""设置环境变量"""
monkeypatch.setenv("ES_HOST", "http://localhost:9200")
monkeypatch.setenv("ES_USERNAME", "elastic")
monkeypatch.setenv("ES_PASSWORD", "changeme")
|
16c42787
tangwang
feat: implement r...
|
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# 标记配置
pytest_plugins = []
# 标记定义
def pytest_configure(config):
"""配置pytest标记"""
config.addinivalue_line(
"markers", "unit: 单元测试"
)
config.addinivalue_line(
"markers", "integration: 集成测试"
)
config.addinivalue_line(
"markers", "api: API测试"
)
config.addinivalue_line(
"markers", "e2e: 端到端测试"
)
config.addinivalue_line(
"markers", "performance: 性能测试"
)
config.addinivalue_line(
"markers", "slow: 慢速测试"
)
# 测试数据
@pytest.fixture
def test_queries():
"""测试查询集合"""
return [
"红色连衣裙",
"wireless bluetooth headphones",
"手机 手机壳",
"laptop AND (gaming OR professional)",
"运动鞋 -价格:0-500"
]
@pytest.fixture
def expected_response_structure():
"""期望的API响应结构"""
return {
"hits": list,
"total": int,
"max_score": float,
"took_ms": int,
"aggregations": dict,
"query_info": dict,
"performance_summary": dict
|
33839b37
tangwang
属性值参与搜索:
|
270
|
}
|