* externalize mongo wrapper * remove mongo env from drone * export mongo test for coverage report
Mongo

Provides helpers on top of mgo
Install and update
go get -u github.com/go-pkgz/mongo
Usage
-
Serverrepresents mongo instance and provides session accessor. Application usually creates one server object and uses it for anything needed with this particular mongo host or replica set. -
Connectionencapsulates session and provides auto-closable wrapper. Each requests runs inside one of With* function makes new mongo session and closes on completion. -
BufferedWriterimplements buffered writer to mongo. Write method caching internally till it reached buffer size. Flush methods can be called manually at any time.
m, err := NewServerWithURL("mongodb://127.0.0.1:27017/test?debug=true", 3*time.Second)
if err != nil {
panic("can't make mongo server")
}
type testRecord struct {
Key1 string
Kay2 int
}
err = c.WithCollection(func(coll *mgo.Collection) error { // create session
// insert 100 records
for i := 0; i < 100; i++ {
r := testRecord{
Key1: fmt.Sprintf("key-%02d", i%5),
Key2: i,
}
if e := coll.Insert(r); e != nil {
return e
}
}
return nil
})
Dependencies
- globalsign/mgo - mgo mongo driver
- stretchr/testify/ - testing library (test-only dependency)
Testing
testing.go helps to create test for real mongo (not mocks)
mongo.MakeTestConnectioncreatesmongo.Connectionfor url defined in envMONGO_TEST. If not definedmongodb://mongo:27017used. By default it will use random connection with prefixtest_intestDB.mongo.RemoveTestCollection- drops collection used byMakeTestConnectionmongo.RemoveTestCollections- drops user-defined collections fromtestDB