mongo

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2024 License: MIT Imports: 13 Imported by: 1

README

Gnomock MongoDB

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"
	"github.com/orlangure/gnomock/preset/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 := mongo.Preset(
		mongo.WithData("./testdata/"),
		mongo.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

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.

By default, this preset uses MongoDB 4.4.

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
  • path/first/two
  • path/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.

func WithVersion added in v0.9.0

func WithVersion(version string) Option

WithVersion sets image version.

type P

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

P is a Gnomock Preset implementation of MongoDB.

func (*P) Image

func (p *P) Image() string

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

func (*P) Options

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

Options returns a list of options to configure this container.

func (*P) Ports

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