mongosteps
provides steps for cucumber/godog
and makes it easy to run tests with MongoDB.
Go >= 1.19
go get github.com/godogx/mongosteps
Initiate an mongosteps.Manager
and register it to the scenario
package mypackage
import (
"bytes"
"context"
"math/rand"
"testing"
"github.com/cucumber/godog"
"github.com/godogx/mongosteps"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func TestIntegration(t *testing.T) {
out := bytes.NewBuffer(nil)
// Create mongodb connection.
conn, err := mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
t.Fatalf("could not connect to mongodb: %s", err.Error())
}
// Initiate a new manager.
manager := mongosteps.NewManager(
mongosteps.WithDefaultDatabase(conn.Database("mydb"), mongosteps.CleanUpAfterScenario("mycollection")),
// If you have more than 1 database, you can use the following:
// mongosteps.WithDatabase("other", conn.Database("otherdb"), mongosteps.CleanUpAfterScenario("othercollection")),
)
suite := godog.TestSuite{
Name: "Integration",
TestSuiteInitializer: nil,
ScenarioInitializer: func(ctx *godog.ScenarioContext) {
// Register the client.
manager.RegisterContext(ctx)
},
Options: &godog.Options{
Strict: true,
Output: out,
Randomize: rand.Int63(),
},
}
// Run the suite.
if status := suite.Run(); status != 0 {
t.Fatal(out.String())
}
}
- All the JSON (for insertions and assertions) are in MongoDB Extended JSON (v2) format.
- For assertions, we do support
<ignore-diff>
for any data types.
For example: Given these documents are stored in the collection
[
{
"_id": {"$oid": "6250053966df8910f804c3a7"},
"name": "John Doe",
"age": 30,
"address": {
"street": "Street 1",
"city": "City 1",
"country": "Country 1"
}
}
]
This assertion matches
[
{
"_id": "<ignore-diff>",
"name": "John Doe",
"age": 30,
"address": "<ignore-diff>"
}
]
no (?:docs|documents) in collection "([^"]*)"$
no (?:docs|documents) in collection "([^"]*)" of database "([^"]*)"$
For example:
Given no documents in collection "customer"
Given no documents in collection "customer" of database "other"
these (?:docs|documents) are(?: stored)? in collection "([^"]*)"[:]?$
(?:docs|documents) from(?: file)? "([^"]*)" are(?: stored)? in collection "([^"]*)"$
these (?:docs|documents) are(?: stored)? in collection "([^"]*)" of database "([^"]*)"[:]?$
(?:docs|documents) from(?: file)? "([^"]*)" are(?: stored)? in collection "([^"]*)" of database "([^"]*)"[:]?$
For example:
Given these documents are stored in collection "customer":
"""
[
{
"_id": {"$oid": "6250053966df8910f804c3a7"},
"name": "John Doe",
"age": 30,
"address": {
"street": "Street 1",
"city": "City 1",
"country": "Country 1"
}
}
]
"""
Given documents from file "../../resources/fixtures/customers.json" are stored in collection "customer" of database "other"
no (?:docs|documents) are(?: available)? in collection "([^"]*)"$
no (?:docs|documents) are(?: available)? in collection "([^"]*)" of database "([^"]*)"$
For example:
Then no documents are available in collection "customer"
Then no documents are available in collection "customer" of database "other"
collection "([^"]*)" should have ([0-9]+) (?:doc|docs|document|documents)(?: available)?$
there (?:is|are) ([0-9]+) (?:doc|docs|document|documents)(?: available)? in collection "([^"]*)" of database "([^"]*)"$
collection "([^"]*)" of database "([^"]*)" should have ([0-9]+) (?:doc|docs|document|documents)(?: available)?$
there (?:is|are) ([0-9]+) (?:doc|docs|document|documents)(?: available)? in collection "([^"]*)"$
For example:
Then there are 2 documents in collection "customer"
Then collection "customer" of database "other" should have 2 documents
Inline documents:
collection "([^"]*)" should have only (?:this|these) (?:doc|docs|document|documents)(?: available)?[:]?$
there (?:is|are) only (?:this|these) (?:doc|docs|document|documents)(?: available)? in collection "([^"]*)"[:]?$
collection "([^"]*)" of database "([^"]*)" should have only (?:this|these) (?:doc|docs|document|documents)(?: available)?[:]?$
there (?:is|are) only (?:this|these) (?:doc|docs|document|documents)(?: available)? in collection "([^"]*)" of database "([^"]*)"[:]?$
From a file:
there (?:is|are) only (?:this|these) (?:doc|docs|document|documents) from(?: file)? "([^"]*)"(?: available)? in collection "([^"]*)"[:]?$
there (?:is|are) only (?:this|these) (?:doc|docs|document|documents) from(?: file)? "([^"]*)"(?: available)? in collection "([^"]*)" of database "([^"]*)"[:]?$
For example:
Then collection "customer" should have only these documents:
"""
[
{
"_id": "<ignored-diff>",
"name": "John Doe",
"age": 30,
"address": {
"street": "Street 1",
"city": "City 1",
"country": "Country 1"
}
}
]
"""
Then there are only these documents from file "../../resources/fixtures/customers.json" in collection "customer" of database "other"
Without a query:
(?:search|find) in collection "([^"]*)"$
(?:search|find) in collection "([^"]*)" of database "([^"]*)"$
With a query:
(?:search|find) in collection "([^"]*)" with query[:]?$
(?:search|find) in collection "([^"]*)" of database "([^"]*)" with query[:]?$
Assert number of documents found:
found ([0-9]+) (?:doc|docs|document|documents) in the result$
there (?:is|are) ([0-9]+) (?:doc|docs|document|documents) in the result$
Assert documents found:
(?:this|these) (?:doc|docs|document|documents) (?:is|are) in the result[:]?$
found (?:this|these) (?:doc|docs|document|documents) in the result[:]?$
For example:
Given documents from file "../../resources/fixtures/customers.json" are stored in collection "customer"
When I search in collection "customer"
Then I found 2 documents in the result
And I found these documents in the result:
"""
[
{
"_id": "<ignored-diff>",
"name": "John Doe",
"age": 30,
"address": {
"street": "Street 1",
"city": "City 1",
"country": "Country 1"
}
},
{
"_id": "<ignored-diff>",
"name": "Jane Doe",
"age": 20,
"address": {
"street": "Street 2",
"city": "City 2",
"country": "Country 2"
}
}
]
"""