sdbc/update_scope_test.go

75 lines
1.8 KiB
Go
Raw Normal View History

2023-09-16 10:09:34 +00:00
package sdbc
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func Test__update_Replace(t *testing.T) {
var driver = NewSDBC(&Config{
Dbname: "test.db",
MaxIdleConn: 10,
MaxOpenConn: 100,
MaxLifetime: time.Hour,
Debug: true,
}).BindModel(&ModelArticles{})
var doc = &ModelArticles{
Id: 2,
Title: "hello world1",
AvatarUrl: "https://github.com/xuthus5/profile",
Phone: "10086",
CreateTime: time.Now().Unix() - 10000000,
UpdateTime: time.Now().Unix(),
}
err := driver.Update().Replace(doc)
assert.NoError(t, err)
t.Logf("doc id: %v", doc.Id)
}
func Test__update_Update(t *testing.T) {
var driver = NewSDBC(&Config{
Dbname: "test.db",
MaxIdleConn: 10,
MaxOpenConn: 100,
MaxLifetime: time.Hour,
Debug: true,
}).BindModel(&ModelArticles{})
var doc = &ModelArticles{
Id: 2,
Title: "hello world1",
AvatarUrl: "https://github.com/xuthus5/profile",
Phone: "10086",
CreateTime: time.Now().Unix() - 10000000,
UpdateTime: time.Now().Unix(),
}
err := driver.Update().SetWhere("id = ?", doc.Id).Update("phone", "10010")
assert.NoError(t, err)
t.Logf("doc id: %v", doc.Id)
}
func Test__update_Updates(t *testing.T) {
var driver = NewSDBC(&Config{
Dbname: "test.db",
MaxIdleConn: 10,
MaxOpenConn: 100,
MaxLifetime: time.Hour,
Debug: true,
}).BindModel(&ModelArticles{})
var doc = &ModelArticles{
Id: 2,
Title: "hello world1",
AvatarUrl: "https://github.com/xuthus5/profile",
Phone: "10086",
CreateTime: time.Now().Unix() - 10000000,
UpdateTime: time.Now().Unix(),
}
err := driver.Update().SetWhere("update_time = ?", 0).SetOmit("create_time", "update_time").
Updates(map[string]interface{}{
"title": "hello",
"phone": "110",
})
assert.NoError(t, err)
t.Logf("doc id: %v", doc.Id)
}