File size: 3,720 Bytes
6cfe55f | 1 2 3 4 5 6 7 8 9 10 11 12 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 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 | from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app import create_app
@asynccontextmanager
async def noop_lifespan(app: FastAPI):
yield
class FakeService:
def generate_from_url(self, **kwargs):
return {"task_id": kwargs.get("task_id") or "task-1", "article_item_id": 1}
def search(self, platform, keyword, limit=20):
return {"platform": platform, "keyword": keyword, "status": "ok", "message": "", "items": []}
def create_subscription(self, platform, subscription_type, query, label=""):
return {"id": 1, "platform": platform, "type": subscription_type, "query": query, "label": label}
def list_subscriptions(self):
return []
def refresh_subscription(self, subscription_id, limit=20):
return {"subscription_id": subscription_id, "count": 0, "items": []}
def list_items(self, subscription_id=None):
return []
def summarize_item(self, item_id, **kwargs):
return {"task_id": "task-1", "article_item_id": item_id}
def fetch_only_from_url(self, url, platform):
return {"id": 1, "platform": platform, "url": url, "title": "Test Title"}
def import_only_content(self, url, platform, title, content_text, author_name=""):
return {"id": 1, "platform": platform, "url": url, "title": title, "content_text": content_text}
def app_with_fake_service(monkeypatch):
from app.routers import article
monkeypatch.setattr(article, "ArticleService", lambda: FakeService())
return TestClient(create_app(lifespan=noop_lifespan))
def test_generate_article_route(monkeypatch):
client = app_with_fake_service(monkeypatch)
response = client.post(
"/api/articles/generate",
json={
"url": "https://mp.weixin.qq.com/s/a",
"platform": "wechat_mp",
"provider_id": "p",
"model_name": "m",
"style": "minimal",
},
)
assert response.status_code == 200
assert response.json()["data"]["task_id"] == "task-1"
def test_article_search_route(monkeypatch):
client = app_with_fake_service(monkeypatch)
response = client.get("/api/articles/search?platform=xiaohongshu&keyword=AI")
assert response.status_code == 200
assert response.json()["data"]["keyword"] == "AI"
def test_article_subscription_routes(monkeypatch):
client = app_with_fake_service(monkeypatch)
created = client.post(
"/api/article_subscriptions",
json={"platform": "wechat_mp", "type": "publisher", "query": "账号", "label": "账号"},
)
refreshed = client.post("/api/article_subscriptions/1/refresh")
assert created.status_code == 200
assert created.json()["data"]["query"] == "账号"
assert refreshed.json()["data"]["subscription_id"] == 1
def test_fetch_article_route(monkeypatch):
client = app_with_fake_service(monkeypatch)
response = client.post(
"/api/articles/fetch",
json={
"url": "https://mp.weixin.qq.com/s/a",
"platform": "wechat_mp",
},
)
assert response.status_code == 200
assert response.json()["data"]["url"] == "https://mp.weixin.qq.com/s/a"
def test_import_article_route(monkeypatch):
client = app_with_fake_service(monkeypatch)
response = client.post(
"/api/articles/import",
json={
"url": "https://mp.weixin.qq.com/s/a",
"platform": "wechat_mp",
"title": "My Title",
"content_text": "This is a long text content for test.",
},
)
assert response.status_code == 200
assert response.json()["data"]["title"] == "My Title"
|