1
2
3
4
5
6
7 package documentation_examples_test
8
9 import (
10 "context"
11 "flag"
12 "log"
13 "os"
14 "testing"
15 "time"
16
17 "go.mongodb.org/mongo-driver/examples/documentation_examples"
18 "go.mongodb.org/mongo-driver/internal/assert"
19 "go.mongodb.org/mongo-driver/mongo"
20 "go.mongodb.org/mongo-driver/mongo/integration/mtest"
21 "go.mongodb.org/mongo-driver/mongo/options"
22 )
23
24 func TestMain(m *testing.M) {
25
26
27
28 flag.Parse()
29 if testing.Short() {
30 log.Print("skipping mtest integration test in short mode")
31 return
32 }
33
34 if err := mtest.Setup(); err != nil {
35 log.Fatal(err)
36 }
37 defer os.Exit(m.Run())
38 if err := mtest.Teardown(); err != nil {
39 log.Fatal(err)
40 }
41 }
42
43 func TestDocumentationExamples(t *testing.T) {
44 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
45 defer cancel()
46
47 client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(mtest.ClusterURI()))
48 assert.NoError(t, err)
49 defer client.Disconnect(ctx)
50
51 db := client.Database("documentation_examples")
52
53 t.Run("InsertExamples", func(t *testing.T) {
54 documentation_examples.InsertExamples(t, db)
55 })
56 t.Run("QueryArraysExamples", func(t *testing.T) {
57 documentation_examples.QueryArraysExamples(t, db)
58 })
59 t.Run("ProjectionExamples", func(t *testing.T) {
60 documentation_examples.ProjectionExamples(t, db)
61 })
62 t.Run("UpdateExamples", func(t *testing.T) {
63 documentation_examples.UpdateExamples(t, db)
64 })
65 t.Run("DeleteExamples", func(t *testing.T) {
66 documentation_examples.DeleteExamples(t, db)
67 })
68 t.Run("RunCommandExamples", func(t *testing.T) {
69 documentation_examples.RunCommandExamples(t, db)
70 })
71 t.Run("IndexExamples", func(t *testing.T) {
72 documentation_examples.IndexExamples(t, db)
73 })
74 t.Run("StableAPExamples", func(t *testing.T) {
75 documentation_examples.StableAPIExamples()
76 })
77 t.Run("QueryToplevelFieldsExamples", func(t *testing.T) {
78 documentation_examples.QueryToplevelFieldsExamples(t, db)
79 })
80 t.Run("QueryEmbeddedDocumentsExamples", func(t *testing.T) {
81 documentation_examples.QueryEmbeddedDocumentsExamples(t, db)
82 })
83 t.Run("QueryArrayEmbeddedDocumentsExamples", func(t *testing.T) {
84 documentation_examples.QueryArrayEmbeddedDocumentsExamples(t, db)
85 })
86 t.Run("QueryNullMissingFieldsExamples", func(t *testing.T) {
87 documentation_examples.QueryNullMissingFieldsExamples(t, db)
88 })
89
90 mt := mtest.New(t)
91
92
93 mtOpts := mtest.NewOptions().MinServerVersion("5.0")
94 mt.RunOpts("StableAPIStrictCountExample", mtOpts, func(mt *mtest.T) {
95
96 mt.Skip(`skipping because "count" is now part of Stable API v1; see GODRIVER-2482`)
97
98 documentation_examples.StableAPIStrictCountExample(mt.T)
99 })
100
101
102 mtOpts = mtest.NewOptions().MinServerVersion("5.0").Topologies(mtest.ReplicaSet, mtest.Sharded)
103 mt.RunOpts("SnapshotQueryExamples", mtOpts, func(mt *mtest.T) {
104 documentation_examples.SnapshotQueryExamples(mt)
105 })
106
107
108
109
110 mtOpts = mtest.NewOptions().MinServerVersion("3.6").CreateCollection(false)
111 mt.RunOpts("AggregationExamples", mtOpts, func(mt *mtest.T) {
112 documentation_examples.AggregationExamples(mt.T, db)
113 })
114
115
116 mtOpts = mtest.NewOptions().MinServerVersion("4.0").Topologies(mtest.ReplicaSet)
117 mt.RunOpts("TransactionsExamples", mtOpts, func(mt *mtest.T) {
118 mt.ResetClient(options.Client().ApplyURI(mtest.ClusterURI()))
119 documentation_examples.TransactionsExamples(ctx, mt.Client)
120 })
121 mt.RunOpts("WithTransactionExample", mtOpts, func(mt *mtest.T) {
122 mt.ResetClient(options.Client().ApplyURI(mtest.ClusterURI()))
123 documentation_examples.WithTransactionExample(ctx)
124 })
125
126
127 mtOpts = mtest.NewOptions().MinServerVersion("3.6").Topologies(mtest.ReplicaSet)
128 mt.RunOpts("ChangeStreamExamples", mtOpts, func(mt *mtest.T) {
129 mt.ResetClient(options.Client().ApplyURI(mtest.ClusterURI()))
130 csdb := client.Database("changestream_examples")
131 documentation_examples.ChangeStreamExamples(mt.T, csdb)
132 })
133
134
135
136 mtOpts = mtest.NewOptions().MinServerVersion("4.2").Topologies(mtest.ReplicaSet)
137 mt.RunOpts("CausalConsistencyExamples/post4.2", mtOpts, func(mt *mtest.T) {
138 documentation_examples.CausalConsistencyExamples(mt.Client)
139 })
140 mtOpts = mtest.NewOptions().MaxServerVersion("4.0").Topologies(mtest.ReplicaSet)
141 mt.RunOpts("CausalConsistencyExamples/pre4.0", mtOpts, func(mt *mtest.T) {
142 documentation_examples.CausalConsistencyExamples(mt.Client)
143 })
144 }
145
View as plain text