Skip to content

Commit bf1d085

Browse files
committed
GODRIVER-3122 Update prose tests for index management.
1 parent 556e2f2 commit bf1d085

File tree

3 files changed

+89
-22
lines changed

3 files changed

+89
-22
lines changed

mongo/integration/search_index_prose_test.go

+86-19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"go.mongodb.org/mongo-driver/mongo"
2121
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
2222
"go.mongodb.org/mongo-driver/mongo/options"
23+
"go.mongodb.org/mongo-driver/mongo/readconcern"
24+
"go.mongodb.org/mongo-driver/mongo/writeconcern"
2325
)
2426

2527
func TestSearchIndexProse(t *testing.T) {
@@ -61,15 +63,16 @@ func TestSearchIndexProse(t *testing.T) {
6163
if !cursor.Next(ctx) {
6264
break
6365
}
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 {
6569
doc = cursor.Current
6670
} else {
6771
t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String())
6872
time.Sleep(5 * time.Second)
6973
}
7074
}
7175
require.NotNil(mt, doc, "got empty document")
72-
assert.Equal(mt, searchName, doc.Lookup("name").StringValue(), "unmatched name")
7376
expected, err := bson.Marshal(definition)
7477
require.NoError(mt, err, "failed to marshal definition")
7578
actual := doc.Lookup("latestDefinition").Value
@@ -110,7 +113,9 @@ func TestSearchIndexProse(t *testing.T) {
110113
if !cursor.Next(ctx) {
111114
return nil
112115
}
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 {
114119
return cursor.Current
115120
}
116121
t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String())
@@ -126,7 +131,6 @@ func TestSearchIndexProse(t *testing.T) {
126131

127132
doc := getDocument(opts)
128133
require.NotNil(mt, doc, "got empty document")
129-
assert.Equal(mt, *opts.Name, doc.Lookup("name").StringValue(), "unmatched name")
130134
expected, err := bson.Marshal(definition)
131135
require.NoError(mt, err, "failed to marshal definition")
132136
actual := doc.Lookup("latestDefinition").Value
@@ -162,15 +166,16 @@ func TestSearchIndexProse(t *testing.T) {
162166
if !cursor.Next(ctx) {
163167
break
164168
}
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 {
166172
doc = cursor.Current
167173
} else {
168174
t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String())
169175
time.Sleep(5 * time.Second)
170176
}
171177
}
172178
require.NotNil(mt, doc, "got empty document")
173-
require.Equal(mt, searchName, doc.Lookup("name").StringValue(), "unmatched name")
174179

175180
err = view.DropOne(ctx, searchName)
176181
require.NoError(mt, err, "failed to drop index")
@@ -204,33 +209,50 @@ func TestSearchIndexProse(t *testing.T) {
204209
require.NoError(mt, err, "failed to create index")
205210
require.Equal(mt, searchName, index, "unmatched name")
206211

207-
getDocument := func() bson.Raw {
212+
var doc bson.Raw
213+
for doc == nil {
208214
for {
209215
cursor, err := view.List(ctx, opts)
210216
require.NoError(mt, err, "failed to list")
211217

212218
if !cursor.Next(ctx) {
213-
return nil
219+
break
214220
}
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)
217228
}
218-
t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String())
219-
time.Sleep(5 * time.Second)
220229
}
221230
}
222-
223-
doc := getDocument()
224231
require.NotNil(mt, doc, "got empty document")
225-
require.Equal(mt, searchName, doc.Lookup("name").StringValue(), "unmatched name")
226232

227233
definition = bson.D{{"mappings", bson.D{{"dynamic", true}}}}
228234
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+
}
231255
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")
234256
expected, err := bson.Marshal(definition)
235257
require.NoError(mt, err, "failed to marshal definition")
236258
actual := doc.Lookup("latestDefinition").Value
@@ -250,4 +272,49 @@ func TestSearchIndexProse(t *testing.T) {
250272
err = collection.SearchIndexes().DropOne(ctx, "foo")
251273
require.NoError(mt, err)
252274
})
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+
})
253320
}

testdata/index-management/searchIndexIgnoresReadWriteConcern.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
]
9696
},
9797
{
98-
"description": "createSearchIndex ignores read and write concern",
98+
"description": "createSearchIndexes ignores read and write concern",
9999
"operations": [
100100
{
101101
"name": "createSearchIndexes",

testdata/index-management/searchIndexIgnoresReadWriteConcern.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ tests:
4949
writeConcern: { $$exists: false }
5050
readConcern: { $$exists: false }
5151

52-
- description: "createSearchIndex ignores read and write concern"
52+
- description: "createSearchIndexes ignores read and write concern"
5353
operations:
5454
- name: createSearchIndexes
5555
object: *collection0
56-
arguments:
56+
arguments:
5757
models: []
5858
expectError:
5959
# This test always errors in a non-Atlas environment. The test functions as a unit test by asserting

0 commit comments

Comments
 (0)