Skip to content

Commit 06f64ee

Browse files
author
Divjot Arora
authored
GODRIVER-1546 Add accessors for GridFS collections (#354)
1 parent 4d93396 commit 06f64ee

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

mongo/gridfs/bucket.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@ func (b *Bucket) Drop() error {
351351
return b.chunksColl.Drop(ctx)
352352
}
353353

354+
// GetFilesCollection returns a handle to the collection that stores the file documents for this bucket.
355+
func (b *Bucket) GetFilesCollection() *mongo.Collection {
356+
return b.filesColl
357+
}
358+
359+
// GetChunksCollection returns a handle to the collection that stores the file chunks for this bucket.
360+
func (b *Bucket) GetChunksCollection() *mongo.Collection {
361+
return b.chunksColl
362+
}
363+
354364
func (b *Bucket) openDownloadStream(filter interface{}, opts ...*options.FindOptions) (*DownloadStream, error) {
355365
ctx, cancel := deadlineContext(b.readDeadline)
356366
if cancel != nil {

mongo/integration/gridfs_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,42 @@ func TestGridFS(x *testing.T) {
250250
})
251251
})
252252

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+
253289
mt.RunOpts("round trip", mtest.NewOptions().MaxServerVersion("3.6"), func(mt *mtest.T) {
254290
skipRoundTripTest(mt)
255291
oneK := 1024
@@ -317,6 +353,16 @@ func TestGridFS(x *testing.T) {
317353
})
318354
}
319355

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+
320366
func findIndex(ctx context.Context, mt *mtest.T, coll *mongo.Collection, unique bool, keys ...string) {
321367
mt.Helper()
322368
cur, err := coll.Indexes().List(ctx)

0 commit comments

Comments
 (0)