Skip to content

Commit 7fbb175

Browse files
authored
GODRIVER-2398 use CreateCollection() and CreateView() in mtest (#953)
1 parent 6e0334f commit 7fbb175

File tree

8 files changed

+43
-44
lines changed

8 files changed

+43
-44
lines changed

mongo/integration/client_side_encryption_prose_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,10 @@ func TestClientSideEncryptionProse(t *testing.T) {
387387

388388
// create view on db.coll
389389
mt.CreateCollection(mtest.Collection{
390-
Name: "view",
391-
DB: cpt.cseColl.Database().Name(),
392-
CreateOpts: bson.D{{"viewOn", "coll"}},
390+
Name: "view",
391+
DB: cpt.cseColl.Database().Name(),
392+
ViewOn: "coll",
393+
ViewPipeline: mongo.Pipeline{},
393394
}, true)
394395

395396
view := cpt.cseColl.Database().Collection("view")

mongo/integration/collection_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ func TestCollection(t *testing.T) {
311311
mt.RunOpts("write error", mtest.NewOptions().MaxServerVersion("5.0.7"), func(mt *mtest.T) {
312312
// Deletes are not allowed on capped collections on MongoDB 5.0.6-. We use this
313313
// behavior to test the processing of write errors.
314-
cappedOpts := bson.D{{"capped", true}, {"size", 64 * 1024}}
314+
cappedOpts := options.CreateCollection().SetCapped(true).SetSizeInBytes(64 * 1024)
315315
capped := mt.CreateCollection(mtest.Collection{
316316
Name: "deleteOne_capped",
317317
CreateOpts: cappedOpts,
@@ -380,7 +380,7 @@ func TestCollection(t *testing.T) {
380380
mt.RunOpts("write error", mtest.NewOptions().MaxServerVersion("5.0.7"), func(mt *mtest.T) {
381381
// Deletes are not allowed on capped collections on MongoDB 5.0.6-. We use this
382382
// behavior to test the processing of write errors.
383-
cappedOpts := bson.D{{"capped", true}, {"size", 64 * 1024}}
383+
cappedOpts := options.CreateCollection().SetCapped(true).SetSizeInBytes(64 * 1024)
384384
capped := mt.CreateCollection(mtest.Collection{
385385
Name: "deleteMany_capped",
386386
CreateOpts: cappedOpts,
@@ -1531,7 +1531,7 @@ func TestCollection(t *testing.T) {
15311531
// behavior to test the processing of write errors.
15321532
doc := mongo.NewDeleteOneModel().SetFilter(bson.D{{"x", 1}})
15331533
models := []mongo.WriteModel{doc, doc}
1534-
cappedOpts := bson.D{{"capped", true}, {"size", 64 * 1024}}
1534+
cappedOpts := options.CreateCollection().SetCapped(true).SetSizeInBytes(64 * 1024)
15351535
capped := mt.CreateCollection(mtest.Collection{
15361536
Name: "delete_write_errors",
15371537
CreateOpts: cappedOpts,
@@ -1628,7 +1628,7 @@ func TestCollection(t *testing.T) {
16281628
mt.RunOpts("unordered writeError index", mtest.NewOptions().MaxServerVersion("5.0.7"), func(mt *mtest.T) {
16291629
// Deletes are not allowed on capped collections on MongoDB 5.0.6-. We use this
16301630
// behavior to test the processing of write errors.
1631-
cappedOpts := bson.D{{"capped", true}, {"size", 64 * 1024}}
1631+
cappedOpts := options.CreateCollection().SetCapped(true).SetSizeInBytes(64 * 1024)
16321632
capped := mt.CreateCollection(mtest.Collection{
16331633
Name: "deleteOne_capped",
16341634
CreateOpts: cappedOpts,

mongo/integration/crud_prose_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,19 @@ func TestWriteErrorsDetails(t *testing.T) {
130130
// Create a JSON Schema validator document that requires properties "a" and "b". Use it in
131131
// the collection creation options so that collections created for subtests have the JSON
132132
// Schema validator applied.
133-
validator := bson.D{{
134-
Key: "validator",
135-
Value: bson.M{
136-
"$jsonSchema": bson.M{
137-
"bsonType": "object",
138-
"required": []string{"a", "b"},
139-
"properties": bson.M{
140-
"a": bson.M{"bsonType": "string"},
141-
"b": bson.M{"bsonType": "int"},
142-
},
133+
validator := bson.M{
134+
"$jsonSchema": bson.M{
135+
"bsonType": "object",
136+
"required": []string{"a", "b"},
137+
"properties": bson.M{
138+
"a": bson.M{"bsonType": "string"},
139+
"b": bson.M{"bsonType": "int"},
143140
},
144141
},
145-
}}
146-
validatorOpts := mtest.NewOptions().CollectionCreateOptions(validator)
142+
}
143+
144+
cco := options.CreateCollection().SetValidator(validator)
145+
validatorOpts := mtest.NewOptions().CollectionCreateOptions(cco)
147146

148147
cases := []struct {
149148
desc string

mongo/integration/cursor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const (
2626
func TestCursor(t *testing.T) {
2727
mt := mtest.New(t, mtest.NewOptions().CreateClient(false))
2828
defer mt.Close()
29-
cappedCollectionOpts := bson.D{{"capped", true}, {"size", 64 * 1024}}
29+
cappedCollectionOpts := options.CreateCollection().SetCapped(true).SetSizeInBytes(64 * 1024)
3030

3131
// Server versions 2.6 and 3.0 use OP_GET_MORE so this works on >= 3.2 and when RequireAPIVersion is false;
3232
// getMore cannot be sent with RunCommand as server API options will be attached when they should not be.

mongo/integration/database_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func TestDatabase(t *testing.T) {
199199
mt.CreateCollection(mtest.Collection{Name: listCollUncapped}, true)
200200
mt.CreateCollection(mtest.Collection{
201201
Name: listCollCapped,
202-
CreateOpts: bson.D{{"capped", true}, {"size", 64 * 1024}},
202+
CreateOpts: options.CreateCollection().SetCapped(true).SetSizeInBytes(64 * 1024),
203203
}, true)
204204

205205
filter := bson.D{}
@@ -281,11 +281,8 @@ func TestDatabase(t *testing.T) {
281281
// Test that ListCollectionSpecifications correctly uses the supplied filter.
282282
cappedName := "list-collection-specs-capped"
283283
mt.CreateCollection(mtest.Collection{
284-
Name: cappedName,
285-
CreateOpts: bson.D{
286-
{"capped", true},
287-
{"size", 4096},
288-
},
284+
Name: cappedName,
285+
CreateOpts: options.CreateCollection().SetCapped(true).SetSizeInBytes(4096),
289286
}, true)
290287

291288
filter := bson.M{

mongo/integration/mtest/mongotest.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ type T struct {
107107
enterprise *bool
108108
dataLake *bool
109109
ssl *bool
110-
collCreateOpts bson.D
110+
collCreateOpts *options.CreateCollectionOptions
111111
requireAPIVersion *bool
112112

113113
// options copied to sub-tests
@@ -416,12 +416,13 @@ func (t *T) ResetClient(opts *options.ClientOptions) {
416416

417417
// Collection is used to configure a new collection created during a test.
418418
type Collection struct {
419-
Name string
420-
DB string // defaults to mt.DB.Name() if not specified
421-
Client *mongo.Client // defaults to mt.Client if not specified
422-
Opts *options.CollectionOptions
423-
CreateOpts bson.D
424-
419+
Name string
420+
DB string // defaults to mt.DB.Name() if not specified
421+
Client *mongo.Client // defaults to mt.Client if not specified
422+
Opts *options.CollectionOptions
423+
CreateOpts *options.CreateCollectionOptions
424+
ViewOn string
425+
ViewPipeline interface{}
425426
hasDifferentClient bool
426427
created *mongo.Collection // the actual collection that was created
427428
}
@@ -441,15 +442,20 @@ func (t *T) CreateCollection(coll Collection, createOnServer bool) *mongo.Collec
441442
db := coll.Client.Database(coll.DB)
442443

443444
if createOnServer && t.clientType != Mock {
444-
cmd := bson.D{{"create", coll.Name}}
445-
cmd = append(cmd, coll.CreateOpts...)
445+
var err error
446+
if coll.ViewOn != "" {
447+
err = db.CreateView(context.Background(), coll.Name, coll.ViewOn, coll.ViewPipeline)
448+
} else {
449+
err = db.CreateCollection(context.Background(), coll.Name, coll.CreateOpts)
450+
}
446451

447-
if err := db.RunCommand(context.Background(), cmd).Err(); err != nil {
452+
// ignore ErrUnacknowledgedWrite. Client may be configured with unacknowledged write concern.
453+
if err != nil && err != driver.ErrUnacknowledgedWrite {
448454
// ignore NamespaceExists errors for idempotency
449455

450456
cmdErr, ok := err.(mongo.CommandError)
451457
if !ok || cmdErr.Code != namespaceExistsErrCode {
452-
t.Fatalf("error creating collection %v on server: %v", coll.Name, err)
458+
t.Fatalf("error creating collection or view: %v on server: %v", coll.Name, err)
453459
}
454460
}
455461
}

mongo/integration/mtest/options.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,8 @@ func NewOptions() *Options {
117117
return &Options{}
118118
}
119119

120-
// CollectionCreateOptions sets the options to pass to the create command when creating a collection for a test.
121-
// For example, if opts = {"capped": "true"}, the create command sent to the server will be
122-
// {create: <collectionName>, foo: bar}.
123-
func (op *Options) CollectionCreateOptions(opts bson.D) *Options {
120+
// CollectionCreateOptions sets the options to pass to Database.CreateCollection() when creating a collection for a test.
121+
func (op *Options) CollectionCreateOptions(opts *options.CreateCollectionOptions) *Options {
124122
op.optFuncs = append(op.optFuncs, func(t *T) {
125123
t.collCreateOpts = opts
126124
})

mongo/integration/unified_spec_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,7 @@ func runSpecTestCase(mt *mtest.T, test *testCase, testFile testFile) {
236236
validator := bson.D{
237237
{"$jsonSchema", testFile.JSONSchema},
238238
}
239-
opts.CollectionCreateOptions(bson.D{
240-
{"validator", validator},
241-
})
239+
opts.CollectionCreateOptions(options.CreateCollection().SetValidator(validator))
242240
}
243241

244242
// Start the test without setting client options so the setup will be done with a default client.

0 commit comments

Comments
 (0)