@@ -20,6 +20,8 @@ import (
20
20
"go.mongodb.org/mongo-driver/mongo"
21
21
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
22
22
"go.mongodb.org/mongo-driver/mongo/options"
23
+ "go.mongodb.org/mongo-driver/mongo/readconcern"
24
+ "go.mongodb.org/mongo-driver/mongo/writeconcern"
23
25
)
24
26
25
27
func TestSearchIndexProse (t * testing.T ) {
@@ -61,15 +63,16 @@ func TestSearchIndexProse(t *testing.T) {
61
63
if ! cursor .Next (ctx ) {
62
64
break
63
65
}
64
- if cursor .Current .Lookup ("queryable" ).Boolean () {
66
+ name := cursor .Current .Lookup ("name" ).StringValue ()
67
+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
68
+ if name == searchName && queryable {
65
69
doc = cursor .Current
66
70
} else {
67
71
t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
68
72
time .Sleep (5 * time .Second )
69
73
}
70
74
}
71
75
require .NotNil (mt , doc , "got empty document" )
72
- assert .Equal (mt , searchName , doc .Lookup ("name" ).StringValue (), "unmatched name" )
73
76
expected , err := bson .Marshal (definition )
74
77
require .NoError (mt , err , "failed to marshal definition" )
75
78
actual := doc .Lookup ("latestDefinition" ).Value
@@ -110,7 +113,9 @@ func TestSearchIndexProse(t *testing.T) {
110
113
if ! cursor .Next (ctx ) {
111
114
return nil
112
115
}
113
- if cursor .Current .Lookup ("queryable" ).Boolean () {
116
+ name := cursor .Current .Lookup ("name" ).StringValue ()
117
+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
118
+ if name == * opts .Name && queryable {
114
119
return cursor .Current
115
120
}
116
121
t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
@@ -126,7 +131,6 @@ func TestSearchIndexProse(t *testing.T) {
126
131
127
132
doc := getDocument (opts )
128
133
require .NotNil (mt , doc , "got empty document" )
129
- assert .Equal (mt , * opts .Name , doc .Lookup ("name" ).StringValue (), "unmatched name" )
130
134
expected , err := bson .Marshal (definition )
131
135
require .NoError (mt , err , "failed to marshal definition" )
132
136
actual := doc .Lookup ("latestDefinition" ).Value
@@ -162,15 +166,16 @@ func TestSearchIndexProse(t *testing.T) {
162
166
if ! cursor .Next (ctx ) {
163
167
break
164
168
}
165
- if cursor .Current .Lookup ("queryable" ).Boolean () {
169
+ name := cursor .Current .Lookup ("name" ).StringValue ()
170
+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
171
+ if name == searchName && queryable {
166
172
doc = cursor .Current
167
173
} else {
168
174
t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
169
175
time .Sleep (5 * time .Second )
170
176
}
171
177
}
172
178
require .NotNil (mt , doc , "got empty document" )
173
- require .Equal (mt , searchName , doc .Lookup ("name" ).StringValue (), "unmatched name" )
174
179
175
180
err = view .DropOne (ctx , searchName )
176
181
require .NoError (mt , err , "failed to drop index" )
@@ -204,33 +209,50 @@ func TestSearchIndexProse(t *testing.T) {
204
209
require .NoError (mt , err , "failed to create index" )
205
210
require .Equal (mt , searchName , index , "unmatched name" )
206
211
207
- getDocument := func () bson.Raw {
212
+ var doc bson.Raw
213
+ for doc == nil {
208
214
for {
209
215
cursor , err := view .List (ctx , opts )
210
216
require .NoError (mt , err , "failed to list" )
211
217
212
218
if ! cursor .Next (ctx ) {
213
- return nil
219
+ break
214
220
}
215
- if cursor .Current .Lookup ("queryable" ).Boolean () {
216
- return cursor .Current
221
+ name := cursor .Current .Lookup ("name" ).StringValue ()
222
+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
223
+ if name == searchName && queryable {
224
+ doc = cursor .Current
225
+ } else {
226
+ t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
227
+ time .Sleep (5 * time .Second )
217
228
}
218
- t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
219
- time .Sleep (5 * time .Second )
220
229
}
221
230
}
222
-
223
- doc := getDocument ()
224
231
require .NotNil (mt , doc , "got empty document" )
225
- require .Equal (mt , searchName , doc .Lookup ("name" ).StringValue (), "unmatched name" )
226
232
227
233
definition = bson.D {{"mappings" , bson.D {{"dynamic" , true }}}}
228
234
err = view .UpdateOne (ctx , searchName , definition )
229
- require .NoError (mt , err , "failed to drop index" )
230
- doc = getDocument ()
235
+ require .NoError (mt , err , "failed to update index" )
236
+ for doc == nil {
237
+ for {
238
+ cursor , err := view .List (ctx , opts )
239
+ require .NoError (mt , err , "failed to list" )
240
+
241
+ if ! cursor .Next (ctx ) {
242
+ break
243
+ }
244
+ name := cursor .Current .Lookup ("name" ).StringValue ()
245
+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
246
+ status := cursor .Current .Lookup ("status" ).StringValue ()
247
+ if name == searchName && queryable && status == "READY" {
248
+ doc = cursor .Current
249
+ } else {
250
+ t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
251
+ time .Sleep (5 * time .Second )
252
+ }
253
+ }
254
+ }
231
255
require .NotNil (mt , doc , "got empty document" )
232
- assert .Equal (mt , searchName , doc .Lookup ("name" ).StringValue (), "unmatched name" )
233
- assert .Equal (mt , "READY" , doc .Lookup ("status" ).StringValue (), "unexpected status" )
234
256
expected , err := bson .Marshal (definition )
235
257
require .NoError (mt , err , "failed to marshal definition" )
236
258
actual := doc .Lookup ("latestDefinition" ).Value
@@ -250,4 +272,49 @@ func TestSearchIndexProse(t *testing.T) {
250
272
err = collection .SearchIndexes ().DropOne (ctx , "foo" )
251
273
require .NoError (mt , err )
252
274
})
275
+
276
+ mt .RunOpts ("case 6: Driver can successfully create and list search indexes with non-default readConcern and writeConcern" ,
277
+ mtest .NewOptions ().CollectionOptions (options .Collection ().SetWriteConcern (writeconcern .New (writeconcern .W (1 ))).SetReadConcern (readconcern .Majority ())),
278
+ func (mt * mtest.T ) {
279
+ ctx := context .Background ()
280
+
281
+ _ , err := mt .Coll .InsertOne (ctx , bson.D {})
282
+ require .NoError (mt , err , "failed to insert" )
283
+
284
+ view := mt .Coll .SearchIndexes ()
285
+
286
+ definition := bson.D {{"mappings" , bson.D {{"dynamic" , false }}}}
287
+ searchName := "test-search-index6"
288
+ opts := options .SearchIndexes ().SetName (searchName )
289
+ index , err := view .CreateOne (ctx , mongo.SearchIndexModel {
290
+ Definition : definition ,
291
+ Options : opts ,
292
+ })
293
+ require .NoError (mt , err , "failed to create index" )
294
+ require .Equal (mt , searchName , index , "unmatched name" )
295
+ var doc bson.Raw
296
+ for doc == nil {
297
+ for {
298
+ cursor , err := view .List (ctx , opts )
299
+ require .NoError (mt , err , "failed to list" )
300
+
301
+ if ! cursor .Next (ctx ) {
302
+ break
303
+ }
304
+ name := cursor .Current .Lookup ("name" ).StringValue ()
305
+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
306
+ if name == searchName && queryable {
307
+ doc = cursor .Current
308
+ } else {
309
+ t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
310
+ time .Sleep (5 * time .Second )
311
+ }
312
+ }
313
+ }
314
+ require .NotNil (mt , doc , "got empty document" )
315
+ expected , err := bson .Marshal (definition )
316
+ require .NoError (mt , err , "failed to marshal definition" )
317
+ actual := doc .Lookup ("latestDefinition" ).Value
318
+ assert .Equal (mt , expected , actual , "unmatched definition" )
319
+ })
253
320
}
0 commit comments