1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
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) {
// All tests that use mtest.Setup() are expected to be integration tests, so skip them when the
// -short flag is included in the "go test" command. Also, we have to parse flags here to use
// testing.Short() because flags aren't parsed before TestMain() is called.
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)
// Stable API is supported in 5.0+
mtOpts := mtest.NewOptions().MinServerVersion("5.0")
mt.RunOpts("StableAPIStrictCountExample", mtOpts, func(mt *mtest.T) {
// TODO(GODRIVER-2482): Unskip when this test is rewritten to work with Stable API v1.
mt.Skip(`skipping because "count" is now part of Stable API v1; see GODRIVER-2482`)
documentation_examples.StableAPIStrictCountExample(mt.T)
})
// Snapshot queries can only run on 5.0+ non-sharded and sharded replicasets.
mtOpts = mtest.NewOptions().MinServerVersion("5.0").Topologies(mtest.ReplicaSet, mtest.Sharded)
mt.RunOpts("SnapshotQueryExamples", mtOpts, func(mt *mtest.T) {
documentation_examples.SnapshotQueryExamples(mt)
})
// Only 3.6+ supports $lookup in aggregations as is used in aggregation examples. No
// collection needs to be created, and collection creation can sometimes cause failures
// on sharded clusters.
mtOpts = mtest.NewOptions().MinServerVersion("3.6").CreateCollection(false)
mt.RunOpts("AggregationExamples", mtOpts, func(mt *mtest.T) {
documentation_examples.AggregationExamples(mt.T, db)
})
// Transaction examples can only run on replica sets on 4.0+.
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)
})
// Change stream examples can only run on replica sets on 3.6+.
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)
})
// Causal consistency examples cannot run on 4.0.
// TODO(GODRIVER-2238): Remove version filtering once failures on 4.0 sharded clusters are fixed.
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)
})
}
|