mongotest

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2022 License: MIT Imports: 13 Imported by: 0

README

mongotest

Test Coverage Status Go Report Card Godoc license

Description

mongotest is testing helper for using MongoDB.

Example

package main

import (
	"fmt"

	"github.com/pinzolo/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)
}

Install

$ go get github.com/pinzolo/mongotest

Contribution

  1. Fork (https://github.com/pinzolo/mongotest/fork)
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the go test ./... command and confirm that it passes
  6. Run gofmt -s
  7. Create a new Pull Request

Author

pinzolo

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/pinzolo/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

Examples

Constants

View Source
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 Configure

func Configure(c Config)

Configure overwrite configuration by given config.

func Count

func Count(collectionName string) (int64, error)

Count returns document count in collection that has given name.

func CountInt

func CountInt(collectionName string) (int, error)

CountInt returns document count as int in collection that has given name.

func CountIntWithContext

func CountIntWithContext(ctx context.Context, collectionName string) (int, error)

CountIntWithContext returns document count as int in collection that has given name. This function uses given context.

func CountWithContext

func CountWithContext(ctx context.Context, collectionName string) (int64, error)

CountWithContext returns document count in collection that has given name. This function uses given context.

func Find

func Find(collectionName string, id interface{}) (map[string]interface{}, error)

Find document that has given id in given named collection.

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 Try

func Try() error

Try connecting to MongoDB server.

func UseFixture

func UseFixture(names ...string) error

UseFixture apply fixture data to MongoDB. If multi names are given, fixture data will be merged.(overwriting by after dataset)

func UseFixtureWithContext

func UseFixtureWithContext(ctx context.Context, names ...string) error

UseFixtureWithContext apply fixture data to MongoDB with context.Context. If multi names are given, fixture data will be merged.(overwriting by after dataset)

Types

type CollectionData

type CollectionData map[string]DocData

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.

func Configuration

func Configuration() Config

Configuration returns current config.

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

func (DocData) StringValue

func (d DocData) StringValue(key string) (string, bool)

StringValue returns value of given key as string.

type FixtureFormatType

type FixtureFormatType string

FixtureFormatType is decision policy of fixture data format.

type PreInsertFunc

type PreInsertFunc func(collectionName string, doc DocData) (DocData, error)

PreInsertFunc is function for doing additional action to values.

func SimpleConvertTime

func SimpleConvertTime(collectionName, fieldName string) PreInsertFunc

SimpleConvertTime provides simple PreInsertFunc for converting string time to time.Time.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL