mongo

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2020 License: MIT Imports: 13 Imported by: 1

README

Gnomock MongoDB Build Go Report Card

Gnomock MongoDB is a Gnomock preset for running tests against a real MongoDB container, without mocks.

package mongo_test

import (
	"context"
	"fmt"

	"github.com/orlangure/gnomock"
	mockmongo "github.com/orlangure/gnomock-mongo"
	"go.mongodb.org/mongo-driver/bson"
	mongodb "go.mongodb.org/mongo-driver/mongo"
	mongooptions "go.mongodb.org/mongo-driver/mongo/options"
)

func ExamplePreset() {
	p := mockmongo.Preset(
		mockmongo.WithData("./testdata/"),
		mockmongo.WithUser("gnomock", "gnomick"),
	)
	c, err := gnomock.Start(p)

	defer func() { _ = gnomock.Stop(c) }()

	if err != nil {
		panic(err)
	}

	addr := c.DefaultAddress()
	uri := fmt.Sprintf("mongodb://%s:%s@%s", "gnomock", "gnomick", addr)
	clientOptions := mongooptions.Client().ApplyURI(uri)

	client, err := mongodb.NewClient(clientOptions)
	if err != nil {
		panic(err)
	}

	ctx := context.Background()

	err = client.Connect(ctx)
	if err != nil {
		panic(err)
	}

	// see testdata folder to verify names/numbers
	fmt.Println(client.Database("db1").Collection("users").CountDocuments(ctx, bson.D{}))
	fmt.Println(client.Database("db2").Collection("customers").CountDocuments(ctx, bson.D{}))
	fmt.Println(client.Database("db2").Collection("countries").CountDocuments(ctx, bson.D{}))

	// Output:
	// 10 <nil>
	// 5 <nil>
	// 3 <nil>
}

Documentation

Overview

Package mongo includes mongo implementation of Gnomock Preset interface. This Preset can be passed to gnomock.StartPreset function to create a configured mongo container to use in tests

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Preset

func Preset(opts ...Option) gnomock.Preset

Preset creates a new Gmomock MongoDB preset. This preset includes a MongoDB specific healthcheck function, default MongoDB image and port, and allows to optionally set up initial state

Example
package main

import (
	"context"
	"fmt"

	"github.com/orlangure/gnomock"
	mockmongo "github.com/orlangure/gnomock-mongo"
	"go.mongodb.org/mongo-driver/bson"

	mongodb "go.mongodb.org/mongo-driver/mongo"

	mongooptions "go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
	p := mockmongo.Preset(
		mockmongo.WithData("./testdata/"),
		mockmongo.WithUser("gnomock", "gnomick"),
	)
	c, err := gnomock.Start(p)

	defer func() { _ = gnomock.Stop(c) }()

	if err != nil {
		panic(err)
	}

	addr := c.DefaultAddress()
	uri := fmt.Sprintf("mongodb://%s:%s@%s", "gnomock", "gnomick", addr)
	clientOptions := mongooptions.Client().ApplyURI(uri)

	client, err := mongodb.NewClient(clientOptions)
	if err != nil {
		panic(err)
	}

	ctx := context.Background()

	err = client.Connect(ctx)
	if err != nil {
		panic(err)
	}

	// see testdata folder to verify names/numbers
	fmt.Println(client.Database("db1").Collection("users").CountDocuments(ctx, bson.D{}))
	fmt.Println(client.Database("db2").Collection("customers").CountDocuments(ctx, bson.D{}))
	fmt.Println(client.Database("db2").Collection("countries").CountDocuments(ctx, bson.D{}))

}
Output:

10 <nil>
5 <nil>
3 <nil>

Types

type Option

type Option func(*P)

Option is an optional configuration of this Gnomock preset. Use available Options to configure the container

func WithData

func WithData(path string) Option

WithData sets up initial container state according to the directory structure at the given path:

  • path:
  • first
  • one
  • two
  • second
  • three

For such directory structure, two databases are created: "first" and "second". Under "first" database there are two collections, "one" and "two", and under "second" database - one collection "three".

Files "one", "two" and "three" are text files with JSON documents to be inserted into the database. One line should include one document.

Top level files under "path" are ignored, only directories are used. Similarly, directories located anywhere besides top-level "path", are also ignored

func WithUser

func WithUser(user, pass string) Option

WithUser creates a root user with the provided name and password. This user should be used as a part of mongodb connection string. If you choose not to use your own user and password, the databases will be unprotected, and you won't need to specify any name and password in your connection string

type P added in v0.1.1

type P struct {
	DataPath string `json:"data_path"`
	User     string `json:"user"`
	Password string `json:"password"`
}

P is a Gnomock Preset implementation of MongoDB

func (*P) Image added in v0.1.1

func (p *P) Image() string

Image returns an image that should be pulled to create this container

func (*P) Options added in v0.1.1

func (p *P) Options() []gnomock.Option

Options returns a list of options to configure this container

func (*P) Ports added in v0.1.1

func (p *P) Ports() gnomock.NamedPorts

Ports returns ports that should be used to access this container

Jump to

Keyboard shortcuts

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