feat: test for mdbc

This commit is contained in:
xuthus5 2023-06-23 23:56:52 +08:00
parent bf11ba6f68
commit b3bbe08fde
Signed by: xuthus5
GPG Key ID: A23CF9620CBB55F9
9 changed files with 98 additions and 69 deletions

View File

@ -8,8 +8,8 @@ import (
)
type countScope struct {
*scope
*mdbc
scope *scope
mdbc *mdbc
ctx context.Context
limit int64
skip int64
@ -67,5 +67,5 @@ func (cs *countScope) mergeOptions() {
func (cs *countScope) Count() (int64, error) {
cs.mergeOptions()
return cs.database.Collection(cs.tableName).CountDocuments(cs.ctx, cs.filter, &cs.opts)
return cs.mdbc.database.Collection(cs.scope.tableName).CountDocuments(cs.ctx, cs.filter, &cs.opts)
}

View File

@ -8,8 +8,8 @@ import (
)
type deleteScope struct {
*scope
*mdbc
scope *scope
mdbc *mdbc
ctx context.Context
opts *options.DeleteOptions
filter interface{}
@ -27,13 +27,13 @@ func (ds *deleteScope) SetOptions(opts options.DeleteOptions) *deleteScope {
}
// SetID delete documents based on ID, has the highest priority.
func (ds *deleteScope) SetID(id ...string) *deleteScope {
func (ds *deleteScope) SetID(id ...any) *deleteScope {
if len(id) == 0 {
return ds
}
if len(id) == 1 {
ds.filter = bson.M{
"_id": id,
"_id": id[0],
}
}
if len(id) > 1 {
@ -85,7 +85,7 @@ func (ds *deleteScope) One() (int64, error) {
if ds.err != nil {
return 0, ds.err
}
result, err := ds.database.Collection(ds.tableName).DeleteOne(ds.ctx, ds.filter, ds.opts)
result, err := ds.mdbc.database.Collection(ds.scope.tableName).DeleteOne(ds.ctx, ds.filter, ds.opts)
if err != nil {
return 0, err
}
@ -97,7 +97,7 @@ func (ds *deleteScope) Many() (int64, error) {
if ds.err != nil {
return 0, ds.err
}
result, err := ds.database.Collection(ds.tableName).DeleteMany(ds.ctx, ds.filter, ds.opts)
result, err := ds.mdbc.database.Collection(ds.scope.tableName).DeleteMany(ds.ctx, ds.filter, ds.opts)
if err != nil {
return 0, err
}

View File

@ -3,9 +3,9 @@ package mdbc
import "context"
type dropScope struct {
*scope
*mdbc
ctx context.Context
scope *scope
mdbc *mdbc
ctx context.Context
}
func (ds *dropScope) SetContext(ctx context.Context) *dropScope {
@ -21,5 +21,5 @@ func (ds *dropScope) mergeOptions() {
func (ds *dropScope) Do() error {
ds.mergeOptions()
return ds.database.Collection(ds.tableName).Drop(ds.ctx)
return ds.mdbc.database.Collection(ds.scope.tableName).Drop(ds.ctx)
}

29
find.go
View File

@ -9,8 +9,8 @@ import (
)
type findScope struct {
*scope
*mdbc
scope *scope
mdbc *mdbc
ctx context.Context
opts any
limit *int64
@ -27,6 +27,15 @@ func (fs *findScope) SetContext(ctx context.Context) *findScope {
return fs
}
func (fs *findScope) SetFilter(filter any) *findScope {
if filter == nil {
fs.filter = bson.M{}
return fs
}
fs.filter = filter
return fs
}
func (fs *findScope) SetFindOptions(opts options.FindOptions) *findScope {
fs.opts = &opts
return fs
@ -114,7 +123,7 @@ func (fs *findScope) GetList(list any) error {
return fmt.Errorf("invalid list type, must be *[]*struct")
}
fs.findMergeOptions()
cursor, err := fs.database.Collection(fs.tableName).Find(fs.ctx, fs.filter, fs.opts.(*options.FindOptions))
cursor, err := fs.mdbc.database.Collection(fs.scope.tableName).Find(fs.ctx, fs.filter, fs.opts.(*options.FindOptions))
if err != nil {
return err
}
@ -131,11 +140,11 @@ func (fs *findScope) GetMap(m any, field string) error {
return fmt.Errorf("invalid m type, must be based on map")
}
fs.findMergeOptions()
cursor, err := fs.database.Collection(fs.tableName).Find(fs.ctx, fs.filter, fs.opts.(*options.FindOptions))
cursor, err := fs.mdbc.database.Collection(fs.scope.tableName).Find(fs.ctx, fs.filter, fs.opts.(*options.FindOptions))
if err != nil {
return err
}
list := goref.BuildSlice(fs.modelKind)
list := goref.BuildSlice(fs.scope.modelKind)
err = cursor.All(fs.ctx, list)
if err != nil {
return err
@ -174,7 +183,7 @@ func (fs *findScope) FindOne(doc any) error {
return fmt.Errorf("invalid doc type, must be *struct")
}
fs.findOneMergeOptions()
result := fs.database.Collection(fs.tableName).FindOne(fs.ctx, fs.filter, fs.opts.(*options.FindOneOptions))
result := fs.mdbc.database.Collection(fs.scope.tableName).FindOne(fs.ctx, fs.filter, fs.opts.(*options.FindOneOptions))
if result.Err() != nil {
return result.Err()
}
@ -206,7 +215,7 @@ func (fs *findScope) findOneAndDeleteMergeOptions() {
func (fs *findScope) FindOneAndDelete() error {
fs.findOneAndDeleteMergeOptions()
result := fs.database.Collection(fs.tableName).FindOneAndDelete(fs.ctx, fs.filter, fs.opts.(*options.FindOneAndDeleteOptions))
result := fs.mdbc.database.Collection(fs.scope.tableName).FindOneAndDelete(fs.ctx, fs.filter, fs.opts.(*options.FindOneAndDeleteOptions))
if result.Err() != nil {
return result.Err()
}
@ -218,7 +227,7 @@ func (fs *findScope) FindOneAndDeleteWithValue(doc any) error {
return fmt.Errorf("invalid doc type, must be *struct")
}
fs.findOneAndDeleteMergeOptions()
result := fs.database.Collection(fs.tableName).FindOneAndDelete(fs.ctx, fs.filter, fs.opts.(*options.FindOneAndDeleteOptions))
result := fs.mdbc.database.Collection(fs.scope.tableName).FindOneAndDelete(fs.ctx, fs.filter, fs.opts.(*options.FindOneAndDeleteOptions))
if result.Err() != nil {
return result.Err()
}
@ -261,7 +270,7 @@ func (fs *findScope) findOneAndUpdateMergeOptions() {
func (fs *findScope) FindOneAndUpdate(doc any) error {
fs.doc = doc
fs.findOneAndUpdateMergeOptions()
result := fs.database.Collection(fs.tableName).FindOneAndUpdate(fs.ctx, fs.filter, fs.doc, fs.opts.(*options.FindOneAndUpdateOptions))
result := fs.mdbc.database.Collection(fs.scope.tableName).FindOneAndUpdate(fs.ctx, fs.filter, fs.doc, fs.opts.(*options.FindOneAndUpdateOptions))
if result.Err() != nil {
return result.Err()
}
@ -301,7 +310,7 @@ func (fs *findScope) findOneAndReplaceMergeOptions() {
func (fs *findScope) FindOneAndReplace(doc any) error {
fs.doc = doc
fs.findOneAndReplaceMergeOptions()
result := fs.database.Collection(fs.tableName).FindOneAndReplace(fs.ctx, fs.filter, fs.doc, fs.opts.(*options.FindOneAndReplaceOptions))
result := fs.mdbc.database.Collection(fs.scope.tableName).FindOneAndReplace(fs.ctx, fs.filter, fs.doc, fs.opts.(*options.FindOneAndReplaceOptions))
if result.Err() != nil {
return result.Err()
}

10
go.sum
View File

@ -25,11 +25,9 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
@ -45,14 +43,6 @@ github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
gitter.top/coco/lormatter v0.0.0-20230409145644-f9cb43f740dc h1:B0bnfX8Y/dETV+zdcZNCwD/L+DLZN2wSf9szo/fxNCw=
gitter.top/coco/lormatter v0.0.0-20230409145644-f9cb43f740dc/go.mod h1:vyfU6MQ56tkoFKSzEzhYSwXjgZHMcu3AViHC3hIyO7I=
gitter.top/common/goref v0.0.0-20230622151024-9b220e13c7cd h1:EeOtSwGexcx0u1AE91wnemiOGedENxaKcJUXxpzcgiE=
gitter.top/common/goref v0.0.0-20230622151024-9b220e13c7cd/go.mod h1:oK6jZQ/ISS8gZ78rvww6p7FuLUzaJ+S5F5UXSqO7Lr0=
gitter.top/common/goref v0.0.0-20230623104541-a0005020066e h1:y56ZGlp2Pz+pia3gEeRAgrRoCOoiecxg0RtoaCEj7bY=
gitter.top/common/goref v0.0.0-20230623104541-a0005020066e/go.mod h1:oK6jZQ/ISS8gZ78rvww6p7FuLUzaJ+S5F5UXSqO7Lr0=
gitter.top/common/goref v0.0.0-20230623122346-fa52c8a5753e h1:er7LiuFgv7+vFGdJosiHLWgWdjpOQxoL+qi5roxYyTk=
gitter.top/common/goref v0.0.0-20230623122346-fa52c8a5753e/go.mod h1:oK6jZQ/ISS8gZ78rvww6p7FuLUzaJ+S5F5UXSqO7Lr0=
gitter.top/common/goref v0.0.0-20230623134616-9d375bad30e4 h1:yivG8g8Hty/rMiPLWhGDPmRJQXvcZwBtLvmminEEAd0=
gitter.top/common/goref v0.0.0-20230623134616-9d375bad30e4/go.mod h1:oK6jZQ/ISS8gZ78rvww6p7FuLUzaJ+S5F5UXSqO7Lr0=
gitter.top/common/goref v0.0.0-20230623144827-8cd99d7ed68f h1:+b+DwvEDKS6hgtJ49H+F0m310NO0zMVGn2kfXh2UWwA=
gitter.top/common/goref v0.0.0-20230623144827-8cd99d7ed68f/go.mod h1:9ZCvSyMgyJ6ODKdgvHgnNuRlBhvlzIOBcwhP3Buz5SA=
go.mongodb.org/mongo-driver v1.11.7 h1:LIwYxASDLGUg/8wOhgOOZhX8tQa/9tgZPgzZoVqJvcs=

View File

@ -3,9 +3,9 @@ package mdbc
import "context"
type indexesScope struct {
*scope
*mdbc
ctx context.Context
scope *scope
mdbc *mdbc
ctx context.Context
}
func (is *indexesScope) SetContext(ctx context.Context) *indexesScope {

View File

@ -3,15 +3,16 @@ package mdbc
import (
"context"
"gitter.top/common/goref"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
type insertScope struct {
*scope
*mdbc
ctx context.Context
opts any
docs any
scope *scope
mdbc *mdbc
ctx context.Context
opts any
docs any
}
type insert interface {
@ -23,7 +24,7 @@ type insertOne struct {
}
func (io *insertOne) insert(docs any) (*InsertResult, error) {
result, err := io.database.Collection(io.tableName).InsertOne(io.ctx, docs, io.opts.(*options.InsertOneOptions))
result, err := io.mdbc.database.Collection(io.scope.tableName).InsertOne(io.ctx, docs, io.opts.(*options.InsertOneOptions))
if err != nil {
return nil, err
}
@ -39,7 +40,7 @@ func (im *insertMany) insert(docs any) (*InsertResult, error) {
if !ok {
return nil, ErrorNoInsertDocuments
}
result, err := im.database.Collection(im.tableName).InsertMany(im.ctx, vals, im.opts.(*options.InsertManyOptions))
result, err := im.mdbc.database.Collection(im.scope.tableName).InsertMany(im.ctx, vals, im.opts.(*options.InsertManyOptions))
if err != nil {
return nil, err
}
@ -51,8 +52,13 @@ type InsertResult struct {
}
func (ir *InsertResult) GetID() string {
objectID, ok := ir.id.(primitive.ObjectID)
if ok {
return objectID.Hex()
}
val, ok := goref.ToString(ir.id)
if !ok {
panic("get insert id failed")
}
return val
}
@ -60,6 +66,7 @@ func (ir *InsertResult) GetID() string {
func (ir *InsertResult) GetIDs() []string {
vals, ok := goref.ToStrings(ir.id)
if !ok {
panic("get insert id failed")
}
return vals
}

View File

@ -1,15 +1,16 @@
package mdbc
package mdbc_test
import (
"context"
"github.com/stretchr/testify/assert"
"gitter.top/common/goref"
"gitter.top/coco/mdbc"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"testing"
)
func newDriver() *mdbc {
return NewMDBC(&Config{
func newDriver() mdbc.Collection {
return mdbc.NewMDBC(&mdbc.Config{
Address: "192.168.3.21",
Port: 0,
Username: "admin",
@ -17,31 +18,53 @@ func newDriver() *mdbc {
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,
})
}
func TestNewMDBC(t *testing.T) {
driver := newDriver()
databases, err := driver.GetClient().ListDatabaseNames(context.Background(), bson.D{})
assert.NoError(t, err)
t.Log(databases)
t.Log(res.GetID())
}
func TestScope_Count(t *testing.T) {
func TestScope_Delete(t *testing.T) {
driver := newDriver()
article := driver.BindModel(&ModelArticles{})
value, err := article.Count().SetContext(context.Background()).Count()
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(value)
t.Log(res)
}
func TestDropScope_Do(t *testing.T) {
func TestScope_Update(t *testing.T) {
driver := newDriver()
article := driver.BindModel(&ModelArticles{})
err := article.Drop().Do()
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 TestInsertScope_Insert(t *testing.T) {
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)
}

View File

@ -10,8 +10,8 @@ import (
)
type updateScope struct {
*scope
*mdbc
scope *scope
mdbc *mdbc
ctx context.Context
filter any
docs any
@ -34,13 +34,13 @@ func (us *updateScope) SetReplaceOptions(opts options.ReplaceOptions) *updateSco
return us
}
func (us *updateScope) SetID(id ...string) *updateScope {
func (us *updateScope) SetID(id ...any) *updateScope {
if len(id) == 0 {
return us
}
if len(id) == 1 {
us.filter = bson.M{
"_id": id,
"_id": id[0],
}
}
if len(id) > 1 {
@ -88,17 +88,17 @@ func (us *updateScope) mergeOptions() {
func (us *updateScope) One(docs interface{}) (*mongo.UpdateResult, error) {
us.docs = docs
us.mergeOptions()
return us.database.Collection(us.tableName).UpdateOne(us.ctx, us.filter, us.docs, us.opts.(*options.UpdateOptions))
return us.mdbc.database.Collection(us.scope.tableName).UpdateOne(us.ctx, us.filter, us.docs, us.opts.(*options.UpdateOptions))
}
func (us *updateScope) Many(docs interface{}) (*mongo.UpdateResult, error) {
us.docs = docs
us.mergeOptions()
return us.database.Collection(us.tableName).UpdateMany(us.ctx, us.filter, us.docs, us.opts.(*options.UpdateOptions))
return us.mdbc.database.Collection(us.scope.tableName).UpdateMany(us.ctx, us.filter, us.docs, us.opts.(*options.UpdateOptions))
}
func (us *updateScope) Replace(docs interface{}) (*mongo.UpdateResult, error) {
us.docs = docs
us.mergeOptions()
return us.database.Collection(us.tableName).ReplaceOne(us.ctx, us.filter, us.docs, us.opts.(*options.ReplaceOptions))
return us.mdbc.database.Collection(us.scope.tableName).ReplaceOne(us.ctx, us.filter, us.docs, us.opts.(*options.ReplaceOptions))
}