-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
108 lines (102 loc) · 3.42 KB
/
client_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
// 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 mongo
import (
"context"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
)
func ExampleClient_Connect() {
client, err := NewClient(options.Client().ApplyURI("mongodb://foo:bar@localhost:27017"))
if err != nil {
return
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
return
}
return
}
func TestClient(t *testing.T) {
t.Run("Can configure LocalThreshold", func(t *testing.T) {
t.Run("Should have default LocalThreshold set", func(t *testing.T) {
client := new(Client)
err := client.configure(options.Client())
noerr(t, err)
want, got := defaultLocalThreshold, client.localThreshold
if got != want {
t.Errorf("Default LocalThreshold not set on client. got %v; want %v", got, want)
}
})
opts := options.Client().SetLocalThreshold(10 * time.Second)
client := new(Client)
err := client.configure(opts)
noerr(t, err)
want, got := 10*time.Second, client.localThreshold
if got != want {
t.Errorf("Couldn't configure LocalThreshold. got %v; want %v", got, want)
}
})
t.Run("Can configure ReadConcern", func(t *testing.T) {
opts := options.Client().SetReadConcern(readconcern.Majority())
client := new(Client)
err := client.configure(opts)
noerr(t, err)
wantT, wantData, err := readconcern.Majority().MarshalBSONValue()
noerr(t, err)
gotT, gotData, err := client.readConcern.MarshalBSONValue()
noerr(t, err)
want, got := bson.RawValue{Type: wantT, Value: wantData}, bson.RawValue{Type: gotT, Value: gotData}
if !cmp.Equal(got, want) {
t.Errorf("ReadConcern mode not set correctly. got %v; want %v", got, want)
}
})
t.Run("Can configure ReadPreference", func(t *testing.T) {
want, err := readpref.New(readpref.SecondaryMode)
noerr(t, err)
opts := options.Client().SetReadPreference(want)
client := new(Client)
err = client.configure(opts)
noerr(t, err)
got := client.readPreference
if got == nil || got.Mode() != want.Mode() {
t.Errorf("Couldn't configure ReadPreference. got %v; want %v", got, want)
}
})
t.Run("Can configure RetryWrites", func(t *testing.T) {
opts := options.Client().SetRetryWrites(true)
client := new(Client)
err := client.configure(opts)
noerr(t, err)
want, got := true, client.retryWrites
if got != want {
t.Errorf("Couldn't configure RetryWrites. got %v; want %v", got, want)
}
})
t.Run("Can configure WriteConcern", func(t *testing.T) {
wc := writeconcern.New(writeconcern.WMajority())
opts := options.Client().SetWriteConcern(wc)
client := new(Client)
err := client.configure(opts)
noerr(t, err)
wantT, wantData, err := wc.MarshalBSONValue()
noerr(t, err)
gotT, gotData, err := client.writeConcern.MarshalBSONValue()
noerr(t, err)
want, got := bson.RawValue{Type: wantT, Value: wantData}, bson.RawValue{Type: gotT, Value: gotData}
if !got.Equal(want) {
t.Errorf("Couldn't configure WriteConcern. got %v; want %v", got, want)
}
})
}