Files
o2o-weather/backend/tests/test_saju_api.py
T

110 lines
5.9 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["today"]["why"] and not any(ch.isdigit() for w in r["today"]["why"] for ch in w)
# 로그인 안 하면 레벨로 여는 것은 모두 잠김, 내용도 안 옴
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 all(isinstance(x, str) for x in r["money"]) 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 "년" in r["year"]["label"] and 1 <= len(r["year"]["months"]) <= 3
@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("기운")
@pytest.mark.anyio
async def test_match_score_is_same_whoever_goes_first(offline):
a, b = {"birth_date": "1995-03-14"}, {"birth_date": "1996-11-02"}
async with client() as c:
h = await _login(c, "match_sym", points=400)
ab = (await c.post("/v1/saju/match", json={"region_code": REGION, "me": a, "friend": b}, headers=h)).json()
ba = (await c.post("/v1/saju/match", json={"region_code": REGION, "me": b, "friend": a}, headers=h)).json()
assert ab["score"] == ba["score"]
@pytest.mark.anyio
async def test_birth_is_saved_to_account(offline):
body = {"birth_date": "1995-03-14", "birth_time": "07:30", "place": "busan", "gender": "female"}
async with client() as c:
assert (await c.get("/v1/saju/birth")).status_code == 401
h = await _login(c, "birth_01")
assert "birth" not in (await c.get("/v1/saju/birth", headers=h)).json()
assert (await c.put("/v1/saju/birth", json=body, headers=h)).json()["birth"] == body
assert (await c.get("/v1/saju/birth", headers=h)).json()["birth"] == body
assert (await c.put("/v1/saju/birth", json={**body, "birth_date": "1899-12-31"}, headers=h)).json()["result"]["desc"] == "INVALID_INPUT"
await c.delete("/v1/saju/birth", headers=h)
assert "birth" not in (await c.get("/v1/saju/birth", headers=h)).json()