Blame view

tests/ci/test_service_api_contracts.py 29.6 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
14
15
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
  
  
  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 ...
99
100
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
  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
145
146
147
148
149
150
151
152
153
154
155
  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   更新测试用例
156
      def transform_spu_to_doc(self, tenant_id: str, spu_row, skus, options, **kwargs):
7299bae6   tangwang   tests
157
158
159
160
161
162
163
          return {
              "tenant_id": tenant_id,
              "spu_id": str(spu_row.get("id", "0")),
              "title": {"zh": str(spu_row.get("title", ""))},
          }
  
  
2e3670ab   tangwang   index services
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  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
182
  class _FakeIncrementalService:
2e3670ab   tangwang   index services
183
184
185
186
      def __init__(self):
          self.db_engine = _FakeDbEngine()
          self.category_id_to_name = {}
  
7299bae6   tangwang   tests
187
      def index_spus_to_es(self, es_client, tenant_id: str, spu_ids: List[str], delete_spu_ids=None):
2e3670ab   tangwang   index services
188
          out = {
7299bae6   tangwang   tests
189
190
191
192
193
194
195
              "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
196
197
198
199
200
201
202
203
204
205
206
207
          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
208
209
210
211
  
      def _get_transformer_bundle(self, tenant_id: str):
          return _FakeTransformer(), None, False
  
2e3670ab   tangwang   index services
212
213
214
215
216
217
218
219
220
221
222
      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
223
224
225
226
227
228
  
  @pytest.fixture
  def indexer_client(monkeypatch):
      import api.indexer_app as indexer_app
      import api.routes.indexer as indexer_routes
  
ed948666   tangwang   tidy
229
      indexer_app.app.router.on_startup.clear()
7299bae6   tangwang   tests
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
270
271
      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
272
273
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
  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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  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...
345
  def test_indexer_enrich_content_contract(indexer_client: TestClient, monkeypatch):
6f7840cf   tangwang   refactor: rename ...
346
      import indexer.product_enrich as process_products
be3f0d46   tangwang   /indexer/enrich-c...
347
  
5aaf0c7d   tangwang   feat(indexer): 完善...
348
349
350
      def _fake_build_index_content_fields(
          items: List[Dict[str, str]],
          tenant_id: str | None = None,
2703b6ea   tangwang   refactor(indexer)...
351
352
          enrichment_scopes: List[str] | None = None,
          category_taxonomy_profile: str = "apparel",
5aaf0c7d   tangwang   feat(indexer): 完善...
353
      ):
d350861f   tangwang   索引结构修改
354
          assert tenant_id == "162"
2703b6ea   tangwang   refactor(indexer)...
355
356
          assert enrichment_scopes == ["generic", "category_taxonomy"]
          assert category_taxonomy_profile == "apparel"
be3f0d46   tangwang   /indexer/enrich-c...
357
358
          return [
              {
d350861f   tangwang   索引结构修改
359
360
361
362
363
364
365
                  "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...
366
                      {"name": "enriched_tags", "value": {"zh": ["tag1"], "en": ["tag1"]}},
d350861f   tangwang   索引结构修改
367
                  ],
5aaf0c7d   tangwang   feat(indexer): 完善...
368
369
370
                  "enriched_taxonomy_attributes": [
                      {"name": "Product Type", "value": {"zh": ["T恤"], "en": ["t-shirt"]}},
                  ],
be3f0d46   tangwang   /indexer/enrich-c...
371
              }
d350861f   tangwang   索引结构修改
372
              for p in items
be3f0d46   tangwang   /indexer/enrich-c...
373
374
          ]
  
d350861f   tangwang   索引结构修改
375
      monkeypatch.setattr(process_products, "build_index_content_fields", _fake_build_index_content_fields)
be3f0d46   tangwang   /indexer/enrich-c...
376
377
378
379
380
  
      response = indexer_client.post(
          "/indexer/enrich-content",
          json={
              "tenant_id": "162",
2703b6ea   tangwang   refactor(indexer)...
381
382
              "enrichment_scopes": ["generic", "category_taxonomy"],
              "category_taxonomy_profile": "apparel",
be3f0d46   tangwang   /indexer/enrich-c...
383
384
385
386
              "items": [
                  {"spu_id": "1001", "title": "T-shirt"},
                  {"spu_id": "1002", "title": "Toy"},
              ],
be3f0d46   tangwang   /indexer/enrich-c...
387
388
389
390
391
          },
      )
      assert response.status_code == 200
      data = response.json()
      assert data["tenant_id"] == "162"
2703b6ea   tangwang   refactor(indexer)...
392
393
      assert data["enrichment_scopes"] == ["generic", "category_taxonomy"]
      assert data["category_taxonomy_profile"] == "apparel"
be3f0d46   tangwang   /indexer/enrich-c...
394
395
396
      assert data["total"] == 2
      assert len(data["results"]) == 2
      assert data["results"][0]["spu_id"] == "1001"
d350861f   tangwang   索引结构修改
397
398
399
400
401
402
      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...
403
          "value": {"zh": ["tag1"], "en": ["tag1"]},
d350861f   tangwang   索引结构修改
404
      }
5aaf0c7d   tangwang   feat(indexer): 完善...
405
406
407
408
      assert data["results"][0]["enriched_taxonomy_attributes"][0] == {
          "name": "Product Type",
          "value": {"zh": ["T恤"], "en": ["t-shirt"]},
      }
be3f0d46   tangwang   /indexer/enrich-c...
409
410
  
  
2703b6ea   tangwang   refactor(indexer)...
411
412
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
  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): 支持...
457
458
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
  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
503
504
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
  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...
617
618
619
620
621
622
  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...
623
624
625
626
627
          },
      )
      assert response.status_code == 400
  
  
2e3670ab   tangwang   index services
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
  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
646
  class _FakeTextModel:
b754fd41   tangwang   图片向量化支持优先级参数
647
648
649
650
651
652
653
654
655
      """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
656
657
658
659
          return [np.array([0.1, 0.2, 0.3], dtype=np.float32) for _ in texts]
  
  
  class _FakeImageModel:
77ab67ad   tangwang   更新测试用例
660
      def encode_image_urls(self, urls, batch_size=8, normalize_embeddings=True):
7299bae6   tangwang   tests
661
662
          return [np.array([0.3, 0.2, 0.1], dtype=np.float32) for _ in urls]
  
7a013ca7   tangwang   多模态文本向量服务ok
663
664
665
      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
666
  
b754fd41   tangwang   图片向量化支持优先级参数
667
668
669
670
671
672
673
674
675
676
677
678
  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
679
  @pytest.fixture
950a640e   tangwang   embeddings
680
  def embedding_module():
7299bae6   tangwang   tests
681
682
683
684
685
      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   图片向量化支持优先级参数
686
687
688
      emb_server._text_backend_name = "tei"
      emb_server._text_cache = _EmbeddingCacheMiss()
      emb_server._image_cache = _EmbeddingCacheMiss()
7a013ca7   tangwang   多模态文本向量服务ok
689
      emb_server._clip_text_cache = _EmbeddingCacheMiss()
950a640e   tangwang   embeddings
690
      yield emb_server
7299bae6   tangwang   tests
691
692
  
  
950a640e   tangwang   embeddings
693
  def test_embedding_text_contract(embedding_module):
b754fd41   tangwang   图片向量化支持优先级参数
694
695
696
697
698
699
700
      """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
701
702
703
704
      assert len(data) == 2
      assert len(data[0]) == 3
  
  
950a640e   tangwang   embeddings
705
  def test_embedding_image_contract(embedding_module):
b754fd41   tangwang   图片向量化支持优先级参数
706
707
708
709
710
711
      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
712
      assert len(data[0]) == 3
7299bae6   tangwang   tests
713
714
  
  
7a013ca7   tangwang   多模态文本向量服务ok
715
716
717
718
719
720
721
722
723
724
725
  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
726
  class _FakeTranslator:
0fd2f875   tangwang   translate
727
728
729
730
731
732
733
734
735
736
737
738
739
      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
740
741
742
          return f"{text}-{target_lang}"
  
  
a7bb846c   tangwang   monitor
743
  class _FailingTranslator:
0fd2f875   tangwang   translate
744
745
746
747
748
749
750
751
752
753
754
      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
755
756
757
          return None
  
  
7299bae6   tangwang   tests
758
759
760
761
762
  @pytest.fixture
  def translator_client(monkeypatch):
      import api.translator_app as translator_app
  
      translator_app.app.router.on_startup.clear()
0fd2f875   tangwang   translate
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
  
      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
781
782
                      "ttl_seconds": 60,
                      "sliding_expiration": True,
0fd2f875   tangwang   translate
783
784
785
786
787
788
789
790
791
792
793
794
795
796
                  },
              }
              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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
  
      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
811
812
813
814
  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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
  
      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
833
834
                      "ttl_seconds": 60,
                      "sliding_expiration": True,
0fd2f875   tangwang   translate
835
836
837
838
839
840
841
842
843
844
845
846
847
848
                  },
              }
              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
849
850
851
852
853
854
855
856
857
858
859
  
      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
860
861
862
863
  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
864
      assert response.json()["loaded_models"] == ["qwen-mt"]
7299bae6   tangwang   tests
865
866
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
  
  
  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"