github.com/ddspog/mongo (go): Package mongo mask the connection to MongoDB using mgo package.  This is made with function Connect, that saves Session and Mongo object which will be used later from other packages. Also, I've embedded the Collection, Database and Query types, to allow mocking via interfaces. The embedded was necessary for the functions to use the interfaces as return values, that way, the code can use the original, or generate a mock of them for testing purposes.  The package can be used like this:  Other option of usage is through the use of mongo.DatabaseSocket:  Or even through the concept of LinkedHandlers, as described later:  Further usage it's the same way mgo package is used. Look into mgo docs page: https://godoc.org/github.com/globalsign/mgo  The Connect function tries to connect to a MONGODB_URL environment variable, but when it's not defined, it uses a default URL:  You can mock some functions of this package, by mocking the mgo called functions mgo.ParseURL and mgo.Dial. Use the MockMongoSetup presented on this package (only in test environment), like:  Mongo package also contain utility functions to help modeling documents.  The package contains a interface Documenter which contain getters for important attributes to any document on MongoDB: _id, created_on and updated_on. It also contains functions that generates correctly the created_on and updated_on attributes.  The Documenter can be used like this:  You can also mock some other functions of this package, by mocking some called functions time.Now and NewObjectId. Use the MockModelSetup presented on this package (only in test environment), like:  Mongo package also enable creation of Handle, a type that connects to database collections and do some operations.  The Handle were made to be imported on embedding type, and through overriding of some methods, to implement an adequate Handler for a desired type of Document. The Handle type assumes to operate on a Documenter type, that will contain information about the operation to made with Handle.  The package should be used to create new types. Use the Handler type for creating embedding types.  For each new type, a constructor may be needed, and for that Handler has a basic constructor.  All functions were made to be overridden and rewrite. First thing to do it's creating the Link method, as it follows:  The creation of Insert, Remove and RemoveAll are trivial. Call it with a Document getter function defined like:  The Clean function is simple and helps a lot:  The Update function uses an id as an argument:  The complicated functions are Find and FindAll which requires casting for the Document type:  For all functions written, verification it's advisable.
Keywords: embedded|go|golang|mgo|mocking|mongo|mongodb|package