common/structs/storage/article_test.go
2024-08-04 20:46:49 +08:00

64 lines
1.4 KiB
Go

package storage
import (
"fmt"
"testing"
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func GetDevDB() (*gorm.DB, error) {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", "192.168.31.55", "timerzz", "zhhg1997", "kedaya_dev", "5432")
return gorm.Open(postgres.Open(dsn), &gorm.Config{})
}
var article = v2.Article{
Name: "test1",
EnglishName: "test1",
Pid: "111",
Brand: "col",
Desc: "desc",
Image: "http://123",
Available: false,
CostPrice: 123,
Providers: []v2.ProviderArticle{
{
Pid: "111",
Brand: "col1",
Link: "http://123",
ProviderId: "coach2",
SkuID: "sku1",
Cost: v2.ProviderPrice{
OriginalPrice: 111,
FinalPrice: 111,
CalMark: "uuuuu",
},
HistoryPrice: []v2.ProviderPrice{
{
OriginalPrice: 111,
FinalPrice: 111,
CalMark: "uuuuu",
},
},
},
},
SellPrice: 111,
Sellers: []v2.SellerArticle{},
Rate: 111,
Remark: "111",
}
func TestUpsetArticle(t *testing.T) {
db, _ := GetDevDB()
storage := NewStorage(db)
err := db.AutoMigrate(&v2.Article{}, &v2.ProviderArticle{}, &v2.ProviderPrice{}, &v2.SellerArticle{}, &v2.SellerPrice{})
if err != nil {
t.Fatal(err)
}
if err = storage.Article().Upsert(article); err != nil {
t.Fatal(err)
}
}