Files
o2o-weather/backend/tests/test_saju_api.py
T
minaChoi 0399942fb1 feat: 날씨 사주 — 원국(만세력)·일진 해석·조후 날씨 궁합으로 오늘 점수와 할 일, 레벨로 이번 주·금전·연애·궁합·이달·올해
- 엔진(services/saju): 절입 시각을 천문 계산(KASI 대비 최대 41초), 지방평균시·서머타임 보정, 네 기둥, 십성·합충형파해원진·도화·홍염, 조후(한난조습)와 시간별 날씨 맞춤
- API: POST /v1/saju/reading, POST /v1/saju/match (레벨 5). 생년월일은 저장하지 않음, 레벨은 서버에서 잠금
- 화면(/saju): 오늘의 점수(사주+날씨 궁합)와 결정 카드(언제·할 것·조심할 것·좋은 흐름·입을 것·갈 곳), 내 기운(계절·날씨 장면 16종), 잠긴 것은 다음 하나와 남은 각
- 오늘 탭 아이콘 줄에 날씨 사주, 사주 보면 +10각
- GA4 saju_input·saju_view
2026-10-28 15:44:08 +09:00

84 lines
4.3 KiB
Python

import pytest
from httpx import ASGITransport, AsyncClient
from sqlalchemy import update
from common.database.db_session_manager import DB_SESSION_MNG
from common.database.model.models import User
from router.router import app
from services.saju_service import window_text
REGION = "1144000000"
BIRTH = {"region_code": REGION, "birth_date": "1995-03-14"}
pytestmark = pytest.mark.usefixtures("no_bot_comments")
def client():
return AsyncClient(transport=ASGITransport(app=app), base_url="http://test")
async def _login(c, login_id: str, points: int = 0) -> dict:
body = (await c.post("/v1/auth/signup", json={"login_id": login_id, "password": "password1", "nickname": login_id})).json()
if points:
await DB_SESSION_MNG.execute_lambda_write(lambda s: s.execute(update(User).where(User.login_id == login_id).values(points=points)))
return {"Authorization": f"Bearer {body['access_token']}"}
def test_window_text():
assert window_text((13, 14, 15)) == "오후 1~4시"
assert window_text((11, 12, 13)) == "오전 11시~오후 2시"
assert window_text((15,)) == "오후 3시 전후"
@pytest.mark.anyio
async def test_reading_for_anyone_gives_me_today_and_cards(offline):
async with client() as c:
r = (await c.post("/v1/saju/reading", json=BIRTH)).json()
assert [p["stem"] + p["branch"] for p in r["me"]["pillars"]] == ["乙亥", "己卯", "甲辰"] and r["me"]["hour_known"] is False
assert r["me"]["scene"].endswith("기운") and r["today"]["title"] and 0 <= r["today"]["score"] <= 100
kinds = [x["kind"] for x in r["today"]["cards"]]
assert "when" in kinds and "do" in kinds and "wear" in kinds
# 로그인 안 하면 레벨로 여는 것은 모두 잠김, 내용도 안 옴
assert r["logged_in"] is False and all(not x["unlocked"] for x in r["locks"])
assert "week" not in r and "love" not in r and "money" not in r
# 시각을 주면 시주까지
r = (await c.post("/v1/saju/reading", json={**BIRTH, "birth_time": "07:30"})).json()
assert len(r["me"]["pillars"]) == 4 and r["me"]["hour_known"] is True
assert (await c.post("/v1/saju/reading", json={**BIRTH, "birth_date": "1899-12-31"})).json()["result"]["desc"] == "INVALID_INPUT"
assert (await c.post("/v1/saju/reading", json={**BIRTH, "birth_time": "25:00"})).status_code == 422
@pytest.mark.anyio
async def test_level_unlocks_sections_and_awards_once(offline):
async with client() as c:
h = await _login(c, "saju_01")
r = (await c.post("/v1/saju/reading", json=BIRTH, headers=h)).json()
assert r["logged_in"] is True and r["level"] == 1 and "week" not in r
await c.post("/v1/saju/reading", json=BIRTH, headers=h)
me = (await c.get("/v1/level/me", headers=h)).json()
assert next(a["earned"] for a in me["today"] if a["action"] == "saju") == 10
h4 = await _login(c, "saju_04", points=200)
r = (await c.post("/v1/saju/reading", json={**BIRTH, "gender": "male"}, headers=h4)).json()
assert r["level"] == 4
assert len(r["week"]) == 7 and sum(d["best"] for d in r["week"]) == 1
assert r["money"] and r["work"] and r["love"]["gender_known"] is True
assert "month" not in r and "year" not in r
unlocked = {x["key"]: x["unlocked"] for x in r["locks"]}
assert unlocked["saju_love"] and not unlocked["saju_match"]
h7 = await _login(c, "saju_07", points=1000)
r = (await c.post("/v1/saju/reading", json=BIRTH, headers=h7)).json()
assert r["month"]["title"].endswith("달") and r["year"]["label"].endswith("년")
@pytest.mark.anyio
async def test_match_needs_level_five(offline):
body = {"region_code": REGION, "me": {"birth_date": "1995-03-14"}, "friend": {"birth_date": "1996-11-02"}}
async with client() as c:
assert (await c.post("/v1/saju/match", json=body)).json()["result"]["desc"] == "AUTH_REQUIRED"
h = await _login(c, "match_01")
assert (await c.post("/v1/saju/match", json=body, headers=h)).json()["result"]["desc"] == "LEVEL_REQUIRED"
h5 = await _login(c, "match_05", points=400)
m = (await c.post("/v1/saju/match", json=body, headers=h5)).json()
assert m["title"].endswith("사이") and m["lines"] and 0 <= m["score"] <= 100 and m["day"]["weekday"] and m["friend_scene"].endswith("기운")