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 ¶
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.
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.