mdbc/mdbc_test.go

71 lines
1.7 KiB
Go
Raw Normal View History

2023-06-23 15:56:52 +00:00
package mdbc_test
2023-06-22 15:14:51 +00:00
import (
"context"
"github.com/stretchr/testify/assert"
2023-06-23 15:56:52 +00:00
"gitter.top/coco/mdbc"
2023-06-22 15:14:51 +00:00
"go.mongodb.org/mongo-driver/bson"
2023-06-23 15:56:52 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2023-06-22 15:14:51 +00:00
"testing"
)
2023-06-23 15:56:52 +00:00
func newDriver() mdbc.Collection {
return mdbc.NewMDBC(&mdbc.Config{
2023-06-22 15:14:51 +00:00
Address: "192.168.3.21",
Port: 0,
Username: "admin",
Password: "admin",
ProcessTimeout: 0,
ReadPref: 0,
DbName: "articles",
2023-06-23 15:56:52 +00:00
}).BindModel(&mdbc.ModelArticles{})
2023-06-22 15:14:51 +00:00
}
2023-06-23 15:56:52 +00:00
func TestScope_Insert(t *testing.T) {
2023-06-22 15:14:51 +00:00
driver := newDriver()
2023-06-23 15:56:52 +00:00
res, err := driver.Insert().SetContext(context.Background()).Insert(&mdbc.ModelArticles{
Id: "hello---------",
Title: "你好,世界",
AvatarUrl: "",
Phone: "",
CreateTime: 0,
UpdateTime: 0,
})
2023-06-22 15:14:51 +00:00
assert.NoError(t, err)
2023-06-23 15:56:52 +00:00
t.Log(res.GetID())
2023-06-22 15:14:51 +00:00
}
2023-06-23 15:56:52 +00:00
func TestScope_Delete(t *testing.T) {
2023-06-22 15:14:51 +00:00
driver := newDriver()
2023-06-23 15:56:52 +00:00
id, err := primitive.ObjectIDFromHex("6495ba512ff0d373f4691e51")
if err != nil {
panic("id invalid: " + err.Error())
}
res, err := driver.Delete().SetID(id).SetContext(context.Background()).One()
2023-06-22 15:14:51 +00:00
assert.NoError(t, err)
2023-06-23 15:56:52 +00:00
t.Log(res)
2023-06-22 15:14:51 +00:00
}
2023-06-23 15:56:52 +00:00
func TestScope_Update(t *testing.T) {
2023-06-22 15:14:51 +00:00
driver := newDriver()
2023-06-23 15:56:52 +00:00
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",
})
2023-06-22 15:14:51 +00:00
assert.NoError(t, err)
2023-06-23 15:56:52 +00:00
t.Log(res)
2023-06-22 15:14:51 +00:00
}
2023-06-23 15:56:52 +00:00
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)
2023-06-22 15:14:51 +00:00
}