本文整理匯總了Golang中github.com/coocood/assrt.Assert.MustNil方法的典型用法代碼示例。如果您正苦於以下問題:Golang Assert.MustNil方法的具體用法?Golang Assert.MustNil怎麽用?Golang Assert.MustNil使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/coocood/assrt.Assert
的用法示例。
在下文中一共展示了Assert.MustNil方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: setupDb
func setupDb(assert *assrt.Assert, info dialectInfo) (*Migration, *Qbs) {
db1, err := info.openDbFunc()
assert.MustNil(err)
mg := NewMigration(db1, dbName, info.dialect)
db2, err := info.openDbFunc()
assert.MustNil(err)
q := New(db2, info.dialect)
q.Log = true
return mg, q
}
示例2: DoTestUpdate
func DoTestUpdate(assert *assrt.Assert, info dialectInfo) {
mg, q := setupDb(assert, info)
mg.dropTableIfExists(&basic{})
mg.CreateTableIfNotExists(&basic{})
_, err := q.Save(&basic{Name: "a", State: 1})
_, err = q.Save(&basic{Name: "b", State: 1})
_, err = q.Save(&basic{Name: "c", State: 0})
assert.MustNil(err)
{
// define a temporary struct in a block to update partial columns of a table
// as the type is in a block, so it will not conflict with other types with the same name in the same method
type basic struct {
Name string
}
affected, err := q.Where("state = ?", 1).Update(&basic{Name: "d"})
assert.MustNil(err)
assert.Equal(2, affected)
var datas []*basic
q.Where("state = ?", 1).FindAll(&datas)
assert.MustEqual(2, len(datas))
assert.Equal("d", datas[0].Name)
assert.Equal("d", datas[1].Name)
}
// if choose basic table type to update, all zero value in the struct will be updated too.
// this may be cause problems, so define a temporary struct to update table is the recommended way.
affected, err := q.Where("state = ?", 1).Update(&basic{Name: "e"})
assert.MustNil(err)
assert.Equal(2, affected)
var datas []*basic
q.Where("state = ?", 1).FindAll(&datas)
assert.MustEqual(0, len(datas))
}
示例3: DoTestForeignKey
func DoTestForeignKey(assert *assrt.Assert, info dialectInfo) {
mg, q := setupDb(assert, info)
type user struct {
Id Id
Name string
}
type post struct {
Id Id
Title string
AuthorId int64
Author *user
}
aUser := &user{
Name: "john",
}
aPost := &post{
Title: "A Title",
}
mg.dropTableIfExists(aPost)
mg.dropTableIfExists(aUser)
mg.CreateTableIfNotExists(aUser)
mg.CreateTableIfNotExists(aPost)
uid, err := q.Save(aUser)
assert.Nil(err)
aPost.AuthorId = int64(uid)
affected, err := q.Save(aPost)
assert.Equal(1, affected)
pst := new(post)
pst.Id = aPost.Id
err = q.Find(pst)
assert.MustNil(err)
assert.MustNotNil(pst)
assert.Equal(uid, pst.Id)
assert.Equal("john", pst.Author.Name)
}
示例4: DoTestSaveAndDelete
func DoTestSaveAndDelete(assert *assrt.Assert, info dialectInfo) {
x := time.Now()
assert.MustZero(x.Sub(x.UTC()))
now := time.Now()
mg, q := setupDb(assert, info)
type saveModel struct {
Id Id
A string
B int
Updated time.Time
Created time.Time
}
model1 := saveModel{
A: "banana",
B: 5,
}
model2 := saveModel{
A: "orange",
B: 4,
}
mg.dropTableIfExists(&model1)
mg.CreateTableIfNotExists(&model1)
affected, err := q.Save(&model1)
assert.MustNil(err)
assert.Equal(1, affected)
assert.True(model1.Created.Sub(now) > 0)
assert.True(model1.Updated.Sub(now) > 0)
// make sure created/updated values match the db
var model1r []*saveModel
err = q.Where("id = ?", model1.Id).FindAll(&model1r)
assert.MustNil(err)
assert.MustOneLen(model1r)
assert.Equal(model1.Created.Unix(), model1r[0].Created.Unix())
assert.Equal(model1.Updated.Unix(), model1r[0].Updated.Unix())
oldCreate := model1.Created
oldUpdate := model1.Updated
model1.A = "grape"
model1.B = 9
time.Sleep(time.Second * 1) // sleep for 1 sec
affected, err = q.Save(&model1)
assert.MustNil(err)
assert.MustEqual(1, affected)
assert.True(model1.Created.Equal(oldCreate))
assert.True(model1.Updated.Sub(oldUpdate) > 0)
// make sure created/updated values match the db
var model1r2 []*saveModel
err = q.Where("id = ?", model1.Id).FindAll(&model1r2)
assert.MustNil(err)
assert.MustOneLen(model1r2)
assert.True(model1r2[0].Updated.Sub(model1r2[0].Created) >= 1)
assert.Equal(model1.Created.Unix(), model1r2[0].Created.Unix())
assert.Equal(model1.Updated.Unix(), model1r2[0].Updated.Unix())
affected, err = q.Save(&model2)
assert.MustNil(err)
assert.Equal(1, affected)
affected, err = q.Delete(&model2)
assert.MustNil(err)
assert.Equal(1, affected)
}
示例5: DoTestForeignKey
func DoTestForeignKey(assert *assrt.Assert, info dialectInfo) {
mg, q := setupDb(assert, info)
defer mg.Close()
defer q.Close()
type user struct {
Id int64
Name string
}
type post struct {
Id int64
Title string
AuthorId int64
Author *user
}
aUser := &user{
Name: "john",
}
aPost := &post{
Title: "A Title",
}
mg.dropTableIfExists(aPost)
mg.dropTableIfExists(aUser)
mg.CreateTableIfNotExists(aUser)
mg.CreateTableIfNotExists(aPost)
affected, err := q.Save(aUser)
assert.Nil(err)
aPost.AuthorId = int64(aUser.Id)
affected, err = q.Save(aPost)
assert.Equal(1, affected)
pst := new(post)
pst.Id = aPost.Id
err = q.Find(pst)
assert.MustNil(err)
assert.Equal(aPost.Id, pst.Id)
assert.Equal("john", pst.Author.Name)
pst.Author = nil
err = q.OmitFields("Author").Find(pst)
assert.MustNil(err)
assert.MustNil(pst.Author)
err = q.OmitJoin().Find(pst)
assert.MustNil(err)
assert.MustNil(pst.Author)
var psts []*post
err = q.FindAll(&psts)
assert.MustNil(err)
assert.OneLen(psts)
assert.Equal("john", psts[0].Author.Name)
}