@@ -27,6 +27,7 @@ import (
27
27
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
28
28
"go.mongodb.org/mongo-driver/mongo/options"
29
29
"go.mongodb.org/mongo-driver/mongo/readpref"
30
+ "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
30
31
"go.mongodb.org/mongo-driver/x/mongo/driver"
31
32
"go.mongodb.org/mongo-driver/x/mongo/driver/drivertest"
32
33
"go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage"
@@ -147,68 +148,106 @@ func TestClient(t *testing.T) {
147
148
mt .Fatal ("unable to find authenticated user" )
148
149
})
149
150
mt .RunOpts ("list databases" , noClientOpts , func (mt * mtest.T ) {
150
- testCases := []struct {
151
- name string
152
- filter bson.D
153
- hasTestDb bool
154
- minServerVersion string
155
- }{
156
- {"no filter" , bson.D {}, true , "" },
157
- {"filter" , bson.D {{"name" , "foobar" }}, false , "3.6" },
158
- }
159
-
160
- for _ , tc := range testCases {
161
- opts := mtest .NewOptions ()
162
- if tc .minServerVersion != "" {
163
- opts .MinServerVersion (tc .minServerVersion )
151
+ mt .RunOpts ("filter" , noClientOpts , func (mt * mtest.T ) {
152
+ testCases := []struct {
153
+ name string
154
+ filter bson.D
155
+ hasTestDb bool
156
+ minServerVersion string
157
+ }{
158
+ {"empty" , bson.D {}, true , "" },
159
+ {"non-empty" , bson.D {{"name" , "foobar" }}, false , "3.6" },
164
160
}
165
161
166
- mt .RunOpts (tc .name , opts , func (mt * mtest.T ) {
167
- res , err := mt .Client .ListDatabases (mtest .Background , tc .filter )
168
- assert .Nil (mt , err , "ListDatabases error: %v" , err )
162
+ for _ , tc := range testCases {
163
+ opts := mtest .NewOptions ()
164
+ if tc .minServerVersion != "" {
165
+ opts .MinServerVersion (tc .minServerVersion )
166
+ }
167
+
168
+ mt .RunOpts (tc .name , opts , func (mt * mtest.T ) {
169
+ res , err := mt .Client .ListDatabases (mtest .Background , tc .filter )
170
+ assert .Nil (mt , err , "ListDatabases error: %v" , err )
169
171
170
- var found bool
171
- for _ , db := range res .Databases {
172
- if db .Name == mtest .TestDb {
173
- found = true
174
- break
172
+ var found bool
173
+ for _ , db := range res .Databases {
174
+ if db .Name == mtest .TestDb {
175
+ found = true
176
+ break
177
+ }
175
178
}
176
- }
177
- assert .Equal (mt , tc .hasTestDb , found , "expected to find test db: %v, found: %v" , tc .hasTestDb , found )
178
- })
179
- }
179
+ assert .Equal (mt , tc .hasTestDb , found , "expected to find test db: %v, found: %v" , tc .hasTestDb , found )
180
+ })
181
+ }
182
+ })
183
+ mt .Run ("options" , func (mt * mtest.T ) {
184
+ allOpts := options .ListDatabases ().SetNameOnly (true ).SetAuthorizedDatabases (true )
185
+ mt .ClearEvents ()
186
+
187
+ _ , err := mt .Client .ListDatabases (mtest .Background , bson.D {}, allOpts )
188
+ assert .Nil (mt , err , "ListDatabases error: %v" , err )
189
+
190
+ evt := mt .GetStartedEvent ()
191
+ assert .Equal (mt , "listDatabases" , evt .CommandName , "expected " )
192
+
193
+ expectedDoc := bsoncore .BuildDocumentFromElements (nil ,
194
+ bsoncore .AppendBooleanElement (nil , "nameOnly" , true ),
195
+ bsoncore .AppendBooleanElement (nil , "authorizedDatabases" , true ),
196
+ )
197
+ err = compareDocs (mt , expectedDoc , evt .Command )
198
+ assert .Nil (mt , err , "compareDocs error: %v" , err )
199
+ })
180
200
})
181
201
mt .RunOpts ("list database names" , noClientOpts , func (mt * mtest.T ) {
182
- testCases := []struct {
183
- name string
184
- filter bson.D
185
- hasTestDb bool
186
- minServerVersion string
187
- }{
188
- {"no filter" , bson.D {}, true , "" },
189
- {"filter" , bson.D {{"name" , "foobar" }}, false , "3.6" },
190
- }
191
-
192
- for _ , tc := range testCases {
193
- opts := mtest .NewOptions ()
194
- if tc .minServerVersion != "" {
195
- opts .MinServerVersion (tc .minServerVersion )
202
+ mt .RunOpts ("filter" , noClientOpts , func (mt * mtest.T ) {
203
+ testCases := []struct {
204
+ name string
205
+ filter bson.D
206
+ hasTestDb bool
207
+ minServerVersion string
208
+ }{
209
+ {"no filter" , bson.D {}, true , "" },
210
+ {"filter" , bson.D {{"name" , "foobar" }}, false , "3.6" },
196
211
}
197
212
198
- mt .RunOpts (tc .name , opts , func (mt * mtest.T ) {
199
- dbs , err := mt .Client .ListDatabaseNames (mtest .Background , tc .filter )
200
- assert .Nil (mt , err , "ListDatabaseNames error: %v" , err )
213
+ for _ , tc := range testCases {
214
+ opts := mtest .NewOptions ()
215
+ if tc .minServerVersion != "" {
216
+ opts .MinServerVersion (tc .minServerVersion )
217
+ }
218
+
219
+ mt .RunOpts (tc .name , opts , func (mt * mtest.T ) {
220
+ dbs , err := mt .Client .ListDatabaseNames (mtest .Background , tc .filter )
221
+ assert .Nil (mt , err , "ListDatabaseNames error: %v" , err )
201
222
202
- var found bool
203
- for _ , db := range dbs {
204
- if db == mtest .TestDb {
205
- found = true
206
- break
223
+ var found bool
224
+ for _ , db := range dbs {
225
+ if db == mtest .TestDb {
226
+ found = true
227
+ break
228
+ }
207
229
}
208
- }
209
- assert .Equal (mt , tc .hasTestDb , found , "expected to find test db: %v, found: %v" , tc .hasTestDb , found )
210
- })
211
- }
230
+ assert .Equal (mt , tc .hasTestDb , found , "expected to find test db: %v, found: %v" , tc .hasTestDb , found )
231
+ })
232
+ }
233
+ })
234
+ mt .Run ("options" , func (mt * mtest.T ) {
235
+ allOpts := options .ListDatabases ().SetNameOnly (true ).SetAuthorizedDatabases (true )
236
+ mt .ClearEvents ()
237
+
238
+ _ , err := mt .Client .ListDatabaseNames (mtest .Background , bson.D {}, allOpts )
239
+ assert .Nil (mt , err , "ListDatabaseNames error: %v" , err )
240
+
241
+ evt := mt .GetStartedEvent ()
242
+ assert .Equal (mt , "listDatabases" , evt .CommandName , "expected " )
243
+
244
+ expectedDoc := bsoncore .BuildDocumentFromElements (nil ,
245
+ bsoncore .AppendBooleanElement (nil , "nameOnly" , true ),
246
+ bsoncore .AppendBooleanElement (nil , "authorizedDatabases" , true ),
247
+ )
248
+ err = compareDocs (mt , expectedDoc , evt .Command )
249
+ assert .Nil (mt , err , "compareDocs error: %v" , err )
250
+ })
212
251
})
213
252
mt .RunOpts ("ping" , noClientOpts , func (mt * mtest.T ) {
214
253
mt .Run ("default read preference" , func (mt * mtest.T ) {
0 commit comments