Blame view

tests/ci/test_service_api_contracts.py 29.7 KB
7299bae6   tangwang   tests
1
2
  from __future__ import annotations
  
e7a2c0b7   tangwang   img encode
3
4
  import json
  from pathlib import Path
7299bae6   tangwang   tests
5
6
7
8
  from types import SimpleNamespace
  from typing import Any, Dict, List
  
  import numpy as np
2e3670ab   tangwang   index services
9
  import pandas as pd
7299bae6   tangwang   tests
10
11
  import pytest
  from fastapi.testclient import TestClient
0fd2f875   tangwang   translate
12
  from translation.scenes import normalize_scene_name
7299bae6   tangwang   tests
13
  
99b72698   tangwang   测试回归钩子梳理
14
15
  pytestmark = [pytest.mark.contract, pytest.mark.regression]
  
7299bae6   tangwang   tests
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  
  class _FakeSearcher:
      def search(self, **kwargs):
          return SimpleNamespace(
              results=[
                  {
                      "spu_id": "spu-1",
                      "title": "测试商品",
                      "price": 99.0,
                      "currency": "USD",
                      "in_stock": True,
                      "skus": [],
                      "relevance_score": 1.2,
                  }
              ],
              total=1,
              max_score=1.2,
              took_ms=8,
              facets=[],
              query_info={"normalized_query": kwargs.get("query", "")},
              suggestions=[],
              related_searches=[],
              debug_info=None,
          )
  
      def search_by_image(self, **kwargs):
          return self.search(**kwargs)
  
  
  class _FakeSuggestionService:
      def search(self, **kwargs):
          return {
              "query": kwargs["query"],
              "language": kwargs.get("language", "en"),
              "resolved_language": kwargs.get("language", "en"),
              "suggestions": [{"text": "iphone 15", "score": 1.0}],
              "took_ms": 3,
          }
  
  
  @pytest.fixture
  def search_client(monkeypatch):
      import api.app as search_app
  
      monkeypatch.setattr(search_app, "init_service", lambda es_host="": None)
      monkeypatch.setattr(search_app, "get_searcher", lambda: _FakeSearcher())
      monkeypatch.setattr(search_app, "get_suggestion_service", lambda: _FakeSuggestionService())
  
      with TestClient(search_app.app) as client:
          yield client
  
  
  def test_search_api_contract(search_client: TestClient):
      response = search_client.post(
          "/search/",
          headers={"X-Tenant-ID": "162"},
          json={"query": "toy", "size": 5},
      )
      assert response.status_code == 200
      data = response.json()
      assert data["total"] == 1
      assert data["results"][0]["spu_id"] == "spu-1"
  
  
  def test_image_search_api_contract(search_client: TestClient):
      response = search_client.post(
          "/search/image",
          headers={"X-Tenant-ID": "162"},
          json={"image_url": "https://example.com/a.jpg", "size": 3},
      )
      assert response.status_code == 200
      assert response.json()["results"][0]["spu_id"] == "spu-1"
  
  
  def test_suggestion_api_contract(search_client: TestClient):
      response = search_client.get(
          "/search/suggestions?q=iph&size=5&language=en",
          headers={"X-Tenant-ID": "162"},
      )
      assert response.status_code == 200
      data = response.json()
      assert data["query"] == "iph"
      assert len(data["suggestions"]) == 1
  
  
26b910bd   tangwang   refactor service ...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  def test_instant_search_not_implemented(search_client: TestClient):
      response = search_client.get("/search/instant?q=iph&size=5")
      assert response.status_code == 501
  
  
  def test_admin_stats_contract(search_client: TestClient, monkeypatch):
      import api.app as search_app
  
      class _FakeIndices:
          @staticmethod
          def exists(index: str) -> bool:
              return index.endswith("search_products_tenant_162")
  
          @staticmethod
          def stats(index: str):
              return {
                  "indices": {
                      index: {
                          "total": {
                              "store": {
                                  "size_in_bytes": 2 * 1024 * 1024,
                              }
                          }
                      }
                  }
              }
  
      class _FakeClient:
          indices = _FakeIndices()
  
          @staticmethod
          def count(index: str):
              assert index.endswith("search_products_tenant_162")
              return {"count": 123}
  
      monkeypatch.setattr(search_app, "get_es_client", lambda: SimpleNamespace(client=_FakeClient()))
  
      response = search_client.get("/admin/stats", headers={"X-Tenant-ID": "162"})
      assert response.status_code == 200
      data = response.json()
      assert data["tenant_id"] == "162"
      assert data["index_name"].endswith("search_products_tenant_162")
      assert data["document_count"] == 123
      assert data["size_mb"] == 2.0
  
  
7299bae6   tangwang   tests
147
148
149
150
151
152
153
154
155
156
157
  class _FakeBulkService:
      def bulk_index(self, tenant_id: str, recreate_index: bool, batch_size: int):
          return {
              "tenant_id": tenant_id,
              "recreate_index": recreate_index,
              "batch_size": batch_size,
              "success": True,
          }
  
  
  class _FakeTransformer:
77ab67ad   tangwang   更新测试用例
158
      def transform_spu_to_doc(self, tenant_id: str, spu_row, skus, options, **kwargs):
7299bae6   tangwang   tests
159
160
161
162
163
164
165
          return {
              "tenant_id": tenant_id,
              "spu_id": str(spu_row.get("id", "0")),
              "title": {"zh": str(spu_row.get("title", ""))},
          }
  
  
2e3670ab   tangwang   index services
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  class _FakeDbConnection:
      """Minimal fake for indexer health check: connect().execute(text('SELECT 1'))."""
  
      def __enter__(self):
          return self
  
      def __exit__(self, *args):
          pass
  
      def execute(self, stmt):
          pass
  
  
  class _FakeDbEngine:
      def connect(self):
          return _FakeDbConnection()
  
  
7299bae6   tangwang   tests
184
  class _FakeIncrementalService:
2e3670ab   tangwang   index services
185
186
187
188
      def __init__(self):
          self.db_engine = _FakeDbEngine()
          self.category_id_to_name = {}
  
7299bae6   tangwang   tests
189
      def index_spus_to_es(self, es_client, tenant_id: str, spu_ids: List[str], delete_spu_ids=None):
2e3670ab   tangwang   index services
190
          out = {
7299bae6   tangwang   tests
191
192
193
194
195
196
197
              "tenant_id": tenant_id,
              "spu_ids": [{"spu_id": s, "status": "indexed"} for s in spu_ids],
              "delete_spu_ids": [],
              "total": len(spu_ids),
              "success_count": len(spu_ids),
              "failed_count": 0,
          }
2e3670ab   tangwang   index services
198
199
200
201
202
203
204
205
206
207
208
209
          if delete_spu_ids:
              out["delete_spu_ids"] = [{"spu_id": s, "status": "deleted"} for s in delete_spu_ids]
              out["total"] += len(delete_spu_ids)
              out["success_count"] += len(delete_spu_ids)
          return out
  
      def get_spu_document(self, tenant_id: str, spu_id: str):
          return {
              "tenant_id": tenant_id,
              "spu_id": spu_id,
              "title": {"zh": "Fake doc"},
          }
7299bae6   tangwang   tests
210
211
212
213
  
      def _get_transformer_bundle(self, tenant_id: str):
          return _FakeTransformer(), None, False
  
2e3670ab   tangwang   index services
214
215
216
217
218
219
220
221
222
223
224
      def _load_spus_for_spu_ids(self, tenant_id: str, spu_ids: List[str], include_deleted: bool = False):
          if not spu_ids:
              return pd.DataFrame()
          return pd.DataFrame([{"id": int(s), "title": "Fake", "tenant_id": tenant_id} for s in spu_ids])
  
      def _load_skus_for_spu_ids(self, tenant_id: str, spu_ids: List[str]):
          return pd.DataFrame()
  
      def _load_options_for_spu_ids(self, tenant_id: str, spu_ids: List[str]):
          return pd.DataFrame()
  
7299bae6   tangwang   tests
225
226
227
228
229
230
  
  @pytest.fixture
  def indexer_client(monkeypatch):
      import api.indexer_app as indexer_app
      import api.routes.indexer as indexer_routes
  
ed948666   tangwang   tidy
231
      indexer_app.app.router.on_startup.clear()
7299bae6   tangwang   tests
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
270
271
272
273
      monkeypatch.setattr(indexer_app, "init_indexer_service", lambda es_host="": None)
      monkeypatch.setattr(indexer_routes, "get_bulk_indexing_service", lambda: _FakeBulkService())
      monkeypatch.setattr(indexer_routes, "get_incremental_service", lambda: _FakeIncrementalService())
      monkeypatch.setattr(indexer_routes, "get_es_client", lambda: object())
  
      with TestClient(indexer_app.app) as client:
          yield client
  
  
  def test_indexer_reindex_contract(indexer_client: TestClient):
      response = indexer_client.post(
          "/indexer/reindex",
          json={"tenant_id": "162", "batch_size": 100},
      )
      assert response.status_code == 200
      assert response.json()["success"] is True
  
  
  def test_indexer_incremental_contract(indexer_client: TestClient):
      response = indexer_client.post(
          "/indexer/index",
          json={"tenant_id": "162", "spu_ids": ["1001", "1002"]},
      )
      assert response.status_code == 200
      data = response.json()
      assert data["success_count"] == 2
  
  
  def test_indexer_build_docs_contract(indexer_client: TestClient):
      response = indexer_client.post(
          "/indexer/build-docs",
          json={
              "tenant_id": "162",
              "items": [{"spu": {"id": 1, "title": "T-shirt"}, "skus": [], "options": []}],
          },
      )
      assert response.status_code == 200
      data = response.json()
      assert data["success_count"] == 1
      assert data["docs"][0]["spu_id"] == "1"
  
  
e7a2c0b7   tangwang   img encode
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
  def test_indexer_build_docs_show_request_response(indexer_client: TestClient):
      """
      调用「输入 SPU 详情、输出 ES doc」的 build-docs 接口,并打印完整请求与响应,
      便于核对返回的 ES 文档字段是否齐全。
      运行: pytest tests/ci/test_service_api_contracts.py::test_indexer_build_docs_show_request_response -v -s
      """
      request_body = {
          "tenant_id": "162",
          "items": [
              {
                  "spu": {"id": 1, "title": "T-shirt", "brief": "A simple T-shirt"},
                  "skus": [],
                  "options": [],
              }
          ],
      }
      print("\n" + "=" * 60)
      print("【请求】POST /indexer/build-docs")
      print("=" * 60)
      print(json.dumps(request_body, ensure_ascii=False, indent=2))
  
      response = indexer_client.post("/indexer/build-docs", json=request_body)
      data = response.json()
  
      print("\n" + "=" * 60)
      print("【响应】status_code =", response.status_code)
      print("=" * 60)
      print(json.dumps(data, ensure_ascii=False, indent=2, default=str))
  
      if data.get("docs"):
          doc = data["docs"][0]
          doc_keys = sorted(doc.keys())
          print("\n" + "=" * 60)
          print("【返回 doc 顶层字段】共 {} 个".format(len(doc_keys)))
          print("=" * 60)
          for k in doc_keys:
              print(" ", k)
  
          # 与 ES mapping 顶层字段对比
          mapping_path = Path(__file__).resolve().parents[2] / "mappings" / "search_products.json"
          if mapping_path.exists():
              with open(mapping_path) as f:
                  mapping = json.load(f)
              expected_top_level = set(mapping.get("mappings", {}).get("properties", {}).keys())
              returned = set(doc_keys)
              missing = expected_top_level - returned
              extra = returned - expected_top_level
              print("\n" + "=" * 60)
              print("【与 mappings/search_products.json 对比】")
              print("=" * 60)
              print("  mapping 中应有、当前 doc 未返回:", sorted(missing) if missing else "(无)")
              print("  当前 doc 多出:", sorted(extra) if extra else "(无)")
  
      assert response.status_code == 200
      assert data["success_count"] == 1
      assert data["docs"][0]["spu_id"] == "1"
  
  
2e3670ab   tangwang   index services
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
  def test_indexer_build_docs_from_db_contract(indexer_client: TestClient):
      """POST /indexer/build-docs-from-db: tenant_id + spu_ids, returns same shape as build-docs."""
      response = indexer_client.post(
          "/indexer/build-docs-from-db",
          json={"tenant_id": "162", "spu_ids": ["1001", "1002"]},
      )
      assert response.status_code == 200
      data = response.json()
      assert data["tenant_id"] == "162"
      assert "docs" in data
      assert data["success_count"] == 2
      assert len(data["docs"]) == 2
      assert data["docs"][0]["spu_id"] == "1001"
  
  
be3f0d46   tangwang   /indexer/enrich-c...
347
  def test_indexer_enrich_content_contract(indexer_client: TestClient, monkeypatch):
6f7840cf   tangwang   refactor: rename ...
348
      import indexer.product_enrich as process_products
be3f0d46   tangwang   /indexer/enrich-c...
349
  
5aaf0c7d   tangwang   feat(indexer): 完善...
350
351
352
      def _fake_build_index_content_fields(
          items: List[Dict[str, str]],
          tenant_id: str | None = None,
2703b6ea   tangwang   refactor(indexer)...
353
354
          enrichment_scopes: List[str] | None = None,
          category_taxonomy_profile: str = "apparel",
5aaf0c7d   tangwang   feat(indexer): 完善...
355
      ):
d350861f   tangwang   索引结构修改
356
          assert tenant_id == "162"
2703b6ea   tangwang   refactor(indexer)...
357
358
          assert enrichment_scopes == ["generic", "category_taxonomy"]
          assert category_taxonomy_profile == "apparel"
be3f0d46   tangwang   /indexer/enrich-c...
359
360
          return [
              {
d350861f   tangwang   索引结构修改
361
362
363
364
365
366
367
                  "id": p["spu_id"],
                  "qanchors": {
                      "zh": [f"zh-anchor-{p['spu_id']}"],
                      "en": [f"en-anchor-{p['spu_id']}"],
                  },
                  "enriched_tags": {"zh": ["tag1", "tag2"], "en": ["tag1", "tag2"]},
                  "enriched_attributes": [
80f1e036   tangwang   enriched_attribut...
368
                      {"name": "enriched_tags", "value": {"zh": ["tag1"], "en": ["tag1"]}},
d350861f   tangwang   索引结构修改
369
                  ],
5aaf0c7d   tangwang   feat(indexer): 完善...
370
371
372
                  "enriched_taxonomy_attributes": [
                      {"name": "Product Type", "value": {"zh": ["T恤"], "en": ["t-shirt"]}},
                  ],
be3f0d46   tangwang   /indexer/enrich-c...
373
              }
d350861f   tangwang   索引结构修改
374
              for p in items
be3f0d46   tangwang   /indexer/enrich-c...
375
376
          ]
  
d350861f   tangwang   索引结构修改
377
      monkeypatch.setattr(process_products, "build_index_content_fields", _fake_build_index_content_fields)
be3f0d46   tangwang   /indexer/enrich-c...
378
379
380
381
382
  
      response = indexer_client.post(
          "/indexer/enrich-content",
          json={
              "tenant_id": "162",
2703b6ea   tangwang   refactor(indexer)...
383
384
              "enrichment_scopes": ["generic", "category_taxonomy"],
              "category_taxonomy_profile": "apparel",
be3f0d46   tangwang   /indexer/enrich-c...
385
386
387
388
              "items": [
                  {"spu_id": "1001", "title": "T-shirt"},
                  {"spu_id": "1002", "title": "Toy"},
              ],
be3f0d46   tangwang   /indexer/enrich-c...
389
390
391
392
393
          },
      )
      assert response.status_code == 200
      data = response.json()
      assert data["tenant_id"] == "162"
2703b6ea   tangwang   refactor(indexer)...
394
395
      assert data["enrichment_scopes"] == ["generic", "category_taxonomy"]
      assert data["category_taxonomy_profile"] == "apparel"
be3f0d46   tangwang   /indexer/enrich-c...
396
397
398
      assert data["total"] == 2
      assert len(data["results"]) == 2
      assert data["results"][0]["spu_id"] == "1001"
d350861f   tangwang   索引结构修改
399
400
401
402
403
404
      assert data["results"][0]["qanchors"]["zh"] == ["zh-anchor-1001"]
      assert data["results"][0]["qanchors"]["en"] == ["en-anchor-1001"]
      assert data["results"][0]["enriched_tags"]["zh"] == ["tag1", "tag2"]
      assert data["results"][0]["enriched_tags"]["en"] == ["tag1", "tag2"]
      assert data["results"][0]["enriched_attributes"][0] == {
          "name": "enriched_tags",
80f1e036   tangwang   enriched_attribut...
405
          "value": {"zh": ["tag1"], "en": ["tag1"]},
d350861f   tangwang   索引结构修改
406
      }
5aaf0c7d   tangwang   feat(indexer): 完善...
407
408
409
410
      assert data["results"][0]["enriched_taxonomy_attributes"][0] == {
          "name": "Product Type",
          "value": {"zh": ["T恤"], "en": ["t-shirt"]},
      }
be3f0d46   tangwang   /indexer/enrich-c...
411
412
  
  
2703b6ea   tangwang   refactor(indexer)...
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
  def test_indexer_enrich_content_contract_accepts_deprecated_analysis_kinds(indexer_client: TestClient, monkeypatch):
      import indexer.product_enrich as process_products
  
      seen: Dict[str, Any] = {}
  
      def _fake_build_index_content_fields(
          items: List[Dict[str, str]],
          tenant_id: str | None = None,
          enrichment_scopes: List[str] | None = None,
          category_taxonomy_profile: str = "apparel",
      ):
          seen["tenant_id"] = tenant_id
          seen["enrichment_scopes"] = enrichment_scopes
          seen["category_taxonomy_profile"] = category_taxonomy_profile
          return [
              {
                  "id": items[0]["spu_id"],
                  "qanchors": {},
                  "enriched_tags": {},
                  "enriched_attributes": [],
                  "enriched_taxonomy_attributes": [],
              }
          ]
  
      monkeypatch.setattr(process_products, "build_index_content_fields", _fake_build_index_content_fields)
  
      response = indexer_client.post(
          "/indexer/enrich-content",
          json={
              "tenant_id": "162",
              "analysis_kinds": ["taxonomy"],
              "items": [{"spu_id": "1001", "title": "T-shirt"}],
          },
      )
  
      assert response.status_code == 200
      data = response.json()
      assert seen == {
          "tenant_id": "162",
          "enrichment_scopes": ["category_taxonomy"],
          "category_taxonomy_profile": "apparel",
      }
      assert data["enrichment_scopes"] == ["category_taxonomy"]
      assert data["category_taxonomy_profile"] == "apparel"
  
  
dabd52a5   tangwang   feat(indexer): 支持...
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
  def test_indexer_enrich_content_contract_supports_non_apparel_taxonomy_profiles(indexer_client: TestClient, monkeypatch):
      import indexer.product_enrich as process_products
  
      def _fake_build_index_content_fields(
          items: List[Dict[str, str]],
          tenant_id: str | None = None,
          enrichment_scopes: List[str] | None = None,
          category_taxonomy_profile: str = "apparel",
      ):
          assert tenant_id == "162"
          assert enrichment_scopes == ["category_taxonomy"]
          assert category_taxonomy_profile == "toys"
          return [
              {
                  "id": items[0]["spu_id"],
                  "qanchors": {},
                  "enriched_tags": {},
                  "enriched_attributes": [],
                  "enriched_taxonomy_attributes": [
                      {"name": "Product Type", "value": {"en": ["doll set"]}},
                      {"name": "Age Group", "value": {"en": ["kids"]}},
                  ],
              }
          ]
  
      monkeypatch.setattr(process_products, "build_index_content_fields", _fake_build_index_content_fields)
  
      response = indexer_client.post(
          "/indexer/enrich-content",
          json={
              "tenant_id": "162",
              "enrichment_scopes": ["category_taxonomy"],
              "category_taxonomy_profile": "toys",
              "items": [{"spu_id": "1001", "title": "Toy"}],
          },
      )
  
      assert response.status_code == 200
      data = response.json()
      assert data["category_taxonomy_profile"] == "toys"
      assert data["results"][0]["enriched_taxonomy_attributes"] == [
          {"name": "Product Type", "value": {"en": ["doll set"]}},
          {"name": "Age Group", "value": {"en": ["kids"]}},
      ]
  
  
2e3670ab   tangwang   index services
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
  def test_indexer_documents_contract(indexer_client: TestClient):
      """POST /indexer/documents: tenant_id + spu_ids, returns success/failed lists (no ES write)."""
      response = indexer_client.post(
          "/indexer/documents",
          json={"tenant_id": "162", "spu_ids": ["1001", "1002"]},
      )
      assert response.status_code == 200
      data = response.json()
      assert "success" in data and "failed" in data
      assert data["total"] == 2
      assert data["success_count"] == 2
      assert data["failed_count"] == 0
      assert len(data["success"]) == 2
      assert data["success"][0]["spu_id"] == "1001"
      assert "document" in data["success"][0]
      assert data["success"][0]["document"]["title"]["zh"] == "Fake doc"
  
  
  def test_indexer_health_contract(indexer_client: TestClient):
      """GET /indexer/health: returns status and database/preloaded_data."""
      response = indexer_client.get("/indexer/health")
      assert response.status_code == 200
      data = response.json()
      assert "status" in data
      assert data["status"] in ("available", "unavailable", "error")
      assert "database" in data or "message" in data
      if "preloaded_data" in data:
          assert "category_mappings" in data["preloaded_data"]
  
  
  def test_indexer_incremental_with_delete_spu_ids(indexer_client: TestClient):
      """POST /indexer/index with delete_spu_ids: explicit delete path."""
      response = indexer_client.post(
          "/indexer/index",
          json={
              "tenant_id": "162",
              "spu_ids": ["1001"],
              "delete_spu_ids": ["2001", "2002"],
          },
      )
      assert response.status_code == 200
      data = response.json()
      assert data["success_count"] == 3
      assert len(data["spu_ids"]) == 1
      assert len(data["delete_spu_ids"]) == 2
      assert data["delete_spu_ids"][0]["status"] == "deleted"
  
  
  def test_indexer_index_validation_both_empty(indexer_client: TestClient):
      """POST /indexer/index: 400 when spu_ids and delete_spu_ids both empty."""
      response = indexer_client.post(
          "/indexer/index",
          json={"tenant_id": "162", "spu_ids": [], "delete_spu_ids": []},
      )
      assert response.status_code == 400
  
  
  def test_indexer_index_validation_max_spu_ids(indexer_client: TestClient):
      """POST /indexer/index: 400 when spu_ids > 100."""
      response = indexer_client.post(
          "/indexer/index",
          json={"tenant_id": "162", "spu_ids": [str(i) for i in range(101)], "delete_spu_ids": []},
      )
      assert response.status_code == 400
  
  
  def test_indexer_build_docs_validation_empty_items(indexer_client: TestClient):
      """POST /indexer/build-docs: 400 when items empty."""
      response = indexer_client.post(
          "/indexer/build-docs",
          json={"tenant_id": "162", "items": []},
      )
      assert response.status_code == 400
  
  
  def test_indexer_documents_validation_empty_spu_ids(indexer_client: TestClient):
      """POST /indexer/documents: 400 when spu_ids empty."""
      response = indexer_client.post(
          "/indexer/documents",
          json={"tenant_id": "162", "spu_ids": []},
      )
      assert response.status_code == 400
  
  
  def test_indexer_build_docs_from_db_validation_empty_spu_ids(indexer_client: TestClient):
      """POST /indexer/build-docs-from-db: 400 when spu_ids empty."""
      response = indexer_client.post(
          "/indexer/build-docs-from-db",
          json={"tenant_id": "162", "spu_ids": []},
      )
      assert response.status_code == 400
  
  
  def test_indexer_build_docs_validation_max_items(indexer_client: TestClient):
      """POST /indexer/build-docs: 400 when items > 200."""
      response = indexer_client.post(
          "/indexer/build-docs",
          json={
              "tenant_id": "162",
              "items": [{"spu": {"id": i, "title": "x"}, "skus": [], "options": []} for i in range(201)],
          },
      )
      assert response.status_code == 400
  
  
  def test_indexer_build_docs_from_db_validation_max_spu_ids(indexer_client: TestClient):
      """POST /indexer/build-docs-from-db: 400 when spu_ids > 200."""
      response = indexer_client.post(
          "/indexer/build-docs-from-db",
          json={"tenant_id": "162", "spu_ids": [str(i) for i in range(201)]},
      )
      assert response.status_code == 400
  
  
be3f0d46   tangwang   /indexer/enrich-c...
619
620
621
622
623
624
  def test_indexer_enrich_content_validation_max_items(indexer_client: TestClient):
      response = indexer_client.post(
          "/indexer/enrich-content",
          json={
              "tenant_id": "162",
              "items": [{"spu_id": str(i), "title": "x"} for i in range(51)],
be3f0d46   tangwang   /indexer/enrich-c...
625
626
627
628
629
          },
      )
      assert response.status_code == 400
  
  
2e3670ab   tangwang   index services
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
  def test_indexer_documents_validation_max_spu_ids(indexer_client: TestClient):
      """POST /indexer/documents: 400 when spu_ids > 100."""
      response = indexer_client.post(
          "/indexer/documents",
          json={"tenant_id": "162", "spu_ids": [str(i) for i in range(101)]},
      )
      assert response.status_code == 400
  
  
  def test_indexer_index_validation_max_delete_spu_ids(indexer_client: TestClient):
      """POST /indexer/index: 400 when delete_spu_ids > 100."""
      response = indexer_client.post(
          "/indexer/index",
          json={"tenant_id": "162", "spu_ids": [], "delete_spu_ids": [str(i) for i in range(101)]},
      )
      assert response.status_code == 400
  
  
7299bae6   tangwang   tests
648
  class _FakeTextModel:
b754fd41   tangwang   图片向量化支持优先级参数
649
650
651
652
653
654
655
656
657
      """Matches TEI / server path: `_text_model.encode(...)` (not encode_batch)."""
  
      def encode(
          self,
          texts,
          batch_size=32,
          device="cpu",
          normalize_embeddings=True,
      ):
7299bae6   tangwang   tests
658
659
660
661
          return [np.array([0.1, 0.2, 0.3], dtype=np.float32) for _ in texts]
  
  
  class _FakeImageModel:
77ab67ad   tangwang   更新测试用例
662
      def encode_image_urls(self, urls, batch_size=8, normalize_embeddings=True):
7299bae6   tangwang   tests
663
664
          return [np.array([0.3, 0.2, 0.1], dtype=np.float32) for _ in urls]
  
7a013ca7   tangwang   多模态文本向量服务ok
665
666
667
      def encode_clip_texts(self, texts, batch_size=8, normalize_embeddings=True):
          return [np.array([0.31, 0.21, 0.11], dtype=np.float32) for _ in texts]
  
7299bae6   tangwang   tests
668
  
b754fd41   tangwang   图片向量化支持优先级参数
669
670
671
672
673
674
675
676
677
678
679
680
  class _EmbeddingCacheMiss:
      """Avoid Redis/module cache hits so contract tests exercise the encode path."""
  
      redis_client = None
  
      def get(self, key):
          return None
  
      def set(self, key, value):
          return True
  
  
7299bae6   tangwang   tests
681
  @pytest.fixture
950a640e   tangwang   embeddings
682
  def embedding_module():
7299bae6   tangwang   tests
683
684
685
686
687
      import embeddings.server as emb_server
  
      emb_server.app.router.on_startup.clear()
      emb_server._text_model = _FakeTextModel()
      emb_server._image_model = _FakeImageModel()
b754fd41   tangwang   图片向量化支持优先级参数
688
689
690
      emb_server._text_backend_name = "tei"
      emb_server._text_cache = _EmbeddingCacheMiss()
      emb_server._image_cache = _EmbeddingCacheMiss()
7a013ca7   tangwang   多模态文本向量服务ok
691
      emb_server._clip_text_cache = _EmbeddingCacheMiss()
950a640e   tangwang   embeddings
692
      yield emb_server
7299bae6   tangwang   tests
693
694
  
  
950a640e   tangwang   embeddings
695
  def test_embedding_text_contract(embedding_module):
b754fd41   tangwang   图片向量化支持优先级参数
696
697
698
699
700
701
702
      """Contract via HTTP like production; route handlers require Request/Response."""
      from fastapi.testclient import TestClient
  
      with TestClient(embedding_module.app) as client:
          resp = client.post("/embed/text", json=["hello", "world"])
      assert resp.status_code == 200
      data = resp.json()
7299bae6   tangwang   tests
703
704
705
706
      assert len(data) == 2
      assert len(data[0]) == 3
  
  
950a640e   tangwang   embeddings
707
  def test_embedding_image_contract(embedding_module):
b754fd41   tangwang   图片向量化支持优先级参数
708
709
710
711
712
713
      from fastapi.testclient import TestClient
  
      with TestClient(embedding_module.app) as client:
          resp = client.post("/embed/image", json=["https://example.com/a.jpg"])
      assert resp.status_code == 200
      data = resp.json()
950a640e   tangwang   embeddings
714
      assert len(data[0]) == 3
7299bae6   tangwang   tests
715
716
  
  
7a013ca7   tangwang   多模态文本向量服务ok
717
718
719
720
721
722
723
724
725
726
727
  def test_embedding_clip_text_contract(embedding_module):
      from fastapi.testclient import TestClient
  
      with TestClient(embedding_module.app) as client:
          resp = client.post("/embed/clip_text", json=["纯棉短袖", "street tee"])
      assert resp.status_code == 200
      data = resp.json()
      assert len(data) == 2
      assert len(data[0]) == 3
  
  
7299bae6   tangwang   tests
728
  class _FakeTranslator:
0fd2f875   tangwang   translate
729
730
731
732
733
734
735
736
737
738
739
740
741
      model = "qwen-mt"
      supports_batch = True
  
      def translate(
          self,
          text: str | List[str],
          target_lang: str,
          source_lang: str | None = None,
          scene: str | None = None,
      ):
          del source_lang, scene
          if isinstance(text, list):
              return [f"{item}-{target_lang}" for item in text]
7299bae6   tangwang   tests
742
743
744
          return f"{text}-{target_lang}"
  
  
a7bb846c   tangwang   monitor
745
  class _FailingTranslator:
0fd2f875   tangwang   translate
746
747
748
749
750
751
752
753
754
755
756
      model = "qwen-mt"
      supports_batch = True
  
      def translate(
          self,
          text: str | List[str],
          target_lang: str,
          source_lang: str | None = None,
          scene: str | None = None,
      ):
          del text, target_lang, source_lang, scene
a7bb846c   tangwang   monitor
757
758
759
          return None
  
  
7299bae6   tangwang   tests
760
761
762
763
764
  @pytest.fixture
  def translator_client(monkeypatch):
      import api.translator_app as translator_app
  
      translator_app.app.router.on_startup.clear()
0fd2f875   tangwang   translate
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
  
      class _FakeService:
          def __init__(self, translator):
              self._translator = translator
              self.config = {
                  "default_model": "qwen-mt",
                  "default_scene": "general",
                  "capabilities": {
                      "qwen-mt": {
                          "enabled": True,
                          "backend": "qwen_mt",
                          "model": "qwen-mt-flash",
                          "base_url": "https://example.com",
                          "timeout_sec": 10.0,
                          "use_cache": True,
                      }
                  },
                  "cache": {
0fd2f875   tangwang   translate
783
784
                      "ttl_seconds": 60,
                      "sliding_expiration": True,
0fd2f875   tangwang   translate
785
786
787
788
789
790
791
792
793
794
795
796
797
798
                  },
              }
              self.available_models = ["qwen-mt"]
              self.loaded_models = ["qwen-mt"]
  
          def get_backend(self, model=None):
              del model
              return self._translator
  
          def translate(self, **kwargs):
              kwargs.pop("model", None)
              return self._translator.translate(**kwargs)
  
      monkeypatch.setattr(translator_app, "get_translation_service", lambda: _FakeService(_FakeTranslator()))
7299bae6   tangwang   tests
799
800
801
802
803
804
805
806
807
808
809
810
811
812
  
      with TestClient(translator_app.app) as client:
          yield client
  
  
  def test_translator_api_contract(translator_client: TestClient):
      response = translator_client.post(
          "/translate",
          json={"text": "商品名称", "target_lang": "en", "source_lang": "zh"},
      )
      assert response.status_code == 200
      assert response.json()["translated_text"] == "商品名称-en"
  
  
a7bb846c   tangwang   monitor
813
814
815
816
  def test_translator_api_failure_returns_500(monkeypatch):
      import api.translator_app as translator_app
  
      translator_app.app.router.on_startup.clear()
0fd2f875   tangwang   translate
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
  
      class _FakeService:
          def __init__(self, translator):
              self._translator = translator
              self.config = {
                  "default_model": "qwen-mt",
                  "default_scene": "general",
                  "capabilities": {
                      "qwen-mt": {
                          "enabled": True,
                          "backend": "qwen_mt",
                          "model": "qwen-mt-flash",
                          "base_url": "https://example.com",
                          "timeout_sec": 10.0,
                          "use_cache": True,
                      }
                  },
                  "cache": {
0fd2f875   tangwang   translate
835
836
                      "ttl_seconds": 60,
                      "sliding_expiration": True,
0fd2f875   tangwang   translate
837
838
839
840
841
842
843
844
845
846
847
848
849
850
                  },
              }
              self.available_models = ["qwen-mt"]
              self.loaded_models = ["qwen-mt"]
  
          def get_backend(self, model=None):
              del model
              return self._translator
  
          def translate(self, **kwargs):
              kwargs.pop("model", None)
              return self._translator.translate(**kwargs)
  
      monkeypatch.setattr(translator_app, "get_translation_service", lambda: _FakeService(_FailingTranslator()))
a7bb846c   tangwang   monitor
851
852
853
854
855
856
857
858
859
860
861
  
      with TestClient(translator_app.app) as client:
          response = client.post(
              "/translate",
              json={"text": "商品名称", "target_lang": "en", "source_lang": "zh"},
          )
  
      assert response.status_code == 500
      assert response.json()["detail"] == "Translation failed"
  
  
7299bae6   tangwang   tests
862
863
864
865
  def test_translator_health_contract(translator_client: TestClient):
      response = translator_client.get("/health")
      assert response.status_code == 200
      assert response.json()["status"] == "healthy"
0fd2f875   tangwang   translate
866
      assert response.json()["loaded_models"] == ["qwen-mt"]
7299bae6   tangwang   tests
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
  
  
  class _FakeReranker:
      _model_name = "fake-reranker"
  
      def score_with_meta(self, query: str, docs: List[str], normalize: bool = True):
          scores = [float(i + 1) for i in range(len(docs))]
          meta: Dict[str, Any] = {"input_docs": len(docs), "unique_docs": len(set(docs))}
          return scores, meta
  
  
  @pytest.fixture
  def reranker_client():
      import reranker.server as reranker_server
  
      reranker_server.app.router.on_startup.clear()
      reranker_server._reranker = _FakeReranker()
      reranker_server._backend_name = "fake"
  
      with TestClient(reranker_server.app) as client:
          yield client
  
  
  def test_reranker_api_contract(reranker_client: TestClient):
      response = reranker_client.post(
          "/rerank",
          json={"query": "wireless mouse", "docs": ["doc-a", "doc-b"]},
      )
      assert response.status_code == 200
      data = response.json()
      assert data["scores"] == [1.0, 2.0]
      assert data["meta"]["input_docs"] == 2
  
  
  def test_reranker_health_contract(reranker_client: TestClient):
      response = reranker_client.get("/health")
      assert response.status_code == 200
      assert response.json()["status"] == "ok"