forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
127 lines (107 loc) · 4.3 KB
/
examples_test.go
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
// 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
// NOTE: Any time this file is modified, a WEBSITE ticket should be opened to sync the changes with
// the "What is MongoDB" webpage, which the example was originally added to as part of WEBSITE-5148.
package documentation_examples_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/examples/documentation_examples"
"go.mongodb.org/mongo-driver/internal/testutil"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/description"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
)
func TestDocumentationExamples(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cs := testutil.ConnString(t)
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(cs.String()))
require.NoError(t, err)
defer client.Disconnect(ctx)
db := client.Database("documentation_examples")
documentation_examples.InsertExamples(t, db)
documentation_examples.QueryToplevelFieldsExamples(t, db)
documentation_examples.QueryEmbeddedDocumentsExamples(t, db)
documentation_examples.QueryArraysExamples(t, db)
documentation_examples.QueryArrayEmbeddedDocumentsExamples(t, db)
documentation_examples.QueryNullMissingFieldsExamples(t, db)
documentation_examples.ProjectionExamples(t, db)
documentation_examples.UpdateExamples(t, db)
documentation_examples.DeleteExamples(t, db)
documentation_examples.RunCommandExamples(t, db)
documentation_examples.IndexExamples(t, db)
}
func TestAggregationExamples(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cs := testutil.ConnString(t)
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(cs.String()))
require.NoError(t, err)
defer client.Disconnect(ctx)
db := client.Database("documentation_examples")
ver, err := getServerVersion(ctx, client)
if err != nil || testutil.CompareVersions(t, ver, "3.6") < 0 {
t.Skip("server does not support let in $lookup in aggregations")
}
documentation_examples.AggregationExamples(t, db)
}
func TestTransactionExamples(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
topo := createTopology(t)
client, err := mongo.Connect(context.Background(), &options.ClientOptions{Deployment: topo})
require.NoError(t, err)
defer client.Disconnect(ctx)
ver, err := getServerVersion(ctx, client)
if err != nil || testutil.CompareVersions(t, ver, "4.0") < 0 || topo.Kind() != description.ReplicaSet {
t.Skip("server does not support transactions")
}
err = documentation_examples.TransactionsExamples(ctx, client)
require.NoError(t, err)
}
func TestChangeStreamExamples(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
topo := createTopology(t)
client, err := mongo.Connect(context.Background(), &options.ClientOptions{Deployment: topo})
require.NoError(t, err)
defer client.Disconnect(ctx)
db := client.Database("changestream_examples")
ver, err := getServerVersion(ctx, client)
if err != nil || testutil.CompareVersions(t, ver, "3.6") < 0 || topo.Kind() != description.ReplicaSet {
t.Skip("server does not support changestreams")
}
documentation_examples.ChangeStreamExamples(t, db)
}
func getServerVersion(ctx context.Context, client *mongo.Client) (string, error) {
serverStatus, err := client.Database("admin").RunCommand(
ctx,
bsonx.Doc{{"serverStatus", bsonx.Int32(1)}},
).DecodeBytes()
if err != nil {
return "", err
}
version, err := serverStatus.LookupErr("version")
if err != nil {
return "", err
}
return version.StringValue(), nil
}
func createTopology(t *testing.T) *topology.Topology {
topo, err := topology.New(topology.WithConnString(func(connstring.ConnString) connstring.ConnString {
return testutil.ConnString(t)
}))
if err != nil {
t.Fatalf("topology.New error: %v", err)
}
return topo
}