github.com/shuguocloud/go-zero@v1.3.0/core/stores/mongo/collection_test.go (about) 1 package mongo 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/globalsign/mgo" 8 "github.com/golang/mock/gomock" 9 "github.com/stretchr/testify/assert" 10 "github.com/shuguocloud/go-zero/core/breaker" 11 "github.com/shuguocloud/go-zero/core/logx" 12 "github.com/shuguocloud/go-zero/core/stores/mongo/internal" 13 "github.com/shuguocloud/go-zero/core/stringx" 14 ) 15 16 var errDummy = errors.New("dummy") 17 18 func init() { 19 logx.Disable() 20 } 21 22 func TestKeepPromise_accept(t *testing.T) { 23 p := new(mockPromise) 24 kp := keepablePromise{ 25 promise: p, 26 log: func(error) {}, 27 } 28 assert.Nil(t, kp.accept(nil)) 29 assert.Equal(t, mgo.ErrNotFound, kp.accept(mgo.ErrNotFound)) 30 } 31 32 func TestKeepPromise_keep(t *testing.T) { 33 tests := []struct { 34 err error 35 accepted bool 36 reason string 37 }{ 38 { 39 err: nil, 40 accepted: true, 41 reason: "", 42 }, 43 { 44 err: mgo.ErrNotFound, 45 accepted: true, 46 reason: "", 47 }, 48 { 49 err: errors.New("any"), 50 accepted: false, 51 reason: "any", 52 }, 53 } 54 55 for _, test := range tests { 56 t.Run(stringx.RandId(), func(t *testing.T) { 57 p := new(mockPromise) 58 kp := keepablePromise{ 59 promise: p, 60 log: func(error) {}, 61 } 62 assert.Equal(t, test.err, kp.keep(test.err)) 63 assert.Equal(t, test.accepted, p.accepted) 64 assert.Equal(t, test.reason, p.reason) 65 }) 66 } 67 } 68 69 func TestNewCollection(t *testing.T) { 70 col := newCollection(&mgo.Collection{ 71 Database: nil, 72 Name: "foo", 73 FullName: "bar", 74 }, breaker.GetBreaker("localhost")) 75 assert.Equal(t, "bar", col.(*decoratedCollection).name) 76 } 77 78 func TestCollectionFind(t *testing.T) { 79 ctrl := gomock.NewController(t) 80 defer ctrl.Finish() 81 82 var query mgo.Query 83 col := internal.NewMockMgoCollection(ctrl) 84 col.EXPECT().Find(gomock.Any()).Return(&query) 85 c := decoratedCollection{ 86 collection: col, 87 brk: breaker.NewBreaker(), 88 } 89 actual := c.Find(nil) 90 switch v := actual.(type) { 91 case promisedQuery: 92 assert.Equal(t, &query, v.Query) 93 assert.Equal(t, errDummy, v.promise.keep(errDummy)) 94 default: 95 t.Fail() 96 } 97 c.brk = new(dropBreaker) 98 actual = c.Find(nil) 99 assert.Equal(t, rejectedQuery{}, actual) 100 } 101 102 func TestCollectionFindId(t *testing.T) { 103 ctrl := gomock.NewController(t) 104 defer ctrl.Finish() 105 106 var query mgo.Query 107 col := internal.NewMockMgoCollection(ctrl) 108 col.EXPECT().FindId(gomock.Any()).Return(&query) 109 c := decoratedCollection{ 110 collection: col, 111 brk: breaker.NewBreaker(), 112 } 113 actual := c.FindId(nil) 114 switch v := actual.(type) { 115 case promisedQuery: 116 assert.Equal(t, &query, v.Query) 117 assert.Equal(t, errDummy, v.promise.keep(errDummy)) 118 default: 119 t.Fail() 120 } 121 c.brk = new(dropBreaker) 122 actual = c.FindId(nil) 123 assert.Equal(t, rejectedQuery{}, actual) 124 } 125 126 func TestCollectionInsert(t *testing.T) { 127 ctrl := gomock.NewController(t) 128 defer ctrl.Finish() 129 130 col := internal.NewMockMgoCollection(ctrl) 131 col.EXPECT().Insert(nil, nil).Return(errDummy) 132 c := decoratedCollection{ 133 collection: col, 134 brk: breaker.NewBreaker(), 135 } 136 err := c.Insert(nil, nil) 137 assert.Equal(t, errDummy, err) 138 c.brk = new(dropBreaker) 139 err = c.Insert(nil, nil) 140 assert.Equal(t, errDummy, err) 141 } 142 143 func TestCollectionPipe(t *testing.T) { 144 ctrl := gomock.NewController(t) 145 defer ctrl.Finish() 146 147 var pipe mgo.Pipe 148 col := internal.NewMockMgoCollection(ctrl) 149 col.EXPECT().Pipe(gomock.Any()).Return(&pipe) 150 c := decoratedCollection{ 151 collection: col, 152 brk: breaker.NewBreaker(), 153 } 154 actual := c.Pipe(nil) 155 switch v := actual.(type) { 156 case promisedPipe: 157 assert.Equal(t, &pipe, v.Pipe) 158 assert.Equal(t, errDummy, v.promise.keep(errDummy)) 159 default: 160 t.Fail() 161 } 162 c.brk = new(dropBreaker) 163 actual = c.Pipe(nil) 164 assert.Equal(t, rejectedPipe{}, actual) 165 } 166 167 func TestCollectionRemove(t *testing.T) { 168 ctrl := gomock.NewController(t) 169 defer ctrl.Finish() 170 171 col := internal.NewMockMgoCollection(ctrl) 172 col.EXPECT().Remove(gomock.Any()).Return(errDummy) 173 c := decoratedCollection{ 174 collection: col, 175 brk: breaker.NewBreaker(), 176 } 177 err := c.Remove(nil) 178 assert.Equal(t, errDummy, err) 179 c.brk = new(dropBreaker) 180 err = c.Remove(nil) 181 assert.Equal(t, errDummy, err) 182 } 183 184 func TestCollectionRemoveAll(t *testing.T) { 185 ctrl := gomock.NewController(t) 186 defer ctrl.Finish() 187 188 col := internal.NewMockMgoCollection(ctrl) 189 col.EXPECT().RemoveAll(gomock.Any()).Return(nil, errDummy) 190 c := decoratedCollection{ 191 collection: col, 192 brk: breaker.NewBreaker(), 193 } 194 _, err := c.RemoveAll(nil) 195 assert.Equal(t, errDummy, err) 196 c.brk = new(dropBreaker) 197 _, err = c.RemoveAll(nil) 198 assert.Equal(t, errDummy, err) 199 } 200 201 func TestCollectionRemoveId(t *testing.T) { 202 ctrl := gomock.NewController(t) 203 defer ctrl.Finish() 204 205 col := internal.NewMockMgoCollection(ctrl) 206 col.EXPECT().RemoveId(gomock.Any()).Return(errDummy) 207 c := decoratedCollection{ 208 collection: col, 209 brk: breaker.NewBreaker(), 210 } 211 err := c.RemoveId(nil) 212 assert.Equal(t, errDummy, err) 213 c.brk = new(dropBreaker) 214 err = c.RemoveId(nil) 215 assert.Equal(t, errDummy, err) 216 } 217 218 func TestCollectionUpdate(t *testing.T) { 219 ctrl := gomock.NewController(t) 220 defer ctrl.Finish() 221 222 col := internal.NewMockMgoCollection(ctrl) 223 col.EXPECT().Update(gomock.Any(), gomock.Any()).Return(errDummy) 224 c := decoratedCollection{ 225 collection: col, 226 brk: breaker.NewBreaker(), 227 } 228 err := c.Update(nil, nil) 229 assert.Equal(t, errDummy, err) 230 c.brk = new(dropBreaker) 231 err = c.Update(nil, nil) 232 assert.Equal(t, errDummy, err) 233 } 234 235 func TestCollectionUpdateId(t *testing.T) { 236 ctrl := gomock.NewController(t) 237 defer ctrl.Finish() 238 239 col := internal.NewMockMgoCollection(ctrl) 240 col.EXPECT().UpdateId(gomock.Any(), gomock.Any()).Return(errDummy) 241 c := decoratedCollection{ 242 collection: col, 243 brk: breaker.NewBreaker(), 244 } 245 err := c.UpdateId(nil, nil) 246 assert.Equal(t, errDummy, err) 247 c.brk = new(dropBreaker) 248 err = c.UpdateId(nil, nil) 249 assert.Equal(t, errDummy, err) 250 } 251 252 func TestCollectionUpsert(t *testing.T) { 253 ctrl := gomock.NewController(t) 254 defer ctrl.Finish() 255 256 col := internal.NewMockMgoCollection(ctrl) 257 col.EXPECT().Upsert(gomock.Any(), gomock.Any()).Return(nil, errDummy) 258 c := decoratedCollection{ 259 collection: col, 260 brk: breaker.NewBreaker(), 261 } 262 _, err := c.Upsert(nil, nil) 263 assert.Equal(t, errDummy, err) 264 c.brk = new(dropBreaker) 265 _, err = c.Upsert(nil, nil) 266 assert.Equal(t, errDummy, err) 267 } 268 269 type mockPromise struct { 270 accepted bool 271 reason string 272 } 273 274 func (p *mockPromise) Accept() { 275 p.accepted = true 276 } 277 278 func (p *mockPromise) Reject(reason string) { 279 p.reason = reason 280 } 281 282 type dropBreaker struct{} 283 284 func (d *dropBreaker) Name() string { 285 return "dummy" 286 } 287 288 func (d *dropBreaker) Allow() (breaker.Promise, error) { 289 return nil, errDummy 290 } 291 292 func (d *dropBreaker) Do(req func() error) error { 293 return nil 294 } 295 296 func (d *dropBreaker) DoWithAcceptable(req func() error, acceptable breaker.Acceptable) error { 297 return errDummy 298 } 299 300 func (d *dropBreaker) DoWithFallback(req func() error, fallback func(err error) error) error { 301 return nil 302 } 303 304 func (d *dropBreaker) DoWithFallbackAcceptable(req func() error, fallback func(err error) error, 305 acceptable breaker.Acceptable) error { 306 return nil 307 }