Files
hufs-notice-crawler/tests/test_api.py
nkey ca460453af
All checks were successful
hufs-notice-crawler-cicd / build_push_deploy (push) Successful in 8m35s
Feat: [main] hufs-notice-crawler CI/CD까지 구현 완료
2026-03-17 17:18:16 +09:00

65 lines
2.3 KiB
Python

from datetime import datetime
from fastapi.testclient import TestClient
from app.main import app, get_db
from app.schemas import CrawlResponse
def test_health_endpoint(monkeypatch):
monkeypatch.setattr("app.main.Base.metadata.create_all", lambda bind: None)
with TestClient(app) as client:
response = client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "ok"}
def test_crawl_endpoint_returns_mocked_posts(monkeypatch):
monkeypatch.setattr("app.main.Base.metadata.create_all", lambda bind: None)
monkeypatch.setattr(
"app.main.CrawlService.crawl_new_posts",
lambda self: CrawlResponse(
checked_at=datetime(2026, 3, 17, 12, 0, 0),
bootstrap_mode=False,
bootstrap_inserted_count=0,
new_posts_count=1,
new_posts=[
{
"board_key": "notice",
"board_name": "공지사항",
"board_id": 1926,
"article_id": 1001,
"title": "테스트 공지",
"post_url": "https://computer.hufs.ac.kr/computer/10058/subview.do?enc=test-notice-link",
"author": "관리자",
"published_at": datetime(2026, 3, 17),
"summary": "요약",
"content_text": "본문",
"attachments": [
{"name": "guide.pdf", "url": "https://computer.hufs.ac.kr/files/guide.pdf"}
],
}
],
latest_posts_by_board=[],
),
)
app.dependency_overrides[get_db] = lambda: iter([None])
try:
with TestClient(app) as client:
response = client.post("/api/v1/crawl")
finally:
app.dependency_overrides.clear()
assert response.status_code == 200
payload = response.json()
assert payload["bootstrap_mode"] is False
assert payload["new_posts_count"] == 1
assert payload["new_posts"][0]["board_key"] == "notice"
assert payload["new_posts"][0]["article_id"] == 1001
assert "/computer/10058/subview.do?enc=" in payload["new_posts"][0]["post_url"]
assert payload["new_posts"][0]["attachments"][0]["name"] == "guide.pdf"
assert payload["latest_posts_by_board"] == []