//go:build integration // 集成测试:街拍主副图分组需要真实 PostgreSQL。 // // 单表发布模型下操作对象是正式表本身(street_snaps / street_snap_images), // 不再有草稿表;审核通过只改状态、不重建图片,故本文件不再覆盖「晋升重建父引用」。 // // 运行方式(先起库并确保结构已就绪,见 README「数据库」一节): // // docker compose -f scripts/pgvector/docker-compose.yml up -d // go test -tags integration ./internal/repository/ -run TestStreetMainDetail -v // // 默认 `go test ./...` 不会编译本文件(build tag 隔离)。 package repository import ( "context" "errors" "fmt" "testing" "fashionapi/internal/model" ) // mainDetailSeedSeq 让每次 seed 出来的实体键(city + year)互不相同。 // 正式表上有部分唯一索引 uq_ss_entity (city, year) WHERE is_deleted = 0, // 若测试沿用同一 city/year,第二条记录会被唯一索引拒绝。 var mainDetailSeedSeq int // seedMainDetailSnap 插入一条街拍正式记录(status=pending)并返回其 id。 // 命名加 MainDetail 前缀以避免与 publish_status_integration_test.go 的 seedStreetSnap 冲突。 func seedMainDetailSnap(t *testing.T, repo ReviewRepository) uint32 { t.Helper() ctx := context.Background() db := repo.(*reviewRepository).db mainDetailSeedSeq++ snap := model.StreetSnap{ Title: "主副图测试", Year: 2026, City: fmt.Sprintf("TestCity-%d", mainDetailSeedSeq), Status: model.StatusPending, } if err := db.WithContext(ctx).Create(&snap).Error; err != nil { t.Fatalf("插入街拍记录失败: %v", err) } t.Cleanup(func() { db.Exec("DELETE FROM street_snap_images WHERE snap_id = ?", snap.ID) db.Exec("DELETE FROM street_snaps WHERE id = ?", snap.ID) }) return snap.ID } // seedMainDetailImage 插入一张正式街拍图并返回其行 id。 func seedMainDetailImage(t *testing.T, repo ReviewRepository, snapID uint32, name string, order uint32) uint32 { t.Helper() img := model.StreetSnapImage{SnapID: snapID, Image: name + ".jpg", Name: name, SortOrder: order} if err := repo.(*reviewRepository).db.WithContext(context.Background()).Create(&img).Error; err != nil { t.Fatalf("插入街拍图失败: %v", err) } return img.ID } // reloadSnapImages 取记录的全部图(按 sort_order, id 升序),便于断言。 func reloadSnapImages(t *testing.T, repo ReviewRepository, snapID uint32) []model.StreetSnapImage { t.Helper() imgs, err := repo.StreetRecordImages(context.Background(), snapID) if err != nil { t.Fatalf("读街拍图失败: %v", err) } return imgs } // TestStreetMainDetail 覆盖批量并入 / 拆出 / 跨记录拦截三条主线。 func TestStreetMainDetail(t *testing.T) { repo := NewReviewRepository(testDB(t)) ctx := context.Background() snapID := seedMainDetailSnap(t, repo) m1 := seedMainDetailImage(t, repo, snapID, "a", 1) d2 := seedMainDetailImage(t, repo, snapID, "b", 2) d3 := seedMainDetailImage(t, repo, snapID, "c", 3) if err := repo.AttachStreetSnapImages(ctx, snapID, m1, []uint32{d2, d3}); err != nil { t.Fatalf("并入失败: %v", err) } for _, im := range reloadSnapImages(t, repo, snapID) { switch im.ID { case m1: if im.IsDetail != 0 || im.ParentImageID != 0 { t.Fatalf("主图应保持 is_detail=0/parent=0,实际 %d/%d", im.IsDetail, im.ParentImageID) } case d2, d3: if im.IsDetail != 1 || im.ParentImageID != m1 { t.Fatalf("副图应指向 %d,实际 is_detail=%d parent=%d", m1, im.IsDetail, im.ParentImageID) } } } // 幂等:重复并入同一批不报错,结果不变。 if err := repo.AttachStreetSnapImages(ctx, snapID, m1, []uint32{d2, d3}); err != nil { t.Fatalf("重复并入应幂等,实际报错: %v", err) } // 传入主图自身:调用方过滤掉的语义由仓储兜住(id <> main 条件),不报错也不改动主图。 if err := repo.AttachStreetSnapImages(ctx, snapID, m1, []uint32{m1}); err != nil { t.Fatalf("并入集合含主图自身应被忽略而非报错: %v", err) } for _, im := range reloadSnapImages(t, repo, snapID) { if im.ID == m1 && (im.IsDetail != 0 || im.ParentImageID != 0) { t.Fatalf("主图被自身并入后不应变化") } } // 拆出:把 d3 恢复为主图。 if err := repo.DetachStreetSnapImage(ctx, snapID, d3); err != nil { t.Fatalf("拆出失败: %v", err) } for _, im := range reloadSnapImages(t, repo, snapID) { if im.ID == d3 && (im.IsDetail != 0 || im.ParentImageID != 0) { t.Fatalf("拆出后应为 is_detail=0/parent=0,实际 %d/%d", im.IsDetail, im.ParentImageID) } } if err := repo.DetachStreetSnapImage(ctx, snapID, m1); err != nil { // 对主图幂等成功 t.Fatalf("对主图拆出应幂等成功: %v", err) } // 跨记录拦截:空集合 + 跨记录主图。 if err := repo.AttachStreetSnapImages(ctx, snapID, m1, nil); !errors.Is(err, ErrInvalidImageGroup) { t.Fatalf("空集合应返回 ErrInvalidImageGroup,实际 %v", err) } otherMain := seedMainDetailImage(t, repo, seedMainDetailSnap(t, repo), "x", 1) if err := repo.AttachStreetSnapImages(ctx, snapID, otherMain, []uint32{d2}); !errors.Is(err, ErrInvalidImageGroup) { t.Fatalf("跨记录主图应被拦截,实际 %v", err) } } // TestStreetMainDetailReparentGrandchildren 并入一张自带副图的主图时,其原副图应改挂到新主图。 func TestStreetMainDetailReparentGrandchildren(t *testing.T) { repo := NewReviewRepository(testDB(t)) ctx := context.Background() snapID := seedMainDetailSnap(t, repo) m1 := seedMainDetailImage(t, repo, snapID, "m1", 1) m2 := seedMainDetailImage(t, repo, snapID, "m2", 2) child := seedMainDetailImage(t, repo, snapID, "child", 3) if err := repo.AttachStreetSnapImages(ctx, snapID, m2, []uint32{child}); err != nil { t.Fatalf("预置副图失败: %v", err) } if err := repo.AttachStreetSnapImages(ctx, snapID, m1, []uint32{m2}); err != nil { t.Fatalf("并入整组失败: %v", err) } for _, im := range reloadSnapImages(t, repo, snapID) { if im.ID == m2 && (im.IsDetail != 1 || im.ParentImageID != m1) { t.Fatalf("m2 应成为 m1 的副图,实际 %d/%d", im.IsDetail, im.ParentImageID) } if im.ID == child && (im.IsDetail != 1 || im.ParentImageID != m1) { t.Fatalf("child 应改挂到 m1,实际 %d/%d", im.IsDetail, im.ParentImageID) } } }