mtest

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package mtest is a helper library for integration test with some MongoDB instance.

This package provide a clean and readable way to ensure testing pre-condition and validate post-condition for some test case.

As the design of MongoDB official Golang library, the document to be checked by this package is given by bson.M, bson.D or any Golang struct (with "bson" structure tag).

See https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo for more details.

Example (Basic)
t := &testing.T{}
someCollection := &mongo.Collection{}

// Prepare pre-condition with Condition.
For(t, someCollection).Condition().
	Empty().
	Exists(bson.M{"name": "Alice", "age": 22})

// Some manipulation on the document ...

// Check post-condition with Assert.
For(t, someCollection).Assert().
	Exists(bson.M{"name": "Alice", "age": 24}).
	NotExists(bson.M{"name": "Bob"})

// Can also use Golang struct for checking clause
type Person struct {
	Name string `bson:"name"`
	Age  int    `bson:"age"`
}
For(t, someCollection).Assert().
	Exists(Person{Name: "Alice", Age: 22})

// Use "From" helper to get collection if we only have the client object
client, _ := mongo.NewClient()
For(t, From(client, "company", "person")).Condition().
	Empty().
	Exists(bson.M{"name": "Alice", "age": 22})

// Use "OfId" helper to get a filter for some document ID
For(t, From(client, "company", "person")).Condition().
	Empty().
	NotExists(OfId("some-id"))
Output:

Example (ImmediatelyFail)
t := &testing.T{}
client, _ := mongo.NewClient()

// First part of logic being testing ...

// Check the post-condition and return immediately
// if the condition doesn't meet the requirement
For(t, From(client, "company", "person")).Require().
	NotExists(bson.M{"name": "Alice", "age": 24})

// Second part of logic being testing ...

For(t, From(client, "company", "person")).Require().
	Exists(bson.M{"name": "Alice", "age": 24})
Output:

Example (MultipleRequirement)
t := &testing.T{}
client, _ := mongo.NewClient()

// Batch insert logic ...

// Can check multiple document at once.
For(t, From(client, "company", "person")).Assert().
	Exists(
		bson.M{"name": "Alice", "age": 24},
		bson.M{"name": "Bob", "age": 16},
		bson.M{"name": "Charlie", "age": 53},
	)

// Another checking style.
For(t, From(client, "company", "person")).Assert().
	Exists(bson.M{"name": "Alice", "age": 24}).
	Exists(bson.M{"name": "Bob", "age": 16}).
	Exists(bson.M{"name": "Charlie", "age": 53})
Output:

Example (NoExistence)
t := &testing.T{}
client, _ := mongo.NewClient()

// Cleanup the collection and insert one document.
For(t, From(client, "company", "person")).Condition().
	Empty().
	Exists(bson.M{"name": "Alice", "age": 22})

// Delete the document ...

// Check post-condition with Assert.
For(t, From(client, "company", "person")).Assert().
	NotExists(bson.M{"name": "Alice", "age": 24})

// If we need to check the collection is indeed cleaned up.
For(t, From(client, "company", "person")).Assert().
	Empty()
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func From

func From(client *mongo.Client, database, collection string) *mongo.Collection

From is a shortcut for construct a collection from client object.

func OfId

func OfId(id interface{}) interface{}

OfId return a document filter which matches a document by document ID ("_id" field).

Types

type Assert

type Assert struct {
	// contains filtered or unexported fields
}

Assert is a object for validate(t.Fail) post-condition for some test case.

func (*Assert) Empty

func (r *Assert) Empty() *Assert

func (*Assert) Exists

func (r *Assert) Exists(documents ...interface{}) *Assert

func (*Assert) NotExists

func (r *Assert) NotExists(documents ...interface{}) *Assert

type Collection

type Collection struct {
	// contains filtered or unexported fields
}

func For

func For(t *testing.T, col *mongo.Collection) *Collection

For binds a Mongo collection with a test reporting interface.

func (*Collection) Assert

func (c *Collection) Assert() *Assert

func (*Collection) Condition

func (c *Collection) Condition() *Condition

func (*Collection) Require

func (c *Collection) Require() *Require

type Condition

type Condition struct {
	// contains filtered or unexported fields
}

Condition is a object for ensure(t.FailNow) pre-condition for some test case.

func (*Condition) Empty

func (c *Condition) Empty() *Condition

func (*Condition) Exists

func (c *Condition) Exists(documents ...interface{}) *Condition

func (*Condition) NotExists

func (c *Condition) NotExists(documents ...interface{}) *Condition

type Require

type Require struct {
	// contains filtered or unexported fields
}

Require is a object for validate(t.FailNow) post-condition for some test case.

func (*Require) Empty

func (r *Require) Empty() *Require

func (*Require) Exists

func (r *Require) Exists(documents ...interface{}) *Require

func (*Require) NotExists

func (r *Require) NotExists(documents ...interface{}) *Require

Jump to

Keyboard shortcuts

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