fix(street): 晋升时重建副图父引用(草稿行 id 到 正式行 id)
This commit is contained in:
@ -159,3 +159,50 @@ func TestStreetMainDetailReparentGrandchildren(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestStreetPromoteRebuildsParentRef 晋升时必须把副图的父引用从「草稿行 id」改写为「新正式行 id」。
|
||||
func TestStreetPromoteRebuildsParentRef(t *testing.T) {
|
||||
repo := NewReviewRepository(testDB(t))
|
||||
ctx := context.Background()
|
||||
|
||||
draftID := seedStreetDraft(t, repo)
|
||||
m1 := seedStreetDraftImage(t, repo, draftID, "main", 1)
|
||||
d2 := seedStreetDraftImage(t, repo, draftID, "detail-a", 2)
|
||||
d3 := seedStreetDraftImage(t, repo, draftID, "detail-b", 3)
|
||||
if err := repo.AttachStreetDraftImages(ctx, draftID, m1, []uint32{d2, d3}); err != nil {
|
||||
t.Fatalf("预置主副图失败: %v", err)
|
||||
}
|
||||
|
||||
snapID, err := repo.SaveStreetSnapFromDraft(ctx, draftID)
|
||||
if err != nil {
|
||||
t.Fatalf("晋升失败: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
db := repo.(*reviewRepository).db
|
||||
db.Exec("DELETE FROM street_snap_images WHERE snap_id = ?", snapID)
|
||||
db.Exec("DELETE FROM street_snaps WHERE id = ?", snapID)
|
||||
})
|
||||
|
||||
db := repo.(*reviewRepository).db
|
||||
var rows []model.StreetSnapImage
|
||||
if err := db.WithContext(ctx).Where("snap_id = ? AND is_deleted = 0", snapID).
|
||||
Order("sort_order ASC, id ASC").Find(&rows).Error; err != nil {
|
||||
t.Fatalf("读正式图失败: %v", err)
|
||||
}
|
||||
if len(rows) != 3 {
|
||||
t.Fatalf("期望 3 行正式图,实际 %d", len(rows))
|
||||
}
|
||||
|
||||
mainRow := rows[0]
|
||||
if mainRow.IsDetail != 0 || mainRow.ParentImageID != 0 {
|
||||
t.Fatalf("首行应是主图,实际 is_detail=%d parent=%d", mainRow.IsDetail, mainRow.ParentImageID)
|
||||
}
|
||||
for _, row := range rows[1:] {
|
||||
if row.IsDetail != 1 {
|
||||
t.Fatalf("第 %d 行应是副图", row.ID)
|
||||
}
|
||||
if row.ParentImageID != mainRow.ID {
|
||||
t.Fatalf("副图 parent 应指向新主图行 id %d,实际 %d", mainRow.ID, row.ParentImageID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user