package documentation_examples_test
import (
"context"
"flag"
"log"
"os"
"testing"
"time"
"go.mongodb.org/mongo-driver/examples/documentation_examples"
"go.mongodb.org/mongo-driver/internal/assert"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.mongodb.org/mongo-driver/mongo/options"
)
func TestMain(m *testing.M) {
flag.Parse()
if testing.Short() {
log.Print("skipping mtest integration test in short mode")
return
}
if err := mtest.Setup(); err != nil {
log.Panic(err)
}
defer os.Exit(m.Run())
if err := mtest.Teardown(); err != nil {
log.Panic(err)
}
}
func TestDocumentationExamples(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(mtest.ClusterURI()))
assert.NoError(t, err)
defer client.Disconnect(ctx)
db := client.Database("documentation_examples")
t.Run("InsertExamples", func(t *testing.T) {
documentation_examples.InsertExamples(t, db)
})
t.Run("QueryArraysExamples", func(t *testing.T) {
documentation_examples.QueryArraysExamples(t, db)
})
t.Run("ProjectionExamples", func(t *testing.T) {
documentation_examples.ProjectionExamples(t, db)
})
t.Run("UpdateExamples", func(t *testing.T) {
documentation_examples.UpdateExamples(t, db)
})
t.Run("DeleteExamples", func(t *testing.T) {
documentation_examples.DeleteExamples(t, db)
})
t.Run("RunCommandExamples", func(t *testing.T) {
documentation_examples.RunCommandExamples(t, db)
})
t.Run("IndexExamples", func(t *testing.T) {
documentation_examples.IndexExamples(t, db)
})
t.Run("StableAPExamples", func(t *testing.T) {
documentation_examples.StableAPIExamples()
})
t.Run("QueryToplevelFieldsExamples", func(t *testing.T) {
documentation_examples.QueryToplevelFieldsExamples(t, db)
})
t.Run("QueryEmbeddedDocumentsExamples", func(t *testing.T) {
documentation_examples.QueryEmbeddedDocumentsExamples(t, db)
})
t.Run("QueryArrayEmbeddedDocumentsExamples", func(t *testing.T) {
documentation_examples.QueryArrayEmbeddedDocumentsExamples(t, db)
})
t.Run("QueryNullMissingFieldsExamples", func(t *testing.T) {
documentation_examples.QueryNullMissingFieldsExamples(t, db)
})
mt := mtest.New(t)
mtOpts := mtest.NewOptions().MinServerVersion("5.0")
mt.RunOpts("StableAPIStrictCountExample", mtOpts, func(mt *mtest.T) {
mt.Skip(`skipping because "count" is now part of Stable API v1; see GODRIVER-2482`)
documentation_examples.StableAPIStrictCountExample(mt.T)
})
mtOpts = mtest.NewOptions().MinServerVersion("5.0").Topologies(mtest.ReplicaSet, mtest.Sharded)
mt.RunOpts("SnapshotQueryExamples", mtOpts, func(mt *mtest.T) {
documentation_examples.SnapshotQueryExamples(mt)
})
mtOpts = mtest.NewOptions().MinServerVersion("3.6").CreateCollection(false)
mt.RunOpts("AggregationExamples", mtOpts, func(mt *mtest.T) {
documentation_examples.AggregationExamples(mt.T, db)
})
mtOpts = mtest.NewOptions().MinServerVersion("4.0").Topologies(mtest.ReplicaSet)
mt.RunOpts("TransactionsExamples", mtOpts, func(mt *mtest.T) {
mt.ResetClient(options.Client().ApplyURI(mtest.ClusterURI()))
documentation_examples.TransactionsExamples(ctx, mt.Client)
})
mt.RunOpts("WithTransactionExample", mtOpts, func(mt *mtest.T) {
mt.ResetClient(options.Client().ApplyURI(mtest.ClusterURI()))
documentation_examples.WithTransactionExample(ctx)
})
mtOpts = mtest.NewOptions().MinServerVersion("3.6").Topologies(mtest.ReplicaSet)
mt.RunOpts("ChangeStreamExamples", mtOpts, func(mt *mtest.T) {
mt.ResetClient(options.Client().ApplyURI(mtest.ClusterURI()))
csdb := client.Database("changestream_examples")
documentation_examples.ChangeStreamExamples(mt.T, csdb)
})
mtOpts = mtest.NewOptions().MinServerVersion("4.2").Topologies(mtest.ReplicaSet)
mt.RunOpts("CausalConsistencyExamples/post4.2", mtOpts, func(mt *mtest.T) {
documentation_examples.CausalConsistencyExamples(mt.Client)
})
mtOpts = mtest.NewOptions().MaxServerVersion("4.0").Topologies(mtest.ReplicaSet)
mt.RunOpts("CausalConsistencyExamples/pre4.0", mtOpts, func(mt *mtest.T) {
documentation_examples.CausalConsistencyExamples(mt.Client)
})
}