feat: 포스트를 만들 때 날씨각 캐릭터(해님·구름·달님) 댓글 1~2개 — '날씨각' 표시, 신고·차단 메뉴 없음, 이미 만든 날은 scripts/fill_bot_comments.py

This commit is contained in:
minaChoi committed 2026-10-27 13:14:30 +09:00
1 parent d32e24a5ab
commit 98b5c0cbe3
19 files changed
+864 -4

No files matched your search

+9
View File
@@ -73,6 +73,15 @@ def _no_seoul_crawl(monkeypatch):
monkeypatch.setattr(week_service, "fetch_seoul_festivals", lambda *a: _async([]))
@pytest.fixture
def no_bot_comments(monkeypatch):
"""사람이 단 댓글·좋아요만 세는 테스트용: 포스트를 만들 때 캐릭터 댓글을 달지 않는다 (캐릭터 댓글은 test_bot_comment)."""
import services.feed_service as feed_service
from common.enums import ErrorType
monkeypatch.setattr(feed_service, "fill_bot_comments", lambda *a: _async(ErrorType.SUCCESS))
@pytest.fixture
def offline(monkeypatch):
"""외부 API 를 모두 맑고 잔잔한 가짜 값으로 바꾼다."""
+1
View File
@@ -4,6 +4,7 @@ from httpx import ASGITransport, AsyncClient
from router.router import app
REGION = "1144000000"
pytestmark = pytest.mark.usefixtures("no_bot_comments")
IP_A = {"X-Forwarded-For": "203.0.113.10"}
IP_B = {"X-Forwarded-For": "203.0.113.20"}
+1
View File
@@ -4,6 +4,7 @@ from httpx import ASGITransport, AsyncClient
from router.router import app
REGION = "1144000000"
pytestmark = pytest.mark.usefixtures("no_bot_comments")
def client():
+82
View File
@@ -0,0 +1,82 @@
from datetime import date, datetime
import pytest
from httpx import ASGITransport, AsyncClient
from sqlalchemy import text
from common.database.db_session_manager import DB_SESSION_MNG
from common.enums import PostType
from common.utils.kma_time import KST
from crud.feed_crud import FeedCRUD
from router.router import app
from services.bot_comment import _COPY, BOT_NAMES, _moon_key, bot_lines, fill_bot_comments
from services.feed_service import LINEUP
DAY = date(2001, 1, 2)
def test_every_post_type_has_lines_from_known_characters():
for t in {x[1] for x in LINEUP} | {PostType.CARD}:
assert bot_lines(t, DAY, 1), t
for sets in _COPY.values():
for s in sets:
assert 1 <= len(s) <= 2 and len({bot for bot, _ in s}) == len(s)
assert all(bot in BOT_NAMES and 0 < len(body) <= 60 for bot, body in s)
def test_lines_are_one_or_two_and_same_for_same_post():
counts = {len(bot_lines(PostType.GAK, DAY, post_id)) for post_id in range(40)}
assert counts == {1, 2}
assert bot_lines(PostType.GAK, DAY, 7) == bot_lines(PostType.GAK, DAY, 7)
def test_moon_lines_follow_full_and_new_moon_nights():
assert _moon_key(date(2025, 10, 7)) == "MOON_FULL" # 보름 10.07 12:47
assert _moon_key(date(2024, 4, 8)) == "MOON_NEW" # 삭 04.09 03:21, 그 전날 밤
assert _moon_key(date(2025, 10, 4)) == "MOON"
full = {body for s in _COPY["MOON_FULL"] for _, body in s}
assert bot_lines(PostType.MOON, date(2025, 10, 7), 3)[0][1] in full
@pytest.fixture
async def day_posts():
async def wipe():
await DB_SESSION_MNG.execute_lambda_run([
lambda s: s.execute(text(f"DELETE FROM comment WHERE post_id IN (SELECT post_id FROM post WHERE feed_date = '{DAY}')")),
lambda s: s.execute(text(f"DELETE FROM post WHERE feed_date = '{DAY}'")),
])
await wipe()
rows = [{"feed_date": DAY, "position": i, "scope": "00", "post_type": t.value, "ref_type": 1, "published_at": datetime(2001, 1, 2, 3, tzinfo=KST)}
for i, t in enumerate([PostType.WEATHER, PostType.GAK, PostType.MOON, PostType.SUNSET], 1)]
await DB_SESSION_MNG.execute_lambda_run([lambda s: FeedCRUD().insert_day(s, rows)])
yield
await wipe()
async def _counts():
sql = f"""SELECT p.post_id, p.comment_count, count(c.comment_id) FROM post p LEFT JOIN comment c ON c.post_id = p.post_id AND c.bot IS NOT NULL
WHERE p.feed_date = '{DAY}' GROUP BY p.post_id, p.comment_count ORDER BY p.post_id"""
_, rows = await DB_SESSION_MNG.execute_lambda(lambda s: s.execute(text(sql)))
return [tuple(r) for r in rows.all()]
@pytest.mark.anyio
async def test_fill_adds_one_or_two_comments_once_and_counts_them(day_posts):
await fill_bot_comments(DAY, FeedCRUD())
first = await _counts()
assert len(first) == 4 and all(1 <= n <= 2 and count == n for _, count, n in first)
await fill_bot_comments(DAY, FeedCRUD())
assert await _counts() == first
post_id = first[0][0]
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
comments = (await c.get(f"/v1/reaction/post/{post_id}/comment/list")).json()["comments"]
assert comments and all(x["official"] and x["nickname"] in BOT_NAMES.values() and not x["mine"] for x in comments)
@pytest.mark.anyio
async def test_feed_build_puts_character_comments_on_new_posts(offline):
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
feed = (await c.get("/v1/feed", params={"region": "1144000000"})).json()
assert feed["hero_post"]["comment_count"] >= 1
assert all(p["comment_count"] >= 1 for p in feed["posts"])
+1
View File
@@ -17,6 +17,7 @@ from router.router import app
from services.feed_service import LINEUP, NATIONAL_SCOPE, NATIONAL_TYPES, build_feed_day, feed_day, sido_of
SEOUL_A, SEOUL_B, BUSAN = "1111000000", "1114000000", "2611000000"
pytestmark = pytest.mark.usefixtures("no_bot_comments")
REGION_CSV = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "seed", "region.csv")