@@ -250,6 +250,42 @@ func TestGridFS(x *testing.T) {
250
250
})
251
251
})
252
252
253
+ mt .RunOpts ("bucket collection accessors" , noClientOpts , func (mt * mtest.T ) {
254
+ // Tests for the GetFilesCollection and GetChunksCollection accessors.
255
+
256
+ fileData := []byte {1 , 2 , 3 , 4 }
257
+ var chunkSize int32 = 2
258
+
259
+ testCases := []struct {
260
+ name string
261
+ bucketName string // defaults to "fs"
262
+ }{
263
+ {"default bucket name" , "" },
264
+ {"custom bucket name" , "bucket" },
265
+ }
266
+ for _ , tc := range testCases {
267
+ mt .Run (tc .name , func (mt * mtest.T ) {
268
+ bucketOpts := options .GridFSBucket ().SetChunkSizeBytes (chunkSize )
269
+ if tc .bucketName != "" {
270
+ bucketOpts .SetName (tc .bucketName )
271
+ }
272
+ bucket , err := gridfs .NewBucket (mt .DB , bucketOpts )
273
+ assert .Nil (mt , err , "NewBucket error: %v" , err )
274
+ defer func () { _ = bucket .Drop () }()
275
+
276
+ _ , err = bucket .UploadFromStream ("accessors-test-file" , bytes .NewReader (fileData ))
277
+ assert .Nil (mt , err , "UploadFromStream error: %v" , err )
278
+
279
+ bucketName := tc .bucketName
280
+ if bucketName == "" {
281
+ bucketName = "fs"
282
+ }
283
+ assertGridFSCollectionState (mt , bucket .GetFilesCollection (), bucketName + ".files" , 1 )
284
+ assertGridFSCollectionState (mt , bucket .GetChunksCollection (), bucketName + ".chunks" , 2 )
285
+ })
286
+ }
287
+ })
288
+
253
289
mt .RunOpts ("round trip" , mtest .NewOptions ().MaxServerVersion ("3.6" ), func (mt * mtest.T ) {
254
290
skipRoundTripTest (mt )
255
291
oneK := 1024
@@ -317,6 +353,16 @@ func TestGridFS(x *testing.T) {
317
353
})
318
354
}
319
355
356
+ func assertGridFSCollectionState (mt * mtest.T , coll * mongo.Collection , expectedName string , expectedNumDocuments int64 ) {
357
+ mt .Helper ()
358
+
359
+ assert .Equal (mt , expectedName , coll .Name (), "expected collection name %v, got %v" , expectedName , coll .Name ())
360
+ count , err := coll .CountDocuments (mtest .Background , bson.D {})
361
+ assert .Nil (mt , err , "CountDocuments error: %v" , err )
362
+ assert .Equal (mt , expectedNumDocuments , count , "expected %d documents in collection, got %d" , expectedNumDocuments ,
363
+ count )
364
+ }
365
+
320
366
func findIndex (ctx context.Context , mt * mtest.T , coll * mongo.Collection , unique bool , keys ... string ) {
321
367
mt .Helper ()
322
368
cur , err := coll .Indexes ().List (ctx )
0 commit comments