2022-04-19 14:03:04 +08:00
|
|
|
package mon
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestBulkInserter(t *testing.T) {
|
|
|
|
|
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
|
|
|
|
defer mt.Close()
|
|
|
|
|
|
|
|
|
|
mt.Run("test", func(mt *mtest.T) {
|
|
|
|
|
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...))
|
2023-01-09 09:38:57 +08:00
|
|
|
bulk, err := NewBulkInserter(createModel(mt).Collection)
|
2023-01-06 23:30:50 +08:00
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
|
bulk.SetResultHandler(func(result *mongo.InsertManyResult, err error) {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, 2, len(result.InsertedIDs))
|
|
|
|
|
})
|
|
|
|
|
bulk.Insert(bson.D{{Key: "foo", Value: "bar"}})
|
|
|
|
|
bulk.Insert(bson.D{{Key: "foo", Value: "baz"}})
|
|
|
|
|
bulk.Flush()
|
|
|
|
|
})
|
|
|
|
|
}
|