package productv1 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_dev", }) 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", "original_price", "cny_price", "cal_mark", "rate"}), }).Create([]Product{ { Name: "test", Pid: "test-3", Color: "green", Link: "www.baidu.com", Image: "image", Orderable: false, OriginalPrice: 123, DWPrice: 3000, Freight: 130, }, }) } func TestUpdate(t *testing.T) { db, err := database.InitDatabase(&database.DBOption{ Host: "192.168.31.55", User: "timerzz", Password: "zhhg1997", Port: "5432", DBName: "kedaya_dev", }) if err != nil { t.Fatal(err) } var p = Product{Pid: "CJ575-QBO4G", DWPrice: 3000, Freight: 150, Remark: "test", OriginalPrice: 209.3, ExchangeRate: 7.3} err = db.Model(&p).Where("pid = ?", p.Pid).Select("dw_price", "freight", "remark", "rate", "cal_mark", "cny_price").Updates(&p).Error if err != nil { t.Fatal(err) } } func TestGetConfig(t *testing.T) { db, err := database.InitDatabase(&database.DBOption{ Host: "192.168.31.55", User: "timerzz", Password: "zhhg1997", Port: "5432", DBName: "kedaya_dev", }) if err != nil { t.Fatal(err) } var exchangeRate float64 db.Table("options").Select("exchange_rate").Where("id = ?", 1).Scan(&exchangeRate) t.Log(exchangeRate) } func TestCalculateProcess_Calculate(t *testing.T) { p := Product{ OriginalPrice: 160.9, Freight: 100, ExchangeRate: 7.3, PriceStatus: 0, } cal := CalculateProcess{ Condition: "usPrice >= 150", Process: "(usPrice - 20) * exchangeRate + freight", Name: "满150 - 20", } t.Log(cal.Calculate(p.Env())) }