@@ -10,6 +10,7 @@ import (
1010 "context"
1111 "testing"
1212
13+ "go.mongodb.org/mongo-driver/v2/bson"
1314 "go.mongodb.org/mongo-driver/v2/event"
1415 "go.mongodb.org/mongo-driver/v2/internal/assert"
1516 "go.mongodb.org/mongo-driver/v2/internal/integtest"
@@ -109,3 +110,54 @@ func TestGridFS(t *testing.T) {
109110 }
110111 })
111112}
113+
114+ func TestGridFSFile_UnmarshalBSON (t * testing.T ) {
115+ client , err := Connect ()
116+ require .NoError (t , err )
117+
118+ defer func () {
119+ err := client .Disconnect (context .Background ())
120+ require .NoError (t , err )
121+ }()
122+
123+ // Get the database and create a GridFS bucket
124+ db := client .Database ("gridfs_test_db" )
125+
126+ // Drop the collection
127+ err = db .Collection ("myfiles.files" ).Drop (context .Background ())
128+ require .NoError (t , err )
129+
130+ err = db .Collection ("myfiles.chunks" ).Drop (context .Background ())
131+ require .NoError (t , err )
132+
133+ bucket := db .GridFSBucket (options .GridFSBucket ().SetName ("myfiles" ))
134+
135+ // Data to upload
136+ fileName := "example-file.txt"
137+ fileContent := []byte ("Hello GridFS! This is a test file." )
138+
139+ // Upload data into GridFS
140+ uploadStream , err := bucket .OpenUploadStream (context .Background (), fileName )
141+ require .NoError (t , err )
142+
143+ _ , err = uploadStream .Write (fileContent )
144+ require .NoError (t , err )
145+
146+ uploadStream .Close ()
147+
148+ // Verify the file metadata
149+ fileCursor , err := bucket .Find (context .Background (), bson.D {})
150+ require .NoError (t , err )
151+
152+ for fileCursor .Next (context .Background ()) {
153+ var file GridFSFile
154+ err := fileCursor .Decode (& file )
155+ require .NoError (t , err )
156+
157+ assert .NotNil (t , file .ID )
158+ assert .Equal (t , int64 (34 ), file .Length )
159+ assert .Equal (t , int32 (261120 ), file .ChunkSize )
160+ assert .NotNil (t , file .UploadDate )
161+ assert .Equal (t , fileName , file .Name )
162+ }
163+ }
0 commit comments