fix: 각을 시간대와 관계없이 활성화하고 시간 적합도를 가중치로 반영

This commit is contained in:
skkim committed 2026-10-22 13:38:39 +09:00
1 parent 3b5afba8a1
commit 799a46f53c
13 files changed
+124 -66

No files matched your search

+38 -7
View File
@@ -8,7 +8,7 @@ import services.air_service as air_service
import services.warning_service as warning_service
import services.weather_service as weather_service
from common.database.db_session_manager import DB_SESSION_MNG
from common.database.model.models import OpenMeteoHourly, WeatherHourly
from common.database.model.models import IndexScore, OpenMeteoHourly, WeatherHourly
from common.enums import ErrorType
from common.utils.kma_time import kst_midnight, now_kst
from crud.region_crud import RegionCRUD
@@ -24,6 +24,14 @@ async def _async(v):
return v
def _board_score(index_id, day, score, slot_score=None):
value = score if slot_score is None else slot_score
return IndexScore(index_id=index_id, score_date=day, score=score, slots={
k: {"score": value, "summary": "", "factors": {}, "zero_reason": None, "notes": {}}
for k in ("morning", "day", "evening", "night", "early")
})
def _vilage(*_):
start = kst_midnight(now_kst().date())
items = []
@@ -61,7 +69,8 @@ async def test_index_list_ranks_every_gak_with_today_tomorrow_weekend_trend(fake
assert body["result"]["success"] is True
assert len(body["items"]) == len(_seed()) and len(body["top3"]) == 3
priority = {d["code"]: d["priority"] for d in _seed()}
# 지금 구간에 해당하는 각이 먼저(순위), 해당 없는 각은 그 뒤, 제철 아닌 각은 맨 끝. 앞 두 묶음 안에서는 점수 × 중요도 순
# 시간대와 관계없이 제철인 각은 모두 활성화하고, 제철 아닌 각은 맨 끝.
assert all(i["active"] for i in body["items"] if i["in_season"])
actives = [i["active"] for i in body["items"]]
assert actives == sorted(actives, reverse=True) and all(i["active"] for i in body["top3"])
seasons = [i["in_season"] for i in body["items"]]
@@ -139,7 +148,7 @@ async def test_board_puts_out_of_season_gak_last_without_rank_or_badge(monkeypat
SimpleNamespace(index_id=2, code="now", priority=2, months=[today.month], sort_order=2),
SimpleNamespace(index_id=3, code="off", priority=1, months=other[:3], sort_order=3),
]
scores = [SimpleNamespace(index_id=x, score_date=today, score=v, slots={}) for x, v in ((1, 50), (2, 60), (3, 99))]
scores = [_board_score(x, today, v) for x, v in ((1, 50), (2, 60), (3, 99))]
crud = SimpleNamespace(definitions=lambda s: _async(defs), scores=lambda s, *a: _async(scores))
service = IndexService(RegionCRUD(), crud, None, None, None, None, None, None)
monkeypatch.setattr(service, "ensure", lambda *a: _async((ErrorType.SUCCESS, {"morning": 50, "day": 50, "evening": 50, "night": 50, "early": 50})))
@@ -159,7 +168,7 @@ async def test_board_ranks_by_score_times_priority_weight(monkeypatch):
SimpleNamespace(index_id=2, code="mid", priority=2),
SimpleNamespace(index_id=3, code="high", priority=1),
]
scores = [SimpleNamespace(index_id=x, score_date=today, score=v, slots={}) for x, v in ((1, 90), (2, 80), (3, 70))]
scores = [_board_score(x, today, v) for x, v in ((1, 90), (2, 80), (3, 70))]
crud = SimpleNamespace(definitions=lambda s: _async(defs), scores=lambda s, *a: _async(scores))
service = IndexService(RegionCRUD(), crud, None, None, None, None, None, None)
monkeypatch.setattr(service, "ensure", lambda *a: _async((ErrorType.SUCCESS, {"morning": 50, "day": 50, "evening": 50, "night": 50, "early": 50})))
@@ -175,7 +184,7 @@ async def test_board_ranks_each_slot_with_priority_weight(monkeypatch):
defs = [SimpleNamespace(index_id=1, code="low", priority=3), SimpleNamespace(index_id=2, code="high", priority=1)]
every = ("morning", "day", "evening", "night", "early")
slots = {1: {k: {"score": 90, "summary": "", "factors": {}, "zero_reason": None, "notes": []} for k in every},
2: {k: {"score": 20 if k == "morning" else 70, "summary": "", "factors": {}, "zero_reason": None, "notes": []} for k in every if k not in ("night", "early")}}
2: {k: {"score": 20 if k == "morning" else 70, "summary": "", "factors": {}, "zero_reason": None, "notes": []} for k in every}}
scores = [SimpleNamespace(index_id=x, score_date=today, score=80, slots=slots[x]) for x in (1, 2)]
crud = SimpleNamespace(definitions=lambda s: _async(defs), scores=lambda s, *a: _async(scores))
service = IndexService(RegionCRUD(), crud, None, None, None, None, None, None)
@@ -183,10 +192,32 @@ async def test_board_ranks_each_slot_with_priority_weight(monkeypatch):
monkeypatch.setattr("services.index_service._with_slot", lambda row, data: SimpleNamespace(score=data["score"]))
_, board = await service.board(REGION)
ranks = {i.definition.code: i.slot_ranks for i in board.items}
# 아침: 하 90(63) > 상 20(20). 낮: 상 70(70) > 하 90(63). 상은 밤 구간이 없어 밤 순위도 없다
# 아침: 하 90(63) > 상 20(20). 낮·밤: 상 70(70) > 하 90(63).
assert ranks["low"]["morning"] == 1 and ranks["high"]["morning"] == 2
assert ranks["high"]["day"] == 1 and ranks["low"]["day"] == 2
assert "night" not in ranks["high"]
assert ranks["high"]["night"] == 1 and ranks["low"]["night"] == 2
@pytest.mark.anyio
@pytest.mark.parametrize("cur", ["morning", "day", "evening", "night", "early"])
async def test_board_activates_gak_outside_its_time_window(monkeypatch, cur):
today = now_kst().date()
defs = [SimpleNamespace(index_id=1, code="outside", priority=1)]
row = _board_score(1, today, 85, slot_score=65)
crud = SimpleNamespace(definitions=lambda s: _async(defs), scores=lambda s, *a: _async([row]))
service = IndexService(RegionCRUD(), crud, None, None, None, None, None, None)
monkeypatch.setattr(service, "ensure", lambda *a: _async((ErrorType.SUCCESS, {cur: 50})))
monkeypatch.setattr("services.index_service.current_slot", lambda *a: cur)
err, board = await service.board(REGION)
assert err is ErrorType.SUCCESS
assert len(board.ranked) == 1
item = board.ranked[0]
assert item.active and item.score.score == 65 and item.rank == 1
assert item.trend == [item.score] and item.slot == ("night" if cur == "early" else cur)
assert item.slots == {k: 65 for k in ("morning", "day", "evening", "night")}
assert item.slot_ranks == {k: 1 for k in item.slots}
def _om_row(at, **over):