Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"github.com/shellhub-io/mongotest"
)
func main() {
mongotest.Configure(mongotest.Config{
URL: "mongodb://root:password@127.0.0.1:27017",
Database: "mongotest",
FixtureRootDir: "testdata",
FixtureFormat: mongotest.FixtureFormatJSON,
PreInsertFuncs: []mongotest.PreInsertFunc{
mongotest.SimpleConvertTime("users", "created_at"),
},
})
// 1. Read testdata/json/admin_users.json and testdata/json/foo_users.json
// 2. Merge read data
// 3. Drop collection and insert read data
err := mongotest.UseFixture("json/admin_users", "json/foo_users")
if err != nil {
panic(err)
}
// Count is helper function.
// mongotest has some useful helper functions.
n, err := mongotest.Count("users")
if err != nil {
panic(err)
}
fmt.Println(n)
}
Output: 3
Index ¶
- Constants
- func Configure(c Config)
- func Count(collectionName string) (int64, error)
- func CountInt(collectionName string) (int, error)
- func CountIntWithContext(ctx context.Context, collectionName string) (int, error)
- func CountWithContext(ctx context.Context, collectionName string) (int64, error)
- func DropCollections(collections ...string) error
- func DropDatabase() error
- func Find(collectionName string, id interface{}) (map[string]interface{}, error)
- func FindWithContext(ctx context.Context, collectionName string, id interface{}) (map[string]interface{}, error)
- func Try() error
- func UseFixture(names ...string) error
- func UseFixtureWithContext(ctx context.Context, names ...string) error
- type CollectionData
- type Config
- type DataSet
- type DocData
- type FixtureFormatType
- type PreInsertFunc
Examples ¶
Constants ¶
const ( // FixtureFormatAuto means that fixture format is decided with file extension. (default) FixtureFormatAuto = FixtureFormatType("Auto") // FixtureFormatJSON means that fixture is written with JSON format. FixtureFormatJSON = FixtureFormatType("JSON") // FixtureFormatYAML means that fixture is written with YAML format. FixtureFormatYAML = FixtureFormatType("YAML") )
Variables ¶
This section is empty.
Functions ¶
func CountIntWithContext ¶
CountIntWithContext returns document count as int in collection that has given name. This function uses given context.
func CountWithContext ¶
CountWithContext returns document count in collection that has given name. This function uses given context.
func DropCollections ¶
DropCollections drops multiple collections with the given names.
func FindWithContext ¶
func FindWithContext(ctx context.Context, collectionName string, id interface{}) (map[string]interface{}, error)
FindWithContext document that has given id in given named collection. This function uses given context.
func UseFixture ¶
UseFixture apply fixture data to MongoDB. If multi names are given, fixture data will be merged.(overwriting by after dataset)
Types ¶
type CollectionData ¶
CollectionData is collection data
key: document ID value: document data (exclude ID)
type Config ¶
type Config struct {
URL string
Database string
FixtureRootDir string
FixtureFormat FixtureFormatType
Timeout int
PreInsertFuncs []PreInsertFunc
// contains filtered or unexported fields
}
Config is configuration holder of mongotest module.
type DataSet ¶
type DataSet map[string]CollectionData
DataSet is collection of collection data
key: collection name value: collection data
type DocData ¶
type DocData map[string]interface{}
DocData is document data
key: field name value: field value
type FixtureFormatType ¶
type FixtureFormatType string
FixtureFormatType is decision policy of fixture data format.
type PreInsertFunc ¶
PreInsertFunc is function for doing additional action to values.
func SimpleConvertBytes ¶
func SimpleConvertBytes(collectionName, fieldName string) PreInsertFunc
SimpleConvertBytes provides simple PreInsertFunc for converting string to BinData
func SimpleConvertObjID ¶
func SimpleConvertObjID(collectionName, fieldName string) PreInsertFunc
SimpleConvertObjID provides simple PreInsertFunc for converting string to ObjectID
func SimpleConvertTime ¶
func SimpleConvertTime(collectionName, fieldName string) PreInsertFunc
SimpleConvertTime provides simple PreInsertFunc for converting string time to time.Time.