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)
|
77ab67ad
tangwang
更新测试用例
|
18
|
from config import SearchConfig, QueryConfig, IndexConfig, SPUConfig, 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="默认索引",
|
d7d48f52
tangwang
改动(mapping + 灌入结构)
|
31
|
fields=["title.zh", "brief.zh", "tags"],
|
33839b37
tangwang
属性值参与搜索:
|
32
|
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
|
query_config = QueryConfig(
enable_query_rewrite=True,
|
16c42787
tangwang
feat: implement r...
|
41
42
43
44
45
46
47
48
49
50
|
enable_text_embedding=True,
supported_languages=["zh", "en"]
)
spu_config = SPUConfig(
enabled=True,
spu_field="spu_id",
inner_hits_size=3
)
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
51
52
53
|
function_score_config = FunctionScoreConfig()
rerank_config = RerankConfig()
|
9cb7528e
tangwang
店匠体系数据的搜索:mock da...
|
54
|
return SearchConfig(
|
16c42787
tangwang
feat: implement r...
|
55
|
es_index_name="test_products",
|
33839b37
tangwang
属性值参与搜索:
|
56
57
|
field_boosts={
"tenant_id": 1.0,
|
d7d48f52
tangwang
改动(mapping + 灌入结构)
|
58
59
|
"title.zh": 3.0,
"brief.zh": 1.5,
|
33839b37
tangwang
属性值参与搜索:
|
60
|
"tags": 1.0,
|
d7d48f52
tangwang
改动(mapping + 灌入结构)
|
61
|
"category_path.zh": 1.5,
|
33839b37
tangwang
属性值参与搜索:
|
62
|
},
|
16c42787
tangwang
feat: implement r...
|
63
|
indexes=[sample_index_config],
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
64
|
query_config=query_config,
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
65
66
67
|
function_score=function_score_config,
rerank=rerank_config,
spu_config=spu_config
|
16c42787
tangwang
feat: implement r...
|
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
)
@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": {
|
d7d48f52
tangwang
改动(mapping + 灌入结构)
|
86
87
|
"title": {"zh": "红色连衣裙"},
"vendor": {"zh": "测试品牌"},
|
33839b37
tangwang
属性值参与搜索:
|
88
89
|
"min_price": 299.0,
"category_id": "1"
|
16c42787
tangwang
feat: implement r...
|
90
91
92
93
94
95
|
}
},
{
"_id": "2",
"_score": 2.2,
"_source": {
|
d7d48f52
tangwang
改动(mapping + 灌入结构)
|
96
97
|
"title": {"zh": "蓝色连衣裙"},
"vendor": {"zh": "测试品牌"},
|
33839b37
tangwang
属性值参与搜索:
|
98
99
|
"min_price": 399.0,
"category_id": "1"
|
16c42787
tangwang
feat: implement r...
|
100
101
102
103
104
105
106
107
108
109
110
111
|
}
}
]
},
"took": 15
}
mock_client.search.return_value = mock_response
return mock_client
@pytest.fixture
|
9cb7528e
tangwang
店匠体系数据的搜索:mock da...
|
112
|
def test_searcher(sample_search_config, mock_es_client) -> Searcher:
|
16c42787
tangwang
feat: implement r...
|
113
114
|
"""测试用Searcher实例"""
return Searcher(
|
9f96d6f3
tangwang
短query不用语义搜索
|
115
116
|
es_client=mock_es_client,
config=sample_search_config
|
16c42787
tangwang
feat: implement r...
|
117
118
119
120
|
)
@pytest.fixture
|
9cb7528e
tangwang
店匠体系数据的搜索:mock da...
|
121
|
def test_query_parser(sample_search_config) -> QueryParser:
|
16c42787
tangwang
feat: implement r...
|
122
|
"""测试用QueryParser实例"""
|
9cb7528e
tangwang
店匠体系数据的搜索:mock da...
|
123
|
return QueryParser(sample_search_config)
|
16c42787
tangwang
feat: implement r...
|
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
@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": [
|
d7d48f52
tangwang
改动(mapping + 灌入结构)
|
139
140
|
{"title": "红色连衣裙", "min_price": 299.0},
{"title": "蓝色连衣裙", "min_price": 399.0}
|
16c42787
tangwang
feat: implement r...
|
141
142
143
144
145
146
147
148
149
150
151
|
]
}
@pytest.fixture
def temp_config_file() -> Generator[str, None, None]:
"""临时配置文件"""
import tempfile
import yaml
config_data = {
|
16c42787
tangwang
feat: implement r...
|
152
|
"es_index_name": "test_products",
|
33839b37
tangwang
属性值参与搜索:
|
153
|
"field_boosts": {
|
d7d48f52
tangwang
改动(mapping + 灌入结构)
|
154
155
|
"title.zh": 3.0,
"brief.zh": 1.5,
|
33839b37
tangwang
属性值参与搜索:
|
156
|
"tags": 1.0,
|
d7d48f52
tangwang
改动(mapping + 灌入结构)
|
157
|
"category_path.zh": 1.5
|
16c42787
tangwang
feat: implement r...
|
158
|
},
|
16c42787
tangwang
feat: implement r...
|
159
160
161
|
"indexes": [
{
"name": "default",
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
162
|
"label": "默认索引",
|
d7d48f52
tangwang
改动(mapping + 灌入结构)
|
163
|
"fields": ["title.zh", "brief.zh", "tags"],
|
33839b37
tangwang
属性值参与搜索:
|
164
|
"boost": 1.0
|
16c42787
tangwang
feat: implement r...
|
165
166
|
}
],
|
33839b37
tangwang
属性值参与搜索:
|
167
168
169
|
"query_config": {
"supported_languages": ["zh", "en"],
"default_language": "zh",
|
33839b37
tangwang
属性值参与搜索:
|
170
171
172
|
"enable_text_embedding": True,
"enable_query_rewrite": True
},
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
173
|
"spu_config": {
|
16c42787
tangwang
feat: implement r...
|
174
175
176
177
178
|
"enabled": True,
"spu_field": "spu_id",
"inner_hits_size": 3
},
"ranking": {
|
33839b37
tangwang
属性值参与搜索:
|
179
|
"expression": "bm25() + 0.2*text_embedding_relevance()",
|
4d824a77
tangwang
所有租户共用一套统一配置.tena...
|
180
181
182
183
184
185
186
187
|
"description": "Test ranking"
},
"function_score": {
"score_mode": "sum",
"boost_mode": "multiply",
"functions": []
},
"rerank": {
|
a99e62ba
tangwang
记录各阶段耗时
|
188
|
"rerank_window": 386
|
16c42787
tangwang
feat: implement r...
|
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
}
}
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...
|
208
209
210
211
212
213
214
215
216
217
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
|
# 标记配置
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
属性值参与搜索:
|
260
|
}
|