|
1 | 1 | package mon |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "testing" |
| 6 | + "time" |
5 | 7 |
|
6 | 8 | "github.com/stretchr/testify/assert" |
7 | | - "go.mongodb.org/mongo-driver/bson" |
8 | | - "go.mongodb.org/mongo-driver/mongo" |
9 | | - "go.mongodb.org/mongo-driver/mongo/integration/mtest" |
| 9 | + "go.mongodb.org/mongo-driver/v2/mongo" |
| 10 | + "go.uber.org/mock/gomock" |
10 | 11 | ) |
11 | 12 |
|
12 | | -func TestBulkInserter(t *testing.T) { |
13 | | - mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) |
14 | | - mt.Run("test", func(mt *mtest.T) { |
15 | | - mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...)) |
16 | | - bulk, err := NewBulkInserter(createModel(mt).Collection) |
17 | | - assert.Equal(t, err, nil) |
18 | | - bulk.SetResultHandler(func(result *mongo.InsertManyResult, err error) { |
19 | | - assert.Nil(t, err) |
20 | | - assert.Equal(t, 2, len(result.InsertedIDs)) |
21 | | - }) |
22 | | - bulk.Insert(bson.D{{Key: "foo", Value: "bar"}}) |
23 | | - bulk.Insert(bson.D{{Key: "foo", Value: "baz"}}) |
24 | | - bulk.Flush() |
| 13 | +func TestBulkInserter_InsertAndFlush(t *testing.T) { |
| 14 | + ctrl := gomock.NewController(t) |
| 15 | + defer ctrl.Finish() |
| 16 | + mockCollection := NewMockCollection(ctrl) |
| 17 | + mockCollection.EXPECT().Clone().Return(&mongo.Collection{}) |
| 18 | + bulkInserter, err := NewBulkInserter(mockCollection, time.Second) |
| 19 | + assert.NoError(t, err) |
| 20 | + bulkInserter.SetResultHandler(func(result *mongo.InsertManyResult, err error) { |
| 21 | + assert.Nil(t, err) |
| 22 | + assert.Equal(t, 2, len(result.InsertedIDs)) |
25 | 23 | }) |
| 24 | + doc := map[string]interface{}{"name": "test"} |
| 25 | + bulkInserter.Insert(doc) |
| 26 | + bulkInserter.Flush() |
| 27 | +} |
| 28 | + |
| 29 | +func TestBulkInserter_SetResultHandler(t *testing.T) { |
| 30 | + ctrl := gomock.NewController(t) |
| 31 | + defer ctrl.Finish() |
| 32 | + mockCollection := NewMockCollection(ctrl) |
| 33 | + mockCollection.EXPECT().Clone().Return(nil) |
| 34 | + bulkInserter, err := NewBulkInserter(mockCollection) |
| 35 | + assert.NoError(t, err) |
| 36 | + mockHandler := func(result *mongo.InsertManyResult, err error) {} |
| 37 | + bulkInserter.SetResultHandler(mockHandler) |
| 38 | +} |
| 39 | + |
| 40 | +func TestDbInserter_RemoveAll(t *testing.T) { |
| 41 | + inserter := &dbInserter{} |
| 42 | + inserter.documents = []interface{}{} |
| 43 | + docs := inserter.RemoveAll() |
| 44 | + assert.NotNil(t, docs) |
| 45 | + assert.Empty(t, inserter.documents) |
| 46 | +} |
| 47 | + |
| 48 | +func Test_dbInserter_Execute(t *testing.T) { |
| 49 | + type fields struct { |
| 50 | + collection collectionInserter |
| 51 | + documents []any |
| 52 | + resultHandler ResultHandler |
| 53 | + } |
| 54 | + ctrl := gomock.NewController(t) |
| 55 | + defer ctrl.Finish() |
| 56 | + mockCollection := NewMockcollectionInserter(ctrl) |
| 57 | + type args struct { |
| 58 | + objs any |
| 59 | + } |
| 60 | + tests := []struct { |
| 61 | + name string |
| 62 | + fields fields |
| 63 | + args args |
| 64 | + mock func() |
| 65 | + }{ |
| 66 | + { |
| 67 | + name: "empty doc", |
| 68 | + fields: fields{ |
| 69 | + collection: nil, |
| 70 | + documents: nil, |
| 71 | + resultHandler: nil, |
| 72 | + }, |
| 73 | + args: args{ |
| 74 | + objs: make([]any, 0), |
| 75 | + }, |
| 76 | + mock: func() {}, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "result handler", |
| 80 | + fields: fields{ |
| 81 | + collection: mockCollection, |
| 82 | + resultHandler: func(result *mongo.InsertManyResult, err error) { |
| 83 | + assert.NotNil(t, err) |
| 84 | + }, |
| 85 | + }, |
| 86 | + args: args{ |
| 87 | + objs: make([]any, 1), |
| 88 | + }, |
| 89 | + mock: func() { |
| 90 | + mockCollection.EXPECT().InsertMany(gomock.Any(), gomock.Any()).Return(&mongo.InsertManyResult{}, errors.New("error")) |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "normal error handler", |
| 95 | + fields: fields{ |
| 96 | + collection: mockCollection, |
| 97 | + resultHandler: nil, |
| 98 | + }, |
| 99 | + args: args{ |
| 100 | + objs: make([]any, 1), |
| 101 | + }, |
| 102 | + mock: func() { |
| 103 | + mockCollection.EXPECT().InsertMany(gomock.Any(), gomock.Any()).Return(&mongo.InsertManyResult{}, errors.New("error")) |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "no error", |
| 108 | + fields: fields{ |
| 109 | + collection: mockCollection, |
| 110 | + resultHandler: nil, |
| 111 | + }, |
| 112 | + args: args{ |
| 113 | + objs: make([]any, 1), |
| 114 | + }, |
| 115 | + mock: func() { |
| 116 | + mockCollection.EXPECT().InsertMany(gomock.Any(), gomock.Any()).Return(&mongo.InsertManyResult{}, nil) |
| 117 | + }, |
| 118 | + }, |
| 119 | + } |
| 120 | + for _, tt := range tests { |
| 121 | + t.Run(tt.name, func(t *testing.T) { |
| 122 | + tt.mock() |
| 123 | + in := &dbInserter{ |
| 124 | + collection: tt.fields.collection, |
| 125 | + documents: tt.fields.documents, |
| 126 | + resultHandler: tt.fields.resultHandler, |
| 127 | + } |
| 128 | + in.Execute(tt.args.objs) |
| 129 | + }) |
| 130 | + } |
26 | 131 | } |
0 commit comments