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