us-coach-spider/database/model_test.go

51 lines
1.1 KiB
Go
Raw Normal View History

2024-05-14 15:27:40 +08:00
package database
import (
"gitea.timerzz.com/kedaya_haitao/common/pkg/database"
"gorm.io/gorm/clause"
"testing"
)
func TestUpsert(t *testing.T) {
db, err := database.InitDatabase(&database.DBOption{
Host: "192.168.31.55",
User: "timerzz",
Password: "zhhg1997",
Port: "5432",
DBName: "kedaya",
})
if err != nil {
t.Fatal(err)
}
err = db.AutoMigrate(&Product{}, &HistoryPrice{})
if err != nil {
t.Fatal(err)
}
db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "pid"}},
DoUpdates: clause.AssignmentColumns([]string{"name", "color", "link", "orderable", "us_price", "cny_price", "cal_mark", "freight", "rate"}),
}).Create([]Product{
{
Name: "test",
Pid: "test-1",
Color: "green",
Link: "www.baidu.com",
Image: "image",
Orderable: false,
USPrice: 123,
DWPrice: 3000,
Freight: 120,
},
{
Name: "test2",
Pid: "test-2",
Color: "green",
Link: "www.baidu.com",
Image: "image",
Orderable: false,
USPrice: 421,
DWPrice: 3000,
},
})
}