test_oss.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """OSS 转存离线测试:纯 parser + upload_stream + to_oss 兜底(注入 http_post)。"""
  2. from __future__ import annotations
  3. import httpx
  4. import pytest
  5. from core.config import PgConfig, Settings
  6. from acquisition.oss import (
  7. OssError, parse_oss_response, to_oss, upload_stream,
  8. )
  9. def _settings() -> Settings:
  10. return Settings(
  11. pg=PgConfig(host="h", port=5432, user="u", password="p", database="d"),
  12. aiddit_crawler_base_url="x", crawler_timeout=30,
  13. openrouter_timeout_seconds=90,
  14. openrouter_model="m",
  15. openrouter_base_url="b", openrouter_api_key="k",
  16. llm_model="m", max_cards=12, frames_dir="f", douyin_ratio="540p", data_dir="",
  17. oss_upload_url="http://oss.test/upload_stream", oss_upload_timeout_seconds=60)
  18. class _Resp:
  19. def __init__(self, payload): self._p = payload
  20. def raise_for_status(self): return None
  21. def json(self): return self._p
  22. def test_parse_ok():
  23. assert parse_oss_response(
  24. {"status": 0, "oss_object": {"cdn_url": "https://res/x.mp4"}}) == "https://res/x.mp4"
  25. def test_parse_status_error():
  26. with pytest.raises(OssError):
  27. parse_oss_response({"status": 1, "msg": "fail"})
  28. def test_parse_missing_cdn():
  29. with pytest.raises(OssError):
  30. parse_oss_response({"status": 0, "oss_object": {}})
  31. def test_upload_stream_posts_and_returns_cdn():
  32. captured = {}
  33. def post(url, json=None, headers=None, timeout=None):
  34. captured["url"] = url
  35. captured["json"] = json
  36. return _Resp({"status": 0, "oss_object": {"cdn_url": "https://res/y.webp"}})
  37. cdn = upload_stream("http://src/y.webp", src_type="image",
  38. settings=_settings(), http_post=post)
  39. assert cdn == "https://res/y.webp"
  40. assert captured["url"] == "http://oss.test/upload_stream"
  41. assert captured["json"] == {"src_url": "http://src/y.webp", "src_type": "image"}
  42. def test_upload_stream_bad_src_type():
  43. with pytest.raises(OssError):
  44. upload_stream("http://src/a", src_type="audio", settings=_settings())
  45. def test_to_oss_fallback_on_http_error():
  46. def post(url, json=None, headers=None, timeout=None):
  47. raise httpx.ConnectError("boom")
  48. # 失败返回原 url,永不抛
  49. assert to_oss("http://src/z.mp4", "video",
  50. settings=_settings(), http_post=post) == "http://src/z.mp4"
  51. def test_to_oss_fallback_on_business_error():
  52. def post(url, json=None, headers=None, timeout=None):
  53. return _Resp({"status": 500, "msg": "boom"})
  54. assert to_oss("http://src/z.mp4", "video",
  55. settings=_settings(), http_post=post) == "http://src/z.mp4"
  56. def test_to_oss_success():
  57. def post(url, json=None, headers=None, timeout=None):
  58. return _Resp({"status": 0, "oss_object": {"cdn_url": "https://res/z.mp4"}})
  59. assert to_oss("http://src/z.mp4", "video",
  60. settings=_settings(), http_post=post) == "https://res/z.mp4"
  61. def test_to_oss_empty_url_passthrough():
  62. assert to_oss("", "image", settings=_settings()) == ""