mdbc/mdbc_test.go

71 lines
1.7 KiB
Go

package mdbc_test
import (
"context"
"github.com/stretchr/testify/assert"
"gitter.top/coco/mdbc"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"testing"
)
func newDriver() mdbc.Collection {
return mdbc.NewMDBC(&mdbc.Config{
Address: "192.168.3.21",
Port: 0,
Username: "admin",
Password: "admin",
ProcessTimeout: 0,
ReadPref: 0,
DbName: "articles",
}).BindModel(&mdbc.ModelArticles{})
}
func TestScope_Insert(t *testing.T) {
driver := newDriver()
res, err := driver.Insert().SetContext(context.Background()).Insert(&mdbc.ModelArticles{
Id: "hello---------",
Title: "你好,世界",
AvatarUrl: "",
Phone: "",
CreateTime: 0,
UpdateTime: 0,
})
assert.NoError(t, err)
t.Log(res.GetID())
}
func TestScope_Delete(t *testing.T) {
driver := newDriver()
id, err := primitive.ObjectIDFromHex("6495ba512ff0d373f4691e51")
if err != nil {
panic("id invalid: " + err.Error())
}
res, err := driver.Delete().SetID(id).SetContext(context.Background()).One()
assert.NoError(t, err)
t.Log(res)
}
func TestScope_Update(t *testing.T) {
driver := newDriver()
id, err := primitive.ObjectIDFromHex("6495b9ac325292261591a780")
if err != nil {
panic("id invalid: " + err.Error())
}
res, err := driver.Update().SetID(id).SetContext(context.Background()).One(bson.M{
"phone": "12345",
})
assert.NoError(t, err)
t.Log(res)
}
func TestFindScope_GetList(t *testing.T) {
driver := newDriver()
var list []mdbc.ModelArticles
err := driver.Find().SetFilter(bson.M{
"title": "你好,世界",
}).SetContext(context.Background()).GetList(&list)
assert.NoError(t, err)
t.Log(list)
}