[feat] solution/backend: 일정 읽기·덮어쓰기 — 업장×기간 한 행
This commit is contained in:
parent
d80877fe75
commit
0f3861c1e4
41
solution/backend/crud/place_itinerary_crud.py
Normal file
41
solution/backend/crud/place_itinerary_crud.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
from sqlalchemy import select
|
||||||
|
from sqlalchemy.dialects.postgresql import insert as pg_insert
|
||||||
|
|
||||||
|
from common.database.db_session_manager import DB_SESSION_MNG
|
||||||
|
from common.database.model.models import place_itineraries
|
||||||
|
from common.utils.gtime import GTime
|
||||||
|
|
||||||
|
|
||||||
|
class PlaceItineraryCRUD:
|
||||||
|
async def list_by_place(self, db, place_id):
|
||||||
|
"""업장의 일정 전부(기간별 한 행). 정렬은 기간 이름 순이 아니라 저장 순이 아니다 —
|
||||||
|
화면 탭 순서는 읽는 쪽(`snapshot._local_contents`)이 DURATIONS 순으로 정한다."""
|
||||||
|
return await DB_SESSION_MNG.execute(
|
||||||
|
db,
|
||||||
|
select(place_itineraries).where(
|
||||||
|
place_itineraries.place_id == place_id,
|
||||||
|
place_itineraries.deleted == False, # noqa: E712
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def upsert(self, db, values: dict):
|
||||||
|
"""업장 × 기간 한 행의 삽입/갱신.
|
||||||
|
|
||||||
|
★ `uq_place_itineraries`(place_id, duration — deleted = false)에 태운다.
|
||||||
|
ON CONFLICT 술어는 인덱스 술어와 **글자 그대로** 같아야 한다. 아니면 포스트그레스가
|
||||||
|
"no unique or exclusion constraint matching" 으로 거절한다 — 표도 컬럼도 멀쩡해서
|
||||||
|
눈으로는 원인이 안 보인다(`local_content_crud.upsert_kind` 주석과 같은 함정).
|
||||||
|
"""
|
||||||
|
stmt = pg_insert(place_itineraries).values(**values)
|
||||||
|
stmt = stmt.on_conflict_do_update(
|
||||||
|
index_elements=[place_itineraries.place_id, place_itineraries.duration],
|
||||||
|
index_where=(place_itineraries.deleted == False), # noqa: E712
|
||||||
|
set_={
|
||||||
|
"body": stmt.excluded.body,
|
||||||
|
"generated_by": stmt.excluded.generated_by,
|
||||||
|
"model": stmt.excluded.model,
|
||||||
|
"generated_at": stmt.excluded.generated_at,
|
||||||
|
"updated_at": GTime.UTC(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return await DB_SESSION_MNG.add(db, stmt)
|
||||||
90
solution/backend/tests/test_place_itinerary_crud.py
Normal file
90
solution/backend/tests/test_place_itinerary_crud.py
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
"""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] == ["남의 코스"]
|
||||||
Loading…
Reference in New Issue
Block a user