test_ingest_payloads_script.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from __future__ import annotations
  2. from uuid import uuid4
  3. from decode_content.models import IngestRecord, PayloadDraft
  4. from scripts.ingest_payloads import ingest_existing_payloads, list_payload_drafts_for_run
  5. def _payload():
  6. return {
  7. "source": {"id": "xhs_1", "source_type": "post", "title": "帖子"},
  8. "title": "街拍姿势",
  9. "content": "侧身站立可以拉长身体线条。",
  10. "dim_attributes": ["how"],
  11. "dim_creations": ["创作"],
  12. "scopes": [{"scope_type": "effect", "value": "显高"}],
  13. "custom_ext": [{"key": "业务阶段", "type": "str", "value": "灵感"}],
  14. }
  15. class FakeRepo:
  16. def __init__(self):
  17. self.queries = []
  18. self.marked = []
  19. self.records = []
  20. def _all(self, sql, params):
  21. self.queries.append((sql, params))
  22. return [
  23. {
  24. "id": uuid4(),
  25. "particle_id": None,
  26. "item_id": uuid4(),
  27. "payload": _payload(),
  28. "review_status": "pending",
  29. "ingest_ready": False,
  30. "status": "draft",
  31. "error_message": None,
  32. "created_at": None,
  33. "updated_at": None,
  34. }
  35. ]
  36. def mark_payload_draft_ingested(self, payload_draft_id):
  37. self.marked.append(payload_draft_id)
  38. return PayloadDraft(
  39. id=payload_draft_id,
  40. payload=_payload(),
  41. review_status="approved",
  42. ingest_ready=True,
  43. status="ingested",
  44. )
  45. def save_ingest_record(self, **kwargs):
  46. record = IngestRecord(id=uuid4(), **kwargs)
  47. self.records.append(record)
  48. return record
  49. def test_list_payload_drafts_for_run_skips_real_ingested_by_default():
  50. repo = FakeRepo()
  51. run_id = uuid4()
  52. drafts = list_payload_drafts_for_run(repo, run_id=run_id)
  53. sql, params = repo.queries[0]
  54. assert drafts and isinstance(drafts[0], PayloadDraft)
  55. assert params == (run_id,)
  56. assert "target_system = 'knowledge-ingest-api'" in sql
  57. assert "ir.status = 'ingested'" in sql
  58. def test_list_payload_drafts_for_run_can_include_real_ingested():
  59. repo = FakeRepo()
  60. list_payload_drafts_for_run(repo, run_id=uuid4(), include_real_ingested=True)
  61. sql, _params = repo.queries[0]
  62. assert "target_system = 'knowledge-ingest-api'" not in sql
  63. def test_ingest_existing_payloads_dry_run_records_without_http():
  64. repo = FakeRepo()
  65. draft = PayloadDraft(id=uuid4(), payload=_payload(), review_status="pending", ingest_ready=False, status="draft")
  66. records = ingest_existing_payloads(repo, [draft], dry_run=True, env_file=".env")
  67. assert repo.marked == [draft.id]
  68. assert records[0]["target_system"] == "dry-run"
  69. assert records[0]["status"] == "ingested"