211 lines
7.4 KiB
Go
211 lines
7.4 KiB
Go
package service
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"cmautobuy/admin/model"
|
|
"cmautobuy/admin/repository"
|
|
)
|
|
|
|
const (
|
|
pddURLA = "https://mobile.yangkeduo.com/goods.html?goods_id=737116531267"
|
|
pddURLB = "https://mobile.yangkeduo.com/goods.html?goods_id=937122477375"
|
|
)
|
|
|
|
func TestAssociateShopeePdd_新建并重复复用(t *testing.T) {
|
|
db := newTestDB(t)
|
|
seedShopeeProduct(t, db, "1001", "商品一")
|
|
|
|
for i := 0; i < 2; i++ {
|
|
got, err := AssociateShopeePdd(db, "1001", pddURLA, false)
|
|
if err != nil {
|
|
t.Fatalf("第 %d 次关联失败: %v", i+1, err)
|
|
}
|
|
if got != "737116531267" {
|
|
t.Fatalf("goods_id = %q", got)
|
|
}
|
|
}
|
|
var count int
|
|
db.QueryRow(`SELECT COUNT(*) FROM pdd_products WHERE goods_id = ?`, "737116531267").Scan(&count)
|
|
if count != 1 {
|
|
t.Fatalf("同一 PDD 商品建了 %d 条,想要 1", count)
|
|
}
|
|
}
|
|
|
|
func TestAssociateShopeePdd_无效链接不写任何数据(t *testing.T) {
|
|
db := newTestDB(t)
|
|
seedShopeeProduct(t, db, "1001", "商品一")
|
|
if _, err := AssociateShopeePdd(db, "1001", "https://p.pinduoduo.com/short", false); err == nil {
|
|
t.Fatal("短链接应被拒绝")
|
|
}
|
|
var pddCount int
|
|
db.QueryRow(`SELECT COUNT(*) FROM pdd_products`).Scan(&pddCount)
|
|
product, _ := repository.GetShopeeProductByGoodsID(db, "1001")
|
|
if pddCount != 0 || product.PddGoodsID != "" {
|
|
t.Fatalf("失败后仍写入:pdd=%d, link=%q", pddCount, product.PddGoodsID)
|
|
}
|
|
}
|
|
|
|
func TestAssociateShopeePdd_换商品必须确认且旧档案保留(t *testing.T) {
|
|
db := newTestDB(t)
|
|
seedShopeeProduct(t, db, "1001", "商品一")
|
|
if _, err := AssociateShopeePdd(db, "1001", pddURLA, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := AssociateShopeePdd(db, "1001", pddURLB, false); !errors.Is(err, ErrPddAssociationReplaceRequired) {
|
|
t.Fatalf("未确认换品错误 = %v", err)
|
|
}
|
|
product, _ := repository.GetShopeeProductByGoodsID(db, "1001")
|
|
if product.PddGoodsID != "737116531267" {
|
|
t.Fatalf("未确认时关联被改成 %q", product.PddGoodsID)
|
|
}
|
|
if _, err := AssociateShopeePdd(db, "1001", pddURLB, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
product, _ = repository.GetShopeeProductByGoodsID(db, "1001")
|
|
if product.PddGoodsID != "937122477375" {
|
|
t.Fatalf("确认后关联 = %q", product.PddGoodsID)
|
|
}
|
|
old, _ := repository.GetPddProductByGoodsID(db, "737116531267")
|
|
if old == nil {
|
|
t.Fatal("换品不应删除旧 PDD 档案")
|
|
}
|
|
}
|
|
|
|
func TestAssociateShopeePdd_软删除商品可复活(t *testing.T) {
|
|
db := newTestDB(t)
|
|
seedShopeeProduct(t, db, "1001", "商品一")
|
|
repository.EnsurePddProduct(db, "737116531267", pddURLA)
|
|
if _, err := repository.SoftDeletePddProducts(db, []string{"737116531267"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := AssociateShopeePdd(db, "1001", pddURLA, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ := repository.GetPddProductByGoodsID(db, "737116531267")
|
|
if p == nil || p.IsDeleted() {
|
|
t.Fatal("关联应复活软删除的 PDD 商品")
|
|
}
|
|
}
|
|
|
|
func TestCreateShopeePddCollectTask_按当前关联创建(t *testing.T) {
|
|
db := newTestDB(t)
|
|
seedShopeeProduct(t, db, "1001", "商品一")
|
|
AssociateShopeePdd(db, "1001", pddURLA, false)
|
|
result, err := CreateShopeePddCollectTask(db, "1001")
|
|
if err != nil || result.Created != 1 {
|
|
t.Fatalf("创建结果 = %+v, err=%v", result, err)
|
|
}
|
|
_, _, _, ok := collectTaskOf(t, db, "737116531267")
|
|
if !ok {
|
|
t.Fatal("没有创建采集任务")
|
|
}
|
|
}
|
|
|
|
func TestCreateShopeePddCollectTaskForUser_记录当前用户(t *testing.T) {
|
|
db := newTestDB(t)
|
|
admin, _, _ := insertTaskUsers(t, db)
|
|
seedShopeeProduct(t, db, "1001", "商品一")
|
|
AssociateShopeePdd(db, "1001", pddURLA, false)
|
|
result, err := CreateShopeePddCollectTaskForUser(db, admin, "1001")
|
|
if err != nil || result.Created != 1 {
|
|
t.Fatalf("创建结果 = %+v, err=%v", result, err)
|
|
}
|
|
var creator string
|
|
if err := db.QueryRow(`SELECT created_by_user_id FROM tasks WHERE pdd_goods_id='737116531267'`).Scan(&creator); err != nil || creator != admin.UserID {
|
|
t.Fatalf("蝦皮入口任务创建人=%q err=%v", creator, err)
|
|
}
|
|
}
|
|
|
|
func TestCreateSybPddCollectTasksForUser_同一PDD商品去重(t *testing.T) {
|
|
db := newTestDB(t)
|
|
admin, _, _ := insertTaskUsers(t, db)
|
|
seedShopeeProduct(t, db, "1001", "商品一")
|
|
seedShopeeProduct(t, db, "1002", "商品二")
|
|
if _, err := AssociateShopeePdd(db, "1001", pddURLA, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := AssociateShopeePdd(db, "1002", pddURLA, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seedSybCollectOrder(t, db, "SYB-1", "1001")
|
|
seedSybCollectOrder(t, db, "SYB-2", "1002")
|
|
|
|
result, err := CreateSybPddCollectTasksForUser(
|
|
db, admin, []string{"SYB-1", "SYB-2", "SYB-1"}, "")
|
|
if err != nil || result.Created != 1 {
|
|
t.Fatalf("批量创建结果=%+v err=%v", result, err)
|
|
}
|
|
var taskCount int
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM tasks WHERE task_type='collect'`).Scan(&taskCount); err != nil || taskCount != 1 {
|
|
t.Fatalf("同一 PDD 商品应只建一条任务,count=%d err=%v", taskCount, err)
|
|
}
|
|
}
|
|
|
|
func TestCreateSybPddCollectTasksForUser_不可采集明细整批拒绝(t *testing.T) {
|
|
db := newTestDB(t)
|
|
admin, _, _ := insertTaskUsers(t, db)
|
|
seedShopeeProduct(t, db, "1001", "商品一")
|
|
seedShopeeProduct(t, db, "1002", "商品二")
|
|
AssociateShopeePdd(db, "1001", pddURLA, false)
|
|
AssociateShopeePdd(db, "1002", pddURLB, false)
|
|
seedSybCollectOrder(t, db, "SYB-PENDING", "1001")
|
|
seedSybCollectOrder(t, db, "SYB-COLLECTED", "1002")
|
|
if _, err := db.Exec(`UPDATE pdd_products SET collect_status='collected' WHERE goods_id='937122477375'`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err := CreateSybPddCollectTasksForUser(
|
|
db, admin, []string{"SYB-PENDING", "SYB-COLLECTED"}, "")
|
|
if !IsValidationError(err) {
|
|
t.Fatalf("不可采集明细应返回校验错误,实际 %v", err)
|
|
}
|
|
var taskCount int
|
|
db.QueryRow(`SELECT COUNT(*) FROM tasks WHERE task_type='collect'`).Scan(&taskCount)
|
|
if taskCount != 0 {
|
|
t.Fatalf("整批拒绝后不应产生部分任务,实际 %d", taskCount)
|
|
}
|
|
pending, _ := repository.GetPddProductByGoodsID(db, "737116531267")
|
|
if pending.CollectStatus != model.CollectPending {
|
|
t.Fatalf("整批拒绝后待采集商品状态被改变为 %s", pending.CollectStatus)
|
|
}
|
|
}
|
|
|
|
func TestCreateSybPddCollectTasksForUser_拒绝不可见客户端(t *testing.T) {
|
|
db := newTestDB(t)
|
|
admin, buyerA, buyerB := prepareClientAssignmentUsers(t, db)
|
|
if err := RegisterClient(db, model.Client{ClientID: "client-b", Name: "B 的机器"}, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := AssignClient(db, admin, "client-b", buyerB.UserID,
|
|
time.Date(2026, 8, 11, 1, 0, 0, 0, time.UTC)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seedShopeeProduct(t, db, "1001", "商品一")
|
|
AssociateShopeePdd(db, "1001", pddURLA, false)
|
|
seedSybCollectOrder(t, db, "SYB-1", "1001")
|
|
|
|
_, err := CreateSybPddCollectTasksForUser(db, buyerA, []string{"SYB-1"}, "client-b")
|
|
if !IsValidationError(err) {
|
|
t.Fatalf("不可见客户端应返回校验错误,实际 %v", err)
|
|
}
|
|
var taskCount int
|
|
db.QueryRow(`SELECT COUNT(*) FROM tasks WHERE task_type='collect'`).Scan(&taskCount)
|
|
if taskCount != 0 {
|
|
t.Fatalf("客户端权限校验失败后不应建任务,实际 %d", taskCount)
|
|
}
|
|
}
|
|
|
|
func seedSybCollectOrder(t *testing.T, db *sql.DB, sybID, shopeeGoodsID string) {
|
|
t.Helper()
|
|
if _, err := repository.UpsertSybOrder(db, model.SybOrder{
|
|
SybID: sybID, OrderNo: "ORDER-" + sybID, Title: "测试商品", ProductSpec: "黑色,M",
|
|
ShopeeGoodsID: shopeeGoodsID, Quantity: 1, SybData: `{}`,
|
|
}); err != nil {
|
|
t.Fatalf("插入顺运宝测试明细失败: %v", err)
|
|
}
|
|
}
|