@@ -20,6 +20,7 @@ import (
20
20
"go.mongodb.org/mongo-driver/v2/internal/assert"
21
21
"go.mongodb.org/mongo-driver/v2/internal/integration/mtest"
22
22
"go.mongodb.org/mongo-driver/v2/internal/israce"
23
+ "go.mongodb.org/mongo-driver/v2/internal/require"
23
24
"go.mongodb.org/mongo-driver/v2/mongo"
24
25
"go.mongodb.org/mongo-driver/v2/mongo/options"
25
26
)
@@ -74,19 +75,19 @@ func TestGridFS(x *testing.T) {
74
75
bucket := mt .DB .GridFSBucket (options .GridFSBucket ().SetChunkSizeBytes (chunkSize ))
75
76
76
77
ustream , err := bucket .OpenUploadStream (context .Background (), "foo" )
77
- assert . Nil (mt , err , "OpenUploadStream error: %v" , err )
78
+ require . NoError (mt , err , "OpenUploadStream error: %v" , err )
78
79
79
80
id := ustream .FileID
80
81
_ , err = ustream .Write (data )
81
- assert . Nil (mt , err , "Write error: %v" , err )
82
+ require . NoError (mt , err , "Write error: %v" , err )
82
83
err = ustream .Close ()
83
- assert . Nil (mt , err , "Close error: %v" , err )
84
+ require . NoError (mt , err , "Close error: %v" , err )
84
85
85
86
dstream , err := bucket .OpenDownloadStream (context .Background (), id )
86
- assert . Nil (mt , err , "OpenDownloadStream error" )
87
+ require . NoError (mt , err , "OpenDownloadStream error" )
87
88
dst := make ([]byte , tc .read )
88
89
_ , err = dstream .Read (dst )
89
- assert . Nil (mt , err , "Read error: %v" , err )
90
+ require . NoError (mt , err , "Read error: %v" , err )
90
91
91
92
n , err := dstream .Skip (tc .skip )
92
93
assert .Equal (mt , tc .expectedSkipErr , err , "expected error on Skip: %v, got %v" , tc .expectedSkipErr , err )
@@ -114,7 +115,7 @@ func TestGridFS(x *testing.T) {
114
115
mt .Cleanup (cancel )
115
116
116
117
_ , err := bucket .UploadFromStream (uploadCtx , "filename" , r )
117
- assert . Nil (mt , err , "UploadFromStream error: %v" , err )
118
+ require . NoError (mt , err , "UploadFromStream error: %v" , err )
118
119
119
120
findCtx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
120
121
mt .Cleanup (cancel )
@@ -169,7 +170,7 @@ func TestGridFS(x *testing.T) {
169
170
}},
170
171
},
171
172
)
172
- assert . Nil (mt , res .Err (), "createIndexes error: %v" , res .Err ())
173
+ require . NoError (mt , res .Err (), "createIndexes error: %v" , res .Err ())
173
174
174
175
res = mt .DB .RunCommand (context .Background (),
175
176
bson.D {
@@ -179,7 +180,7 @@ func TestGridFS(x *testing.T) {
179
180
}},
180
181
},
181
182
)
182
- assert . Nil (mt , res .Err (), "createIndexes error: %v" , res .Err ())
183
+ require . NoError (mt , res .Err (), "createIndexes error: %v" , res .Err ())
183
184
184
185
mt .ClearEvents ()
185
186
@@ -189,7 +190,7 @@ func TestGridFS(x *testing.T) {
189
190
}()
190
191
191
192
_ , err := bucket .OpenUploadStream (context .Background (), "filename" )
192
- assert . Nil (mt , err , "OpenUploadStream error: %v" , err )
193
+ require . NoError (mt , err , "OpenUploadStream error: %v" , err )
193
194
194
195
mt .FilterStartedEvents (func (evt * event.CommandStartedEvent ) bool {
195
196
return evt .CommandName == "createIndexes"
@@ -215,7 +216,7 @@ func TestGridFS(x *testing.T) {
215
216
}},
216
217
},
217
218
)
218
- assert . Nil (mt , res .Err (), "createIndexes error: %v" , res .Err ())
219
+ require . NoError (mt , res .Err (), "createIndexes error: %v" , res .Err ())
219
220
220
221
res = mt .DB .RunCommand (context .Background (),
221
222
bson.D {
@@ -225,7 +226,7 @@ func TestGridFS(x *testing.T) {
225
226
}},
226
227
},
227
228
)
228
- assert . Nil (mt , res .Err (), "createIndexes error: %v" , res .Err ())
229
+ require . NoError (mt , res .Err (), "createIndexes error: %v" , res .Err ())
229
230
230
231
mt .ClearEvents ()
231
232
var fileContent []byte
@@ -236,7 +237,7 @@ func TestGridFS(x *testing.T) {
236
237
}()
237
238
238
239
_ , err := bucket .UploadFromStream (context .Background (), "filename" , bytes .NewBuffer (fileContent ))
239
- assert . Nil (mt , err , "UploadFromStream error: %v" , err )
240
+ require . NoError (mt , err , "UploadFromStream error: %v" , err )
240
241
241
242
mt .FilterStartedEvents (func (evt * event.CommandStartedEvent ) bool {
242
243
return evt .CommandName == "createIndexes"
@@ -264,7 +265,7 @@ func TestGridFS(x *testing.T) {
264
265
fileData := []byte {1 , 2 , 3 , 4 }
265
266
fileMetadata := bson.D {{"k1" , "v1" }, {"k2" , "v2" }}
266
267
rawMetadata , err := bson .Marshal (fileMetadata )
267
- assert . Nil (mt , err , "Marshal error: %v" , err )
268
+ require . NoError (mt , err , "Marshal error: %v" , err )
268
269
uploadOpts := options .GridFSUpload ().SetMetadata (fileMetadata )
269
270
270
271
testCases := []struct {
@@ -288,13 +289,13 @@ func TestGridFS(x *testing.T) {
288
289
} else {
289
290
err = bucket .UploadFromStreamWithID (context .Background (), tc .fileID , fileName , dataReader , uploadOpts )
290
291
}
291
- assert . Nil (mt , err , "error uploading file: %v" , err )
292
+ require . NoError (mt , err , "error uploading file: %v" , err )
292
293
293
294
// The uploadDate field is calculated when the upload is complete. Manually fetch it from the
294
295
// fs.files collection to use in assertions.
295
296
filesColl := mt .DB .Collection ("fs.files" )
296
297
uploadedFileDoc , err := filesColl .FindOne (context .Background (), bson.D {}).Raw ()
297
- assert . Nil (mt , err , "FindOne error: %v" , err )
298
+ require . NoError (mt , err , "FindOne error: %v" , err )
298
299
uploadTime := uploadedFileDoc .Lookup ("uploadDate" ).Time ().UTC ()
299
300
300
301
expectedFile := & mongo.GridFSFile {
@@ -309,13 +310,13 @@ func TestGridFS(x *testing.T) {
309
310
// stream to the expected File object.
310
311
mt .RunOpts ("OpenDownloadStream" , noClientOpts , func (mt * mtest.T ) {
311
312
downloadStream , err := bucket .OpenDownloadStream (context .Background (), uploadedFileID )
312
- assert . Nil (mt , err , "OpenDownloadStream error: %v" , err )
313
+ require . NoError (mt , err , "OpenDownloadStream error: %v" , err )
313
314
actualFile := downloadStream .GetFile ()
314
315
assert .Equal (mt , expectedFile , actualFile , "expected file %v, got %v" , expectedFile , actualFile )
315
316
})
316
317
mt .RunOpts ("OpenDownloadStreamByName" , noClientOpts , func (mt * mtest.T ) {
317
318
downloadStream , err := bucket .OpenDownloadStreamByName (context .Background (), fileName )
318
- assert . Nil (mt , err , "OpenDownloadStream error: %v" , err )
319
+ require . NoError (mt , err , "OpenDownloadStream error: %v" , err )
319
320
actualFile := downloadStream .GetFile ()
320
321
assert .Equal (mt , expectedFile , actualFile , "expected file %v, got %v" , expectedFile , actualFile )
321
322
})
@@ -332,13 +333,13 @@ func TestGridFS(x *testing.T) {
332
333
fileData := []byte ("hello world" )
333
334
uploadOpts := options .GridFSUpload ().SetChunkSizeBytes (4 )
334
335
fileID , err := bucket .UploadFromStream (context .Background (), "file" , bytes .NewReader (fileData ), uploadOpts )
335
- assert . Nil (mt , err , "UploadFromStream error: %v" , err )
336
+ require . NoError (mt , err , "UploadFromStream error: %v" , err )
336
337
337
338
// If the bucket's chunk size was used, this would error because the actual chunk size is 4 and the bucket
338
339
// chunk size is 255 KB.
339
340
var downloadBuffer bytes.Buffer
340
341
_ , err = bucket .DownloadToStream (context .Background (), fileID , & downloadBuffer )
341
- assert . Nil (mt , err , "DownloadToStream error: %v" , err )
342
+ require . NoError (mt , err , "DownloadToStream error: %v" , err )
342
343
343
344
downloadedBytes := downloadBuffer .Bytes ()
344
345
assert .Equal (mt , fileData , downloadedBytes , "expected bytes %s, got %s" , fileData , downloadedBytes )
@@ -354,7 +355,7 @@ func TestGridFS(x *testing.T) {
354
355
{"filename" , "filename" },
355
356
}
356
357
_ , err := mt .DB .Collection ("fs.files" ).InsertOne (context .Background (), filesDoc )
357
- assert . Nil (mt , err , "InsertOne error for files collection: %v" , err )
358
+ require . NoError (mt , err , "InsertOne error for files collection: %v" , err )
358
359
359
360
bucket := mt .DB .GridFSBucket ()
360
361
defer func () { _ = bucket .Drop (context .Background ()) }()
@@ -376,7 +377,7 @@ func TestGridFS(x *testing.T) {
376
377
377
378
dataReader := bytes .NewReader (fileData )
378
379
_ , err := bucket .UploadFromStream (context .Background (), fileName , dataReader )
379
- assert . Nil (mt , err , "UploadFromStream error: %v" , err )
380
+ require . NoError (mt , err , "UploadFromStream error: %v" , err )
380
381
381
382
ctx , cancel := context .WithCancel (context .Background ())
382
383
@@ -403,17 +404,17 @@ func TestGridFS(x *testing.T) {
403
404
404
405
dataReader := bytes .NewReader (fileData )
405
406
_ , err := bucket .UploadFromStream (context .Background (), fileName , dataReader )
406
- assert . Nil (mt , err , "UploadFromStream error: %v" , err )
407
+ require . NoError (mt , err , "UploadFromStream error: %v" , err )
407
408
408
409
ctx , cancel := context .WithCancel (context .Background ())
409
410
410
411
ds , err := bucket .OpenDownloadStreamByName (ctx , fileName )
411
- assert . Nil (mt , err , "OpenDownloadStreamByName error: %v" , err )
412
+ require . NoError (mt , err , "OpenDownloadStreamByName error: %v" , err )
412
413
413
414
cancel ()
414
415
415
416
_ , err = ds .Skip (int64 (len (fileData )))
416
- assert .NotNil (mt , err , "expected error from Skip, got nil " )
417
+ assert .Error (mt , err , "expected error from Skip" )
417
418
assert .ErrorIs (mt , context .Canceled , err )
418
419
})
419
420
})
@@ -441,7 +442,7 @@ func TestGridFS(x *testing.T) {
441
442
defer func () { _ = bucket .Drop (context .Background ()) }()
442
443
443
444
_ , err := bucket .UploadFromStream (context .Background (), "accessors-test-file" , bytes .NewReader (fileData ))
444
- assert . Nil (mt , err , "UploadFromStream error: %v" , err )
445
+ require . NoError (mt , err , "UploadFromStream error: %v" , err )
445
446
446
447
bucketName := tc .bucketName
447
448
if bucketName == "" {
@@ -499,7 +500,7 @@ func TestGridFS(x *testing.T) {
499
500
mt .Cleanup (cancel )
500
501
501
502
_ , err := bucket .UploadFromStream (ctx , "filename" , bytes .NewReader (p ))
502
- assert . Nil (mt , err , "UploadFromStream error: %v" , err )
503
+ require . NoError (mt , err , "UploadFromStream error: %v" , err )
503
504
504
505
var w * bytes.Buffer
505
506
if test .bufSize == - 1 {
@@ -509,7 +510,7 @@ func TestGridFS(x *testing.T) {
509
510
}
510
511
511
512
_ , err = bucket .DownloadToStreamByName (ctx , "filename" , w )
512
- assert . Nil (mt , err , "DownloadToStreamByName error: %v" , err )
513
+ require . NoError (mt , err , "DownloadToStreamByName error: %v" , err )
513
514
assert .Equal (mt , p , w .Bytes (), "downloaded file did not match p" )
514
515
})
515
516
}
@@ -524,7 +525,7 @@ func TestGridFS(x *testing.T) {
524
525
_ = cursor .Close (context .Background ())
525
526
}()
526
527
527
- assert .Nil (mt , err , "Find error: %v" , err )
528
+ assert .NoError (mt , err , "Find error: %v" , err )
528
529
})
529
530
}
530
531
@@ -533,15 +534,15 @@ func assertGridFSCollectionState(mt *mtest.T, coll *mongo.Collection, expectedNa
533
534
534
535
assert .Equal (mt , expectedName , coll .Name (), "expected collection name %v, got %v" , expectedName , coll .Name ())
535
536
count , err := coll .CountDocuments (context .Background (), bson.D {})
536
- assert . Nil (mt , err , "CountDocuments error: %v" , err )
537
+ require . NoError (mt , err , "CountDocuments error: %v" , err )
537
538
assert .Equal (mt , expectedNumDocuments , count , "expected %d documents in collection, got %d" , expectedNumDocuments ,
538
539
count )
539
540
}
540
541
541
542
func findIndex (ctx context.Context , mt * mtest.T , coll * mongo.Collection , unique bool , keys ... string ) {
542
543
mt .Helper ()
543
544
cur , err := coll .Indexes ().List (ctx )
544
- assert . Nil (mt , err , "Indexes List error: %v" , err )
545
+ require . NoError (mt , err , "Indexes List error: %v" , err )
545
546
546
547
foundIndex := false
547
548
for cur .Next (ctx ) {
@@ -566,7 +567,7 @@ func skipRoundTripTest(mt *mtest.T) {
566
567
context .Background (),
567
568
bson.D {{"serverStatus" , 1 }},
568
569
).Decode (& serverStatus )
569
- assert . Nil (mt , err , "serverStatus error %v" , err )
570
+ require . NoError (mt , err , "serverStatus error %v" , err )
570
571
571
572
// can run on non-sharded clusters or on sharded cluster with auth/ssl disabled
572
573
_ , err = serverStatus .LookupErr ("sharding" )
0 commit comments