91 lines
3.3 KiB
Python
91 lines
3.3 KiB
Python
"""place_itineraries 읽기·덮어쓰기.
|
||
|
||
이 표가 지켜야 하는 것:
|
||
- 업장 × 기간 = 한 행. 다시 생성하면 그 행을 덮어쓴다(행이 늘지 않는다)
|
||
- 다른 업장·다른 기간은 서로를 건드리지 않는다
|
||
"""
|
||
import uuid
|
||
|
||
from common.database.db_session_manager import DB_SESSION_MNG
|
||
from common.database.model.models import place_itineraries
|
||
from common.enums import DBWRType, ErrorType, SourceType
|
||
from common.utils.gtime import GTime
|
||
from crud.place_itinerary_crud import PlaceItineraryCRUD
|
||
|
||
_CRUD = PlaceItineraryCRUD()
|
||
|
||
|
||
def _values(place_id, duration: str, names: list[str]) -> dict:
|
||
return {
|
||
"place_itinerary_id": uuid.uuid4(),
|
||
"place_id": place_id,
|
||
"duration": duration,
|
||
"body": [{"name": n, "duration": duration, "days": [{"label": "1일차", "stops": [{"name": "가"}]}]}
|
||
for n in names],
|
||
"generated_by": SourceType.LLM.value,
|
||
"model": "perplexity:sonar",
|
||
"generated_at": GTime.UTC(),
|
||
}
|
||
|
||
|
||
async def _upsert(values: dict) -> ErrorType:
|
||
return await DB_SESSION_MNG.execute_lambda_run(
|
||
[place_itineraries.DBType()], [lambda s: _CRUD.upsert(s, values)],
|
||
)
|
||
|
||
|
||
async def _rows(place_id):
|
||
err, rows = await DB_SESSION_MNG.execute_lambda(
|
||
place_itineraries.DBType(), DBWRType.DB_READ.value,
|
||
lambda s: _CRUD.list_by_place(s, place_id),
|
||
)
|
||
assert err == ErrorType.SUCCESS
|
||
return list(rows or [])
|
||
|
||
|
||
async def test_upsert_then_read(db_engine):
|
||
"""검증: 넣고 읽는다.
|
||
기대결과: body 가 그대로 돌아온다 — 렌더러 계약을 그 자리에서 담는다."""
|
||
pid = uuid.uuid4()
|
||
assert await _upsert(_values(pid, "1박 2일", ["코스A", "코스B"])) == ErrorType.SUCCESS
|
||
|
||
rows = await _rows(pid)
|
||
assert len(rows) == 1
|
||
assert rows[0].duration == "1박 2일"
|
||
assert [c["name"] for c in rows[0].body] == ["코스A", "코스B"]
|
||
assert rows[0].generated_by == SourceType.LLM.value
|
||
|
||
|
||
async def test_upsert_overwrites_same_place_and_duration(db_engine):
|
||
"""검증: 같은 업장·같은 기간을 다시 넣는다.
|
||
기대결과: 행이 늘지 않고 body 가 바뀐다(uq_place_itineraries)."""
|
||
pid = uuid.uuid4()
|
||
await _upsert(_values(pid, "1박 2일", ["옛 코스"]))
|
||
await _upsert(_values(pid, "1박 2일", ["새 코스1", "새 코스2"]))
|
||
|
||
rows = await _rows(pid)
|
||
assert len(rows) == 1
|
||
assert [c["name"] for c in rows[0].body] == ["새 코스1", "새 코스2"]
|
||
|
||
|
||
async def test_two_durations_live_side_by_side(db_engine):
|
||
"""검증: 같은 업장의 두 기간.
|
||
기대결과: 각각 한 행 — 1박2일이 2박3일을 덮지 않는다."""
|
||
pid = uuid.uuid4()
|
||
await _upsert(_values(pid, "1박 2일", ["짧은 코스"]))
|
||
await _upsert(_values(pid, "2박 3일", ["긴 코스"]))
|
||
|
||
rows = await _rows(pid)
|
||
assert {r.duration for r in rows} == {"1박 2일", "2박 3일"}
|
||
|
||
|
||
async def test_other_place_is_untouched(db_engine):
|
||
"""검증: 다른 업장.
|
||
기대결과: 서로 안 보인다 — 키가 place_id 다."""
|
||
mine, other = uuid.uuid4(), uuid.uuid4()
|
||
await _upsert(_values(mine, "1박 2일", ["내 코스"]))
|
||
await _upsert(_values(other, "1박 2일", ["남의 코스"]))
|
||
|
||
assert [c["name"] for c in (await _rows(mine))[0].body] == ["내 코스"]
|
||
assert [c["name"] for c in (await _rows(other))[0].body] == ["남의 코스"]
|