@@ -8,7 +8,15 @@ package mongo
8
8
9
9
import (
10
10
"context"
11
+ "testing"
11
12
"time"
13
+
14
+ "github.com/google/go-cmp/cmp"
15
+ "github.com/mongodb/mongo-go-driver/mongo/options"
16
+ "github.com/mongodb/mongo-go-driver/mongo/readconcern"
17
+ "github.com/mongodb/mongo-go-driver/mongo/readpref"
18
+ "github.com/mongodb/mongo-go-driver/mongo/writeconcern"
19
+ "github.com/mongodb/mongo-go-driver/x/network/connstring"
12
20
)
13
21
14
22
func ExampleClient_Connect () {
@@ -25,3 +33,167 @@ func ExampleClient_Connect() {
25
33
26
34
return
27
35
}
36
+
37
+ func TestClient (t * testing.T ) {
38
+ t .Run ("Can Set ReadPreference" , func (t * testing.T ) {
39
+ t .Run ("from connstring" , func (t * testing.T ) {
40
+ cs , err := connstring .Parse ("mongodb://localhost/?readPreference=secondary" )
41
+ noerr (t , err )
42
+ want , err := readpref .New (readpref .SecondaryMode )
43
+ noerr (t , err )
44
+ client , err := newClient (cs )
45
+ noerr (t , err )
46
+ got := client .readPreference
47
+ if got .Mode () != want .Mode () {
48
+ t .Errorf ("ReadPreference mode not set correctly. got %v; want %v" , got .Mode (), want .Mode ())
49
+ }
50
+ })
51
+ t .Run ("from option" , func (t * testing.T ) {
52
+ cs , err := connstring .Parse ("mongodb://localhost" )
53
+ noerr (t , err )
54
+ want , err := readpref .New (readpref .SecondaryMode )
55
+ noerr (t , err )
56
+ client , err := newClient (cs , options .Client ().SetReadPreference (want ))
57
+ noerr (t , err )
58
+ got := client .readPreference
59
+ if got .Mode () != want .Mode () {
60
+ t .Errorf ("ReadPreference mode not set correctly. got %v; want %v" , got .Mode (), want .Mode ())
61
+ }
62
+ })
63
+ t .Run ("option overrides connstring" , func (t * testing.T ) {
64
+ cs , err := connstring .Parse ("mongodb://localhost/?readPreference=primaryPreferred" )
65
+ noerr (t , err )
66
+ want , err := readpref .New (readpref .SecondaryPreferredMode )
67
+ noerr (t , err )
68
+ client , err := newClient (cs , options .Client ().SetReadPreference (want ))
69
+ noerr (t , err )
70
+ got := client .readPreference
71
+ if got .Mode () != want .Mode () {
72
+ t .Errorf ("ReadPreference mode not set correctly. got %v; want %v" , got .Mode (), want .Mode ())
73
+ }
74
+ })
75
+ })
76
+ t .Run ("Can Set ReadConcern" , func (t * testing.T ) {
77
+ t .Run ("from connstring" , func (t * testing.T ) {
78
+ cs , err := connstring .Parse ("mongodb://localhost/?readConcernlevel=majority" )
79
+ noerr (t , err )
80
+ rc := readconcern .Majority ()
81
+ client , err := newClient (cs )
82
+ noerr (t , err )
83
+ want , err := rc .MarshalBSONElement ()
84
+ noerr (t , err )
85
+ got , err := client .readConcern .MarshalBSONElement ()
86
+ noerr (t , err )
87
+ if ! cmp .Equal (got , want ) {
88
+ t .Errorf ("ReadConcern mode not set correctly. got %v; want %v" , got , want )
89
+ }
90
+ })
91
+ t .Run ("from option" , func (t * testing.T ) {
92
+ cs , err := connstring .Parse ("mongodb://localhost" )
93
+ noerr (t , err )
94
+ rc := readconcern .Majority ()
95
+ client , err := newClient (cs , options .Client ().SetReadConcern (rc ))
96
+ noerr (t , err )
97
+ want , err := rc .MarshalBSONElement ()
98
+ noerr (t , err )
99
+ got , err := client .readConcern .MarshalBSONElement ()
100
+ noerr (t , err )
101
+ if ! cmp .Equal (got , want ) {
102
+ t .Errorf ("ReadConcern mode not set correctly. got %v; want %v" , got , want )
103
+ }
104
+ })
105
+ t .Run ("option overrides connstring" , func (t * testing.T ) {
106
+ cs , err := connstring .Parse ("mongodb://localhost/?readConcernLevel=majority" )
107
+ noerr (t , err )
108
+ rc := readconcern .Linearizable ()
109
+ client , err := newClient (cs , options .Client ().SetReadConcern (rc ))
110
+ noerr (t , err )
111
+ want , err := rc .MarshalBSONElement ()
112
+ noerr (t , err )
113
+ got , err := client .readConcern .MarshalBSONElement ()
114
+ noerr (t , err )
115
+ if ! cmp .Equal (got , want ) {
116
+ t .Errorf ("ReadConcern mode not set correctly. got %v; want %v" , got , want )
117
+ }
118
+ })
119
+ })
120
+ t .Run ("Can Set WriteConcern" , func (t * testing.T ) {
121
+ t .Run ("from connstring" , func (t * testing.T ) {
122
+ cs , err := connstring .Parse ("mongodb://localhost/?w=majority" )
123
+ noerr (t , err )
124
+ wc := writeconcern .New (writeconcern .WMajority ())
125
+ client , err := newClient (cs )
126
+ noerr (t , err )
127
+ want , err := wc .MarshalBSONElement ()
128
+ noerr (t , err )
129
+ got , err := client .writeConcern .MarshalBSONElement ()
130
+ noerr (t , err )
131
+ if ! cmp .Equal (got , want ) {
132
+ t .Errorf ("WriteConcern mode not set correctly. got %v; want %v" , got , want )
133
+ }
134
+ })
135
+ t .Run ("from option" , func (t * testing.T ) {
136
+ cs , err := connstring .Parse ("mongodb://localhost" )
137
+ noerr (t , err )
138
+ wc := writeconcern .New (writeconcern .WMajority ())
139
+ client , err := newClient (cs , options .Client ().SetWriteConcern (wc ))
140
+ noerr (t , err )
141
+ want , err := wc .MarshalBSONElement ()
142
+ noerr (t , err )
143
+ got , err := client .writeConcern .MarshalBSONElement ()
144
+ noerr (t , err )
145
+ if ! cmp .Equal (got , want ) {
146
+ t .Errorf ("WriteConcern mode not set correctly. got %v; want %v" , got , want )
147
+ }
148
+ })
149
+ t .Run ("option overrides connstring" , func (t * testing.T ) {
150
+ cs , err := connstring .Parse ("mongodb://localhost/?w=1" )
151
+ noerr (t , err )
152
+ wc := writeconcern .New (writeconcern .WMajority ())
153
+ client , err := newClient (cs , options .Client ().SetWriteConcern (wc ))
154
+ noerr (t , err )
155
+ want , err := wc .MarshalBSONElement ()
156
+ noerr (t , err )
157
+ got , err := client .writeConcern .MarshalBSONElement ()
158
+ noerr (t , err )
159
+ if ! cmp .Equal (got , want ) {
160
+ t .Errorf ("WriteConcern mode not set correctly. got %v; want %v" , got , want )
161
+ }
162
+ })
163
+ })
164
+ t .Run ("Can Set RetryWrites" , func (t * testing.T ) {
165
+ t .Run ("from connstring" , func (t * testing.T ) {
166
+ cs , err := connstring .Parse ("mongodb://localhost/?retryWrites=true" )
167
+ noerr (t , err )
168
+ client , err := newClient (cs )
169
+ noerr (t , err )
170
+ want := true
171
+ got := client .retryWrites
172
+ if ! cmp .Equal (got , want ) {
173
+ t .Errorf ("RetryWrites mode not set correctly. got %v; want %v" , got , want )
174
+ }
175
+ })
176
+ t .Run ("from option" , func (t * testing.T ) {
177
+ cs , err := connstring .Parse ("mongodb://localhost" )
178
+ noerr (t , err )
179
+ client , err := newClient (cs , options .Client ().SetRetryWrites (true ))
180
+ noerr (t , err )
181
+ want := true
182
+ got := client .retryWrites
183
+ if ! cmp .Equal (got , want ) {
184
+ t .Errorf ("RetryWrites mode not set correctly. got %v; want %v" , got , want )
185
+ }
186
+ })
187
+ t .Run ("option overrides connstring" , func (t * testing.T ) {
188
+ cs , err := connstring .Parse ("mongodb://localhost/?retryWrites=true" )
189
+ noerr (t , err )
190
+ client , err := newClient (cs , options .Client ().SetRetryWrites (false ))
191
+ noerr (t , err )
192
+ want := false
193
+ got := client .retryWrites
194
+ if ! cmp .Equal (got , want ) {
195
+ t .Errorf ("RetryWrites mode not set correctly. got %v; want %v" , got , want )
196
+ }
197
+ })
198
+ })
199
+ }
0 commit comments