common/structs/storage/provider-article_test.go

106 lines
2.5 KiB
Go
Raw Normal View History

2024-08-27 19:35:21 +08:00
package storage
import (
2024-08-31 18:27:17 +08:00
"fmt"
2024-08-27 19:35:21 +08:00
"testing"
2024-12-06 17:07:14 +08:00
"time"
2024-08-27 19:35:21 +08:00
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
2024-08-31 18:10:06 +08:00
"gorm.io/gorm"
2024-08-27 19:35:21 +08:00
)
var providerArticle = v2.ProviderArticle{
ArticleID: 1,
Brand: v2.Brand_Coach,
Cost: v2.ProviderPrice{
CalMark: "111",
FinalPrice: 1,
OriginalPrice: 0,
ProviderArticleID: 0,
},
HistoryPrice: []v2.ProviderPrice{
{
CalMark: "222",
},
},
}
func TestProviderArticleApi_Upsert(t *testing.T) {
db, err := GetDevDB()
if err != nil {
t.Fatal(err)
}
storage := NewStorage(db)
storage.ProviderArticle().Upsert(providerArticle)
}
2024-08-31 18:10:06 +08:00
func TestProviderArticleApi_FindInBatches(t *testing.T) {
db, err := GetDevDB()
if err != nil {
t.Fatal(err)
}
storage := NewStorage(db)
var results = make([]v2.ProviderArticle, 0)
2024-08-31 18:27:17 +08:00
err = storage.ProviderArticle().FindInBatches(&GetProviderArticleQuery{ProviderId: "1"}, &results, func(tx *gorm.DB, batch int) error {
for idx := range results {
results[idx].Link = fmt.Sprintf("test_%d", idx)
results[idx].SkuID = fmt.Sprintf("sku_%d", idx)
}
return db.Select("id", "link").Save(&results).Error
2024-08-31 18:10:06 +08:00
})
2024-08-31 18:27:17 +08:00
if err != nil {
t.Fatal(err)
}
2024-08-31 18:10:06 +08:00
}
func TestFullImage(t *testing.T) {
db, err := GetDevDB()
if err != nil {
t.Fatal(err)
}
var articles []*v2.ProviderArticle
articles, err = NewStorage(db).ProviderArticle().Find(
NewGetProviderArticleQuery().
SetProviderId("us-coach-outlet").
SetWatch(true),
)
fmt.Println(articles)
//var pids []string
//db.Table("watch_info").Select("pid").Not("pid", nil).Not("pid", "").Where("has_detail=?", true).Where("deleted_at", nil).Find(&pids)
//nots := []string{}
//for _, pid := range pids {
// var p v2.ProviderArticle
// err = db.Where("pid=?", pid).First(&p).Error
// if errors.Is(err, gorm.ErrRecordNotFound) {
// nots = append(nots, pid)
// } else if err != nil {
// fmt.Printf("%s 查询失败: %v", pid, err)
// } else {
// p.SetWatch(true)
// p.Available = false
// err = db.Select("watch", "available").Updates(&p).Error
// if err != nil {
// fmt.Printf("%s 更新失败: %v", pid, err)
// }
// }
//}
//fmt.Printf("以下共%d个pid未找到: %s", len(nots), nots)
}
2024-12-06 17:07:14 +08:00
func TestProviderArticleApi_FindByPid(t *testing.T) {
db, err := GetDevDB()
if err != nil {
t.Fatal(err)
}
var a v2.ProviderArticle
now := time.Now()
n := now.String()
fmt.Println(n)
tt := time.Now().Add(-30 * time.Minute)
s := tt.String()
fmt.Println(s)
db.Preload("HistoryAts", "created_at > ?", tt).First(&a, "pid=?", "CX607-IMXB8")
fmt.Println(a)
}