Blame view

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